blob: d274bf16a753030ad40d6e3e7463ca0252585455 [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
11#include "webrtc/modules/video_coding/include/video_codec_initializer.h"
12
Erik Språng08127a92016-11-16 16:41:30 +010013#include "webrtc/common_types.h"
ilnik04f4d122017-06-19 07:18:55 -070014#include "webrtc/common_video/include/video_bitrate_allocator.h"
Erik Språng08127a92016-11-16 16:41:30 +010015#include "webrtc/modules/video_coding/codecs/vp8/screenshare_layers.h"
kjellandera8d8aad2017-03-08 05:42:26 -080016#include "webrtc/modules/video_coding/codecs/vp8/simulcast_rate_allocator.h"
Erik Språng08127a92016-11-16 16:41:30 +010017#include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"
ilnik04f4d122017-06-19 07:18:55 -070018#include "webrtc/modules/video_coding/include/video_coding_defines.h"
Erik Språng08127a92016-11-16 16:41:30 +010019#include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/basictypes.h"
21#include "webrtc/rtc_base/logging.h"
Erik Språng08127a92016-11-16 16:41:30 +010022#include "webrtc/system_wrappers/include/clock.h"
23
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) {
asapersson5f7226f2016-11-25 04:37:00 -080042 *codec =
43 VideoEncoderConfigToVideoCodec(config, streams, settings.payload_name,
44 settings.payload_type, nack_enabled);
Erik Språng08127a92016-11-16 16:41:30 +010045
46 std::unique_ptr<TemporalLayersFactory> tl_factory;
47 switch (codec->codecType) {
48 case kVideoCodecVP8: {
49 if (!codec->VP8()->tl_factory) {
50 if (codec->mode == kScreensharing &&
sprang429600d2017-01-26 06:12:26 -080051 (codec->numberOfSimulcastStreams > 1 ||
52 (codec->numberOfSimulcastStreams == 1 &&
53 codec->VP8()->numberOfTemporalLayers == 2))) {
Erik Språng08127a92016-11-16 16:41:30 +010054 // Conference mode temporal layering for screen content.
55 tl_factory.reset(new ScreenshareTemporalLayersFactory());
56 } else {
57 // Standard video temporal layers.
58 tl_factory.reset(new TemporalLayersFactory());
59 }
60 codec->VP8()->tl_factory = tl_factory.get();
61 }
62 break;
63 }
64 default: {
65 // TODO(sprang): Warn, once we have specific allocators for all supported
66 // codec types.
67 break;
68 }
69 }
70 *bitrate_allocator = CreateBitrateAllocator(*codec, std::move(tl_factory));
71
72 return true;
73}
74
75std::unique_ptr<VideoBitrateAllocator>
76VideoCodecInitializer::CreateBitrateAllocator(
77 const VideoCodec& codec,
78 std::unique_ptr<TemporalLayersFactory> tl_factory) {
79 std::unique_ptr<VideoBitrateAllocator> rate_allocator;
80
81 switch (codec.codecType) {
82 case kVideoCodecVP8: {
83 // Set up default VP8 temporal layer factory, if not provided.
84 rate_allocator.reset(
85 new SimulcastRateAllocator(codec, std::move(tl_factory)));
86 } break;
87 default:
88 rate_allocator.reset(new DefaultVideoBitrateAllocator(codec));
89 }
90
91 return rate_allocator;
92}
93
94// TODO(sprang): Split this up and separate the codec specific parts.
95VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
96 const VideoEncoderConfig& config,
97 const std::vector<VideoStream>& streams,
98 const std::string& payload_name,
asapersson5f7226f2016-11-25 04:37:00 -080099 int payload_type,
100 bool nack_enabled) {
Erik Språng08127a92016-11-16 16:41:30 +0100101 static const int kEncoderMinBitrateKbps = 30;
102 RTC_DCHECK(!streams.empty());
103 RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0);
104
105 VideoCodec video_codec;
106 memset(&video_codec, 0, sizeof(video_codec));
kthelgason1cdddc92017-08-24 03:52:48 -0700107 video_codec.codecType = PayloadStringToCodecType(payload_name);
Erik Språng08127a92016-11-16 16:41:30 +0100108
109 switch (config.content_type) {
110 case VideoEncoderConfig::ContentType::kRealtimeVideo:
111 video_codec.mode = kRealtimeVideo;
112 break;
113 case VideoEncoderConfig::ContentType::kScreen:
114 video_codec.mode = kScreensharing;
aleloi47037412017-01-26 07:57:15 -0800115 if (!streams.empty() &&
Erik Språng08127a92016-11-16 16:41:30 +0100116 streams[0].temporal_layer_thresholds_bps.size() == 1) {
117 video_codec.targetBitrate =
118 streams[0].temporal_layer_thresholds_bps[0] / 1000;
119 }
120 break;
121 }
122
123 if (config.encoder_specific_settings)
124 config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec);
125
126 switch (video_codec.codecType) {
127 case kVideoCodecVP8: {
128 if (!config.encoder_specific_settings)
129 *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings();
130 video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>(
131 streams.back().temporal_layer_thresholds_bps.size() + 1);
emircanbbcc3562017-08-18 00:28:40 -0700132
133 if (nack_enabled && !TemporalLayersConfigured(streams)) {
asapersson5f7226f2016-11-25 04:37:00 -0800134 LOG(LS_INFO) << "No temporal layers and nack enabled -> resilience off";
135 video_codec.VP8()->resilience = kResilienceOff;
136 }
Erik Språng08127a92016-11-16 16:41:30 +0100137 break;
138 }
139 case kVideoCodecVP9: {
140 if (!config.encoder_specific_settings)
141 *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings();
142 if (video_codec.mode == kScreensharing &&
143 config.encoder_specific_settings) {
144 video_codec.VP9()->flexibleMode = true;
145 // For now VP9 screensharing use 1 temporal and 2 spatial layers.
146 RTC_DCHECK_EQ(1, video_codec.VP9()->numberOfTemporalLayers);
147 RTC_DCHECK_EQ(2, video_codec.VP9()->numberOfSpatialLayers);
148 }
149 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
150 streams.back().temporal_layer_thresholds_bps.size() + 1);
emircanbbcc3562017-08-18 00:28:40 -0700151
152 if (nack_enabled && !TemporalLayersConfigured(streams) &&
153 video_codec.VP9()->numberOfSpatialLayers == 1) {
154 LOG(LS_INFO) << "No temporal or spatial layers and nack enabled -> "
155 << "resilience off";
156 video_codec.VP9()->resilienceOn = false;
157 }
Erik Språng08127a92016-11-16 16:41:30 +0100158 break;
159 }
160 case kVideoCodecH264: {
161 if (!config.encoder_specific_settings)
162 *video_codec.H264() = VideoEncoder::GetDefaultH264Settings();
163 break;
164 }
165 default:
166 // TODO(pbos): Support encoder_settings codec-agnostically.
167 RTC_DCHECK(!config.encoder_specific_settings)
168 << "Encoder-specific settings for codec type not wired up.";
169 break;
170 }
171
172 strncpy(video_codec.plName, payload_name.c_str(), kPayloadNameSize - 1);
173 video_codec.plName[kPayloadNameSize - 1] = '\0';
174 video_codec.plType = payload_type;
175 video_codec.numberOfSimulcastStreams =
176 static_cast<unsigned char>(streams.size());
177 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000;
178 if (video_codec.minBitrate < kEncoderMinBitrateKbps)
179 video_codec.minBitrate = kEncoderMinBitrateKbps;
ilnik04f4d122017-06-19 07:18:55 -0700180 video_codec.timing_frame_thresholds = {kDefaultTimingFramesDelayMs,
181 kDefaultOutlierFrameSizePercent};
kwiberg352444f2016-11-28 15:58:53 -0800182 RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams);
Erik Språng08127a92016-11-16 16:41:30 +0100183 if (video_codec.codecType == kVideoCodecVP9) {
184 // If the vector is empty, bitrates will be configured automatically.
185 RTC_DCHECK(config.spatial_layers.empty() ||
186 config.spatial_layers.size() ==
187 video_codec.VP9()->numberOfSpatialLayers);
188 RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers,
189 kMaxSimulcastStreams);
190 for (size_t i = 0; i < config.spatial_layers.size(); ++i)
191 video_codec.spatialLayers[i] = config.spatial_layers[i];
192 }
193 for (size_t i = 0; i < streams.size(); ++i) {
194 SimulcastStream* sim_stream = &video_codec.simulcastStream[i];
kwibergaf476c72016-11-28 15:21:39 -0800195 RTC_DCHECK_GT(streams[i].width, 0);
196 RTC_DCHECK_GT(streams[i].height, 0);
Erik Språng08127a92016-11-16 16:41:30 +0100197 RTC_DCHECK_GT(streams[i].max_framerate, 0);
sprang429600d2017-01-26 06:12:26 -0800198 // Different framerates not supported per stream at the moment, unless it's
199 // screenshare where there is an exception and a simulcast encoder adapter,
200 // which supports different framerates, is used instead.
201 if (config.content_type != VideoEncoderConfig::ContentType::kScreen) {
202 RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate);
203 }
Erik Språng08127a92016-11-16 16:41:30 +0100204 RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0);
205 RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps);
206 RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps);
207 RTC_DCHECK_GE(streams[i].max_qp, 0);
208
209 sim_stream->width = static_cast<uint16_t>(streams[i].width);
210 sim_stream->height = static_cast<uint16_t>(streams[i].height);
211 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000;
212 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000;
213 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000;
214 sim_stream->qpMax = streams[i].max_qp;
215 sim_stream->numberOfTemporalLayers = static_cast<unsigned char>(
216 streams[i].temporal_layer_thresholds_bps.size() + 1);
217
218 video_codec.width =
219 std::max(video_codec.width, static_cast<uint16_t>(streams[i].width));
220 video_codec.height =
221 std::max(video_codec.height, static_cast<uint16_t>(streams[i].height));
222 video_codec.minBitrate =
223 std::min(static_cast<uint16_t>(video_codec.minBitrate),
224 static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000));
225 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000;
226 video_codec.qpMax = std::max(video_codec.qpMax,
227 static_cast<unsigned int>(streams[i].max_qp));
228 }
229
230 if (video_codec.maxBitrate == 0) {
231 // Unset max bitrate -> cap to one bit per pixel.
232 video_codec.maxBitrate =
233 (video_codec.width * video_codec.height * video_codec.maxFramerate) /
234 1000;
235 }
236 if (video_codec.maxBitrate < kEncoderMinBitrateKbps)
237 video_codec.maxBitrate = kEncoderMinBitrateKbps;
238
239 RTC_DCHECK_GT(streams[0].max_framerate, 0);
240 video_codec.maxFramerate = streams[0].max_framerate;
241 return video_codec;
242}
243
244} // namespace webrtc