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