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 | |
| 11 | #include "webrtc/modules/video_coding/include/video_codec_initializer.h" |
| 12 | |
| 13 | #include "webrtc/base/basictypes.h" |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 14 | #include "webrtc/base/logging.h" |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 15 | #include "webrtc/common_video/include/video_bitrate_allocator.h" |
| 16 | #include "webrtc/common_types.h" |
| 17 | #include "webrtc/modules/video_coding/codecs/vp8/screenshare_layers.h" |
| 18 | #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" |
| 19 | #include "webrtc/modules/video_coding/utility/simulcast_rate_allocator.h" |
| 20 | #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h" |
| 21 | #include "webrtc/system_wrappers/include/clock.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | bool VideoCodecInitializer::SetupCodec( |
| 26 | const VideoEncoderConfig& config, |
| 27 | const VideoSendStream::Config::EncoderSettings settings, |
| 28 | const std::vector<VideoStream>& streams, |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 29 | bool nack_enabled, |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 30 | VideoCodec* codec, |
| 31 | std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator) { |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 32 | *codec = |
| 33 | VideoEncoderConfigToVideoCodec(config, streams, settings.payload_name, |
| 34 | settings.payload_type, nack_enabled); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 35 | |
| 36 | std::unique_ptr<TemporalLayersFactory> tl_factory; |
| 37 | switch (codec->codecType) { |
| 38 | case kVideoCodecVP8: { |
| 39 | if (!codec->VP8()->tl_factory) { |
| 40 | if (codec->mode == kScreensharing && |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 41 | (codec->numberOfSimulcastStreams > 1 || |
| 42 | (codec->numberOfSimulcastStreams == 1 && |
| 43 | codec->VP8()->numberOfTemporalLayers == 2))) { |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 44 | // Conference mode temporal layering for screen content. |
| 45 | tl_factory.reset(new ScreenshareTemporalLayersFactory()); |
| 46 | } else { |
| 47 | // Standard video temporal layers. |
| 48 | tl_factory.reset(new TemporalLayersFactory()); |
| 49 | } |
| 50 | codec->VP8()->tl_factory = tl_factory.get(); |
| 51 | } |
| 52 | break; |
| 53 | } |
| 54 | default: { |
| 55 | // TODO(sprang): Warn, once we have specific allocators for all supported |
| 56 | // codec types. |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | *bitrate_allocator = CreateBitrateAllocator(*codec, std::move(tl_factory)); |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<VideoBitrateAllocator> |
| 66 | VideoCodecInitializer::CreateBitrateAllocator( |
| 67 | const VideoCodec& codec, |
| 68 | std::unique_ptr<TemporalLayersFactory> tl_factory) { |
| 69 | std::unique_ptr<VideoBitrateAllocator> rate_allocator; |
| 70 | |
| 71 | switch (codec.codecType) { |
| 72 | case kVideoCodecVP8: { |
| 73 | // Set up default VP8 temporal layer factory, if not provided. |
| 74 | rate_allocator.reset( |
| 75 | new SimulcastRateAllocator(codec, std::move(tl_factory))); |
| 76 | } break; |
| 77 | default: |
| 78 | rate_allocator.reset(new DefaultVideoBitrateAllocator(codec)); |
| 79 | } |
| 80 | |
| 81 | return rate_allocator; |
| 82 | } |
| 83 | |
| 84 | // TODO(sprang): Split this up and separate the codec specific parts. |
| 85 | VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec( |
| 86 | const VideoEncoderConfig& config, |
| 87 | const std::vector<VideoStream>& streams, |
| 88 | const std::string& payload_name, |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 89 | int payload_type, |
| 90 | bool nack_enabled) { |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 91 | static const int kEncoderMinBitrateKbps = 30; |
| 92 | RTC_DCHECK(!streams.empty()); |
| 93 | RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0); |
| 94 | |
| 95 | VideoCodec video_codec; |
| 96 | memset(&video_codec, 0, sizeof(video_codec)); |
| 97 | video_codec.codecType = PayloadNameToCodecType(payload_name) |
| 98 | .value_or(VideoCodecType::kVideoCodecGeneric); |
| 99 | |
| 100 | switch (config.content_type) { |
| 101 | case VideoEncoderConfig::ContentType::kRealtimeVideo: |
| 102 | video_codec.mode = kRealtimeVideo; |
| 103 | break; |
| 104 | case VideoEncoderConfig::ContentType::kScreen: |
| 105 | video_codec.mode = kScreensharing; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 106 | if (streams.size() >= 1 && |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 107 | streams[0].temporal_layer_thresholds_bps.size() == 1) { |
| 108 | video_codec.targetBitrate = |
| 109 | streams[0].temporal_layer_thresholds_bps[0] / 1000; |
| 110 | } |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | if (config.encoder_specific_settings) |
| 115 | config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec); |
| 116 | |
| 117 | switch (video_codec.codecType) { |
| 118 | case kVideoCodecVP8: { |
| 119 | if (!config.encoder_specific_settings) |
| 120 | *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings(); |
| 121 | video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>( |
| 122 | streams.back().temporal_layer_thresholds_bps.size() + 1); |
asapersson | a90799d | 2016-12-09 02:35:20 -0800 | [diff] [blame] | 123 | bool temporal_layers_configured = false; |
| 124 | for (const VideoStream& stream : streams) { |
| 125 | if (stream.temporal_layer_thresholds_bps.size() > 0) |
| 126 | temporal_layers_configured = true; |
| 127 | } |
| 128 | if (nack_enabled && !temporal_layers_configured) { |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 129 | LOG(LS_INFO) << "No temporal layers and nack enabled -> resilience off"; |
| 130 | video_codec.VP8()->resilience = kResilienceOff; |
| 131 | } |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | case kVideoCodecVP9: { |
| 135 | if (!config.encoder_specific_settings) |
| 136 | *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings(); |
| 137 | if (video_codec.mode == kScreensharing && |
| 138 | config.encoder_specific_settings) { |
| 139 | video_codec.VP9()->flexibleMode = true; |
| 140 | // For now VP9 screensharing use 1 temporal and 2 spatial layers. |
| 141 | RTC_DCHECK_EQ(1, video_codec.VP9()->numberOfTemporalLayers); |
| 142 | RTC_DCHECK_EQ(2, video_codec.VP9()->numberOfSpatialLayers); |
| 143 | } |
| 144 | video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>( |
| 145 | streams.back().temporal_layer_thresholds_bps.size() + 1); |
| 146 | break; |
| 147 | } |
| 148 | case kVideoCodecH264: { |
| 149 | if (!config.encoder_specific_settings) |
| 150 | *video_codec.H264() = VideoEncoder::GetDefaultH264Settings(); |
| 151 | break; |
| 152 | } |
| 153 | default: |
| 154 | // TODO(pbos): Support encoder_settings codec-agnostically. |
| 155 | RTC_DCHECK(!config.encoder_specific_settings) |
| 156 | << "Encoder-specific settings for codec type not wired up."; |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | strncpy(video_codec.plName, payload_name.c_str(), kPayloadNameSize - 1); |
| 161 | video_codec.plName[kPayloadNameSize - 1] = '\0'; |
| 162 | video_codec.plType = payload_type; |
| 163 | video_codec.numberOfSimulcastStreams = |
| 164 | static_cast<unsigned char>(streams.size()); |
| 165 | video_codec.minBitrate = streams[0].min_bitrate_bps / 1000; |
| 166 | if (video_codec.minBitrate < kEncoderMinBitrateKbps) |
| 167 | video_codec.minBitrate = kEncoderMinBitrateKbps; |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 168 | RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 169 | if (video_codec.codecType == kVideoCodecVP9) { |
| 170 | // If the vector is empty, bitrates will be configured automatically. |
| 171 | RTC_DCHECK(config.spatial_layers.empty() || |
| 172 | config.spatial_layers.size() == |
| 173 | video_codec.VP9()->numberOfSpatialLayers); |
| 174 | RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers, |
| 175 | kMaxSimulcastStreams); |
| 176 | for (size_t i = 0; i < config.spatial_layers.size(); ++i) |
| 177 | video_codec.spatialLayers[i] = config.spatial_layers[i]; |
| 178 | } |
| 179 | for (size_t i = 0; i < streams.size(); ++i) { |
| 180 | SimulcastStream* sim_stream = &video_codec.simulcastStream[i]; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 181 | RTC_DCHECK_GT(streams[i].width, 0); |
| 182 | RTC_DCHECK_GT(streams[i].height, 0); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 183 | RTC_DCHECK_GT(streams[i].max_framerate, 0); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 184 | // Different framerates not supported per stream at the moment, unless it's |
| 185 | // screenshare where there is an exception and a simulcast encoder adapter, |
| 186 | // which supports different framerates, is used instead. |
| 187 | if (config.content_type != VideoEncoderConfig::ContentType::kScreen) { |
| 188 | RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate); |
| 189 | } |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 190 | RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0); |
| 191 | RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps); |
| 192 | RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps); |
| 193 | RTC_DCHECK_GE(streams[i].max_qp, 0); |
| 194 | |
| 195 | sim_stream->width = static_cast<uint16_t>(streams[i].width); |
| 196 | sim_stream->height = static_cast<uint16_t>(streams[i].height); |
| 197 | sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000; |
| 198 | sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000; |
| 199 | sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000; |
| 200 | sim_stream->qpMax = streams[i].max_qp; |
| 201 | sim_stream->numberOfTemporalLayers = static_cast<unsigned char>( |
| 202 | streams[i].temporal_layer_thresholds_bps.size() + 1); |
| 203 | |
| 204 | video_codec.width = |
| 205 | std::max(video_codec.width, static_cast<uint16_t>(streams[i].width)); |
| 206 | video_codec.height = |
| 207 | std::max(video_codec.height, static_cast<uint16_t>(streams[i].height)); |
| 208 | video_codec.minBitrate = |
| 209 | std::min(static_cast<uint16_t>(video_codec.minBitrate), |
| 210 | static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000)); |
| 211 | video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000; |
| 212 | video_codec.qpMax = std::max(video_codec.qpMax, |
| 213 | static_cast<unsigned int>(streams[i].max_qp)); |
| 214 | } |
| 215 | |
| 216 | if (video_codec.maxBitrate == 0) { |
| 217 | // Unset max bitrate -> cap to one bit per pixel. |
| 218 | video_codec.maxBitrate = |
| 219 | (video_codec.width * video_codec.height * video_codec.maxFramerate) / |
| 220 | 1000; |
| 221 | } |
| 222 | if (video_codec.maxBitrate < kEncoderMinBitrateKbps) |
| 223 | video_codec.maxBitrate = kEncoderMinBitrateKbps; |
| 224 | |
| 225 | RTC_DCHECK_GT(streams[0].max_framerate, 0); |
| 226 | video_codec.maxFramerate = streams[0].max_framerate; |
| 227 | return video_codec; |
| 228 | } |
| 229 | |
| 230 | } // namespace webrtc |