Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/include/video_codec_initializer.h" |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 12 | |
Anders Carlsson | dd8c165 | 2018-01-30 10:32:13 +0100 | [diff] [blame] | 13 | #include "api/video_codecs/video_encoder.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 14 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #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ång | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | bool VideoCodecInitializer::SetupCodec( |
| 28 | const VideoEncoderConfig& config, |
| 29 | const VideoSendStream::Config::EncoderSettings settings, |
| 30 | const std::vector<VideoStream>& streams, |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 31 | bool nack_enabled, |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 32 | VideoCodec* codec, |
| 33 | std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator) { |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 34 | if (PayloadStringToCodecType(settings.payload_name) == kVideoCodecMultiplex) { |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 35 | VideoSendStream::Config::EncoderSettings associated_codec_settings = |
| 36 | settings; |
| 37 | associated_codec_settings.payload_name = |
| 38 | CodecTypeToPayloadString(kVideoCodecVP9); |
| 39 | if (!SetupCodec(config, associated_codec_settings, streams, nack_enabled, |
| 40 | codec, bitrate_allocator)) { |
| 41 | RTC_LOG(LS_ERROR) << "Failed to create stereo encoder configuration."; |
| 42 | return false; |
| 43 | } |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 44 | codec->codecType = kVideoCodecMultiplex; |
Qiang Chen | a9329db | 2017-12-14 14:05:43 -0800 | [diff] [blame] | 45 | strncpy(codec->plName, settings.payload_name.c_str(), |
| 46 | sizeof(codec->plName)); |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 47 | return true; |
| 48 | } |
| 49 | |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 50 | *codec = |
| 51 | VideoEncoderConfigToVideoCodec(config, streams, settings.payload_name, |
| 52 | settings.payload_type, nack_enabled); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 53 | |
| 54 | std::unique_ptr<TemporalLayersFactory> tl_factory; |
| 55 | switch (codec->codecType) { |
| 56 | case kVideoCodecVP8: { |
| 57 | if (!codec->VP8()->tl_factory) { |
| 58 | if (codec->mode == kScreensharing && |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 59 | (codec->numberOfSimulcastStreams > 1 || |
| 60 | (codec->numberOfSimulcastStreams == 1 && |
| 61 | codec->VP8()->numberOfTemporalLayers == 2))) { |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 62 | // Conference mode temporal layering for screen content. |
| 63 | tl_factory.reset(new ScreenshareTemporalLayersFactory()); |
| 64 | } else { |
| 65 | // Standard video temporal layers. |
| 66 | tl_factory.reset(new TemporalLayersFactory()); |
| 67 | } |
| 68 | codec->VP8()->tl_factory = tl_factory.get(); |
| 69 | } |
| 70 | break; |
| 71 | } |
| 72 | default: { |
| 73 | // TODO(sprang): Warn, once we have specific allocators for all supported |
| 74 | // codec types. |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | *bitrate_allocator = CreateBitrateAllocator(*codec, std::move(tl_factory)); |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | std::unique_ptr<VideoBitrateAllocator> |
| 84 | VideoCodecInitializer::CreateBitrateAllocator( |
| 85 | const VideoCodec& codec, |
| 86 | std::unique_ptr<TemporalLayersFactory> tl_factory) { |
| 87 | std::unique_ptr<VideoBitrateAllocator> rate_allocator; |
| 88 | |
| 89 | switch (codec.codecType) { |
| 90 | case kVideoCodecVP8: { |
| 91 | // Set up default VP8 temporal layer factory, if not provided. |
| 92 | rate_allocator.reset( |
| 93 | new SimulcastRateAllocator(codec, std::move(tl_factory))); |
| 94 | } break; |
| 95 | default: |
| 96 | rate_allocator.reset(new DefaultVideoBitrateAllocator(codec)); |
| 97 | } |
| 98 | |
| 99 | return rate_allocator; |
| 100 | } |
| 101 | |
| 102 | // TODO(sprang): Split this up and separate the codec specific parts. |
| 103 | VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec( |
| 104 | const VideoEncoderConfig& config, |
| 105 | const std::vector<VideoStream>& streams, |
| 106 | const std::string& payload_name, |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 107 | int payload_type, |
| 108 | bool nack_enabled) { |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 109 | static const int kEncoderMinBitrateKbps = 30; |
| 110 | RTC_DCHECK(!streams.empty()); |
| 111 | RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0); |
| 112 | |
| 113 | VideoCodec video_codec; |
| 114 | memset(&video_codec, 0, sizeof(video_codec)); |
kthelgason | 1cdddc9 | 2017-08-24 03:52:48 -0700 | [diff] [blame] | 115 | video_codec.codecType = PayloadStringToCodecType(payload_name); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 116 | |
| 117 | switch (config.content_type) { |
| 118 | case VideoEncoderConfig::ContentType::kRealtimeVideo: |
| 119 | video_codec.mode = kRealtimeVideo; |
| 120 | break; |
| 121 | case VideoEncoderConfig::ContentType::kScreen: |
| 122 | video_codec.mode = kScreensharing; |
Sergey Silkin | d2ed0a4 | 2018-02-28 13:40:08 +0100 | [diff] [blame^] | 123 | if (!streams.empty() && streams[0].num_temporal_layers == 2) { |
| 124 | video_codec.targetBitrate = streams[0].target_bitrate_bps / 1000; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 125 | } |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | if (config.encoder_specific_settings) |
| 130 | config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec); |
| 131 | |
| 132 | switch (video_codec.codecType) { |
| 133 | case kVideoCodecVP8: { |
Sergey Silkin | d2ed0a4 | 2018-02-28 13:40:08 +0100 | [diff] [blame^] | 134 | if (!config.encoder_specific_settings) { |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 135 | *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings(); |
Sergey Silkin | d2ed0a4 | 2018-02-28 13:40:08 +0100 | [diff] [blame^] | 136 | } |
emircan | bbcc356 | 2017-08-18 00:28:40 -0700 | [diff] [blame] | 137 | |
Sergey Silkin | d2ed0a4 | 2018-02-28 13:40:08 +0100 | [diff] [blame^] | 138 | video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>( |
| 139 | streams.back().num_temporal_layers.value_or( |
| 140 | video_codec.VP8()->numberOfTemporalLayers)); |
| 141 | RTC_DCHECK_GE(video_codec.VP8()->numberOfTemporalLayers, 1); |
| 142 | |
| 143 | if (nack_enabled && video_codec.VP8()->numberOfTemporalLayers == 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 144 | RTC_LOG(LS_INFO) |
| 145 | << "No temporal layers and nack enabled -> resilience off"; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 146 | video_codec.VP8()->resilience = kResilienceOff; |
| 147 | } |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | case kVideoCodecVP9: { |
Sergey Silkin | d2ed0a4 | 2018-02-28 13:40:08 +0100 | [diff] [blame^] | 151 | if (!config.encoder_specific_settings) { |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 152 | *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings(); |
Sergey Silkin | d2ed0a4 | 2018-02-28 13:40:08 +0100 | [diff] [blame^] | 153 | } |
| 154 | |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 155 | if (video_codec.mode == kScreensharing && |
| 156 | config.encoder_specific_settings) { |
| 157 | video_codec.VP9()->flexibleMode = true; |
| 158 | // For now VP9 screensharing use 1 temporal and 2 spatial layers. |
| 159 | RTC_DCHECK_EQ(1, video_codec.VP9()->numberOfTemporalLayers); |
| 160 | RTC_DCHECK_EQ(2, video_codec.VP9()->numberOfSpatialLayers); |
| 161 | } |
emircan | bbcc356 | 2017-08-18 00:28:40 -0700 | [diff] [blame] | 162 | |
Sergey Silkin | d2ed0a4 | 2018-02-28 13:40:08 +0100 | [diff] [blame^] | 163 | video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>( |
| 164 | streams.back().num_temporal_layers.value_or( |
| 165 | video_codec.VP9()->numberOfTemporalLayers)); |
| 166 | RTC_DCHECK_GE(video_codec.VP9()->numberOfTemporalLayers, 1); |
| 167 | |
| 168 | if (nack_enabled && video_codec.VP9()->numberOfTemporalLayers == 1 && |
emircan | bbcc356 | 2017-08-18 00:28:40 -0700 | [diff] [blame] | 169 | video_codec.VP9()->numberOfSpatialLayers == 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 170 | RTC_LOG(LS_INFO) << "No temporal or spatial layers and nack enabled -> " |
| 171 | << "resilience off"; |
emircan | bbcc356 | 2017-08-18 00:28:40 -0700 | [diff] [blame] | 172 | video_codec.VP9()->resilienceOn = false; |
| 173 | } |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | case kVideoCodecH264: { |
| 177 | if (!config.encoder_specific_settings) |
| 178 | *video_codec.H264() = VideoEncoder::GetDefaultH264Settings(); |
| 179 | break; |
| 180 | } |
| 181 | default: |
| 182 | // TODO(pbos): Support encoder_settings codec-agnostically. |
| 183 | RTC_DCHECK(!config.encoder_specific_settings) |
| 184 | << "Encoder-specific settings for codec type not wired up."; |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | strncpy(video_codec.plName, payload_name.c_str(), kPayloadNameSize - 1); |
| 189 | video_codec.plName[kPayloadNameSize - 1] = '\0'; |
| 190 | video_codec.plType = payload_type; |
| 191 | video_codec.numberOfSimulcastStreams = |
| 192 | static_cast<unsigned char>(streams.size()); |
| 193 | video_codec.minBitrate = streams[0].min_bitrate_bps / 1000; |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 194 | bool codec_active = false; |
| 195 | for (const VideoStream& stream : streams) { |
| 196 | if (stream.active) { |
| 197 | codec_active = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | // Set active for the entire video codec for the non simulcast case. |
| 202 | video_codec.active = codec_active; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 203 | if (video_codec.minBitrate < kEncoderMinBitrateKbps) |
| 204 | video_codec.minBitrate = kEncoderMinBitrateKbps; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 205 | video_codec.timing_frame_thresholds = {kDefaultTimingFramesDelayMs, |
| 206 | kDefaultOutlierFrameSizePercent}; |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 207 | RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 208 | if (video_codec.codecType == kVideoCodecVP9) { |
| 209 | // If the vector is empty, bitrates will be configured automatically. |
| 210 | RTC_DCHECK(config.spatial_layers.empty() || |
| 211 | config.spatial_layers.size() == |
| 212 | video_codec.VP9()->numberOfSpatialLayers); |
| 213 | RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers, |
| 214 | kMaxSimulcastStreams); |
| 215 | for (size_t i = 0; i < config.spatial_layers.size(); ++i) |
| 216 | video_codec.spatialLayers[i] = config.spatial_layers[i]; |
| 217 | } |
| 218 | for (size_t i = 0; i < streams.size(); ++i) { |
| 219 | SimulcastStream* sim_stream = &video_codec.simulcastStream[i]; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 220 | RTC_DCHECK_GT(streams[i].width, 0); |
| 221 | RTC_DCHECK_GT(streams[i].height, 0); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 222 | RTC_DCHECK_GT(streams[i].max_framerate, 0); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 223 | // Different framerates not supported per stream at the moment, unless it's |
| 224 | // screenshare where there is an exception and a simulcast encoder adapter, |
| 225 | // which supports different framerates, is used instead. |
| 226 | if (config.content_type != VideoEncoderConfig::ContentType::kScreen) { |
| 227 | RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate); |
| 228 | } |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 229 | RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0); |
| 230 | RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps); |
| 231 | RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps); |
| 232 | RTC_DCHECK_GE(streams[i].max_qp, 0); |
| 233 | |
| 234 | sim_stream->width = static_cast<uint16_t>(streams[i].width); |
| 235 | sim_stream->height = static_cast<uint16_t>(streams[i].height); |
| 236 | sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000; |
| 237 | sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000; |
| 238 | sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000; |
| 239 | sim_stream->qpMax = streams[i].max_qp; |
Sergey Silkin | d2ed0a4 | 2018-02-28 13:40:08 +0100 | [diff] [blame^] | 240 | sim_stream->numberOfTemporalLayers = |
| 241 | static_cast<unsigned char>(streams[i].num_temporal_layers.value_or(1)); |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 242 | sim_stream->active = streams[i].active; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 243 | |
| 244 | video_codec.width = |
| 245 | std::max(video_codec.width, static_cast<uint16_t>(streams[i].width)); |
| 246 | video_codec.height = |
| 247 | std::max(video_codec.height, static_cast<uint16_t>(streams[i].height)); |
| 248 | video_codec.minBitrate = |
| 249 | std::min(static_cast<uint16_t>(video_codec.minBitrate), |
| 250 | static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000)); |
| 251 | video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000; |
| 252 | video_codec.qpMax = std::max(video_codec.qpMax, |
| 253 | static_cast<unsigned int>(streams[i].max_qp)); |
| 254 | } |
| 255 | |
| 256 | if (video_codec.maxBitrate == 0) { |
| 257 | // Unset max bitrate -> cap to one bit per pixel. |
| 258 | video_codec.maxBitrate = |
| 259 | (video_codec.width * video_codec.height * video_codec.maxFramerate) / |
| 260 | 1000; |
| 261 | } |
| 262 | if (video_codec.maxBitrate < kEncoderMinBitrateKbps) |
| 263 | video_codec.maxBitrate = kEncoderMinBitrateKbps; |
| 264 | |
| 265 | RTC_DCHECK_GT(streams[0].max_framerate, 0); |
| 266 | video_codec.maxFramerate = streams[0].max_framerate; |
| 267 | return video_codec; |
| 268 | } |
| 269 | |
| 270 | } // namespace webrtc |