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