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