blob: 892113b1008c2308dc5d1772cd3cb305cf595ca2 [file] [log] [blame]
Erik Språng08127a92016-11-16 16:41:30 +01001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/include/video_codec_initializer.h"
Erik Språng08127a92016-11-16 16:41:30 +010012
Anders Carlssondd8c1652018-01-30 10:32:13 +010013#include "api/video_codecs/video_encoder.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020014#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#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ång08127a92016-11-16 16:41:30 +010024
25namespace webrtc {
26
27bool VideoCodecInitializer::SetupCodec(
28 const VideoEncoderConfig& config,
Erik Språng08127a92016-11-16 16:41:30 +010029 const std::vector<VideoStream>& streams,
asapersson5f7226f2016-11-25 04:37:00 -080030 bool nack_enabled,
Erik Språng08127a92016-11-16 16:41:30 +010031 VideoCodec* codec,
32 std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator) {
Niels Möllerbc900cb2018-03-21 12:50:22 +010033 if (config.codec_type == kVideoCodecMultiplex) {
34 VideoEncoderConfig associated_config = config.Copy();
35 associated_config.codec_type = kVideoCodecVP9;
36 if (!SetupCodec(associated_config, streams, nack_enabled, codec,
37 bitrate_allocator)) {
Emircan Uysaler0a375472017-12-11 12:21:02 +053038 RTC_LOG(LS_ERROR) << "Failed to create stereo encoder configuration.";
39 return false;
40 }
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080041 codec->codecType = kVideoCodecMultiplex;
Emircan Uysaler0a375472017-12-11 12:21:02 +053042 return true;
43 }
44
asapersson5f7226f2016-11-25 04:37:00 -080045 *codec =
Niels Möllerbc900cb2018-03-21 12:50:22 +010046 VideoEncoderConfigToVideoCodec(config, streams, nack_enabled);
Erik Språng82fad3d2018-03-21 09:57:23 +010047 *bitrate_allocator = CreateBitrateAllocator(*codec);
Erik Språng08127a92016-11-16 16:41:30 +010048
49 return true;
50}
51
52std::unique_ptr<VideoBitrateAllocator>
Erik Språng82fad3d2018-03-21 09:57:23 +010053VideoCodecInitializer::CreateBitrateAllocator(const VideoCodec& codec) {
Erik Språng08127a92016-11-16 16:41:30 +010054 std::unique_ptr<VideoBitrateAllocator> rate_allocator;
55
56 switch (codec.codecType) {
57 case kVideoCodecVP8: {
58 // Set up default VP8 temporal layer factory, if not provided.
Erik Språng82fad3d2018-03-21 09:57:23 +010059 rate_allocator.reset(new SimulcastRateAllocator(codec));
Erik Språng08127a92016-11-16 16:41:30 +010060 } break;
61 default:
62 rate_allocator.reset(new DefaultVideoBitrateAllocator(codec));
63 }
64
65 return rate_allocator;
66}
67
68// TODO(sprang): Split this up and separate the codec specific parts.
69VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
70 const VideoEncoderConfig& config,
71 const std::vector<VideoStream>& streams,
asapersson5f7226f2016-11-25 04:37:00 -080072 bool nack_enabled) {
Erik Språng08127a92016-11-16 16:41:30 +010073 static const int kEncoderMinBitrateKbps = 30;
74 RTC_DCHECK(!streams.empty());
75 RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0);
76
77 VideoCodec video_codec;
78 memset(&video_codec, 0, sizeof(video_codec));
Niels Möllerbc900cb2018-03-21 12:50:22 +010079 video_codec.codecType = config.codec_type;
Erik Språng08127a92016-11-16 16:41:30 +010080
81 switch (config.content_type) {
82 case VideoEncoderConfig::ContentType::kRealtimeVideo:
83 video_codec.mode = kRealtimeVideo;
84 break;
85 case VideoEncoderConfig::ContentType::kScreen:
86 video_codec.mode = kScreensharing;
Sergey Silkina796a7e2018-03-01 15:11:29 +010087 if (!streams.empty() && streams[0].num_temporal_layers == 2) {
88 video_codec.targetBitrate = streams[0].target_bitrate_bps / 1000;
Erik Språng08127a92016-11-16 16:41:30 +010089 }
90 break;
91 }
92
93 if (config.encoder_specific_settings)
94 config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec);
95
96 switch (video_codec.codecType) {
97 case kVideoCodecVP8: {
Sergey Silkina796a7e2018-03-01 15:11:29 +010098 if (!config.encoder_specific_settings) {
Erik Språng08127a92016-11-16 16:41:30 +010099 *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings();
Sergey Silkina796a7e2018-03-01 15:11:29 +0100100 }
Sergey Silkind2ed0a42018-02-28 13:40:08 +0100101
Sergey Silkina796a7e2018-03-01 15:11:29 +0100102 video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>(
103 streams.back().num_temporal_layers.value_or(
104 video_codec.VP8()->numberOfTemporalLayers));
105 RTC_DCHECK_GE(video_codec.VP8()->numberOfTemporalLayers, 1);
106
107 if (nack_enabled && video_codec.VP8()->numberOfTemporalLayers == 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100108 RTC_LOG(LS_INFO)
109 << "No temporal layers and nack enabled -> resilience off";
asapersson5f7226f2016-11-25 04:37:00 -0800110 video_codec.VP8()->resilience = kResilienceOff;
111 }
Erik Språng08127a92016-11-16 16:41:30 +0100112 break;
113 }
114 case kVideoCodecVP9: {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100115 if (!config.encoder_specific_settings) {
Erik Språng08127a92016-11-16 16:41:30 +0100116 *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings();
Sergey Silkina796a7e2018-03-01 15:11:29 +0100117 }
118
Erik Språng08127a92016-11-16 16:41:30 +0100119 if (video_codec.mode == kScreensharing &&
120 config.encoder_specific_settings) {
121 video_codec.VP9()->flexibleMode = true;
122 // For now VP9 screensharing use 1 temporal and 2 spatial layers.
123 RTC_DCHECK_EQ(1, video_codec.VP9()->numberOfTemporalLayers);
124 RTC_DCHECK_EQ(2, video_codec.VP9()->numberOfSpatialLayers);
125 }
Sergey Silkind2ed0a42018-02-28 13:40:08 +0100126
Sergey Silkina796a7e2018-03-01 15:11:29 +0100127 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
128 streams.back().num_temporal_layers.value_or(
129 video_codec.VP9()->numberOfTemporalLayers));
130 RTC_DCHECK_GE(video_codec.VP9()->numberOfTemporalLayers, 1);
131
132 if (nack_enabled && video_codec.VP9()->numberOfTemporalLayers == 1 &&
emircanbbcc3562017-08-18 00:28:40 -0700133 video_codec.VP9()->numberOfSpatialLayers == 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100134 RTC_LOG(LS_INFO) << "No temporal or spatial layers and nack enabled -> "
135 << "resilience off";
emircanbbcc3562017-08-18 00:28:40 -0700136 video_codec.VP9()->resilienceOn = false;
137 }
Erik Språng08127a92016-11-16 16:41:30 +0100138 break;
139 }
140 case kVideoCodecH264: {
141 if (!config.encoder_specific_settings)
142 *video_codec.H264() = VideoEncoder::GetDefaultH264Settings();
143 break;
144 }
145 default:
146 // TODO(pbos): Support encoder_settings codec-agnostically.
147 RTC_DCHECK(!config.encoder_specific_settings)
148 << "Encoder-specific settings for codec type not wired up.";
149 break;
150 }
151
Niels Möllerbc900cb2018-03-21 12:50:22 +0100152 // TODO(nisse): The plType field should be deleted. Luckily, our
153 // callers don't need it.
154 video_codec.plType = 0;
Erik Språng08127a92016-11-16 16:41:30 +0100155 video_codec.numberOfSimulcastStreams =
156 static_cast<unsigned char>(streams.size());
157 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800158 bool codec_active = false;
159 for (const VideoStream& stream : streams) {
160 if (stream.active) {
161 codec_active = true;
162 break;
163 }
164 }
165 // Set active for the entire video codec for the non simulcast case.
166 video_codec.active = codec_active;
Erik Språng08127a92016-11-16 16:41:30 +0100167 if (video_codec.minBitrate < kEncoderMinBitrateKbps)
168 video_codec.minBitrate = kEncoderMinBitrateKbps;
ilnik04f4d122017-06-19 07:18:55 -0700169 video_codec.timing_frame_thresholds = {kDefaultTimingFramesDelayMs,
170 kDefaultOutlierFrameSizePercent};
kwiberg352444f2016-11-28 15:58:53 -0800171 RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams);
Erik Språng08127a92016-11-16 16:41:30 +0100172 if (video_codec.codecType == kVideoCodecVP9) {
173 // If the vector is empty, bitrates will be configured automatically.
174 RTC_DCHECK(config.spatial_layers.empty() ||
175 config.spatial_layers.size() ==
176 video_codec.VP9()->numberOfSpatialLayers);
177 RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers,
178 kMaxSimulcastStreams);
179 for (size_t i = 0; i < config.spatial_layers.size(); ++i)
180 video_codec.spatialLayers[i] = config.spatial_layers[i];
181 }
182 for (size_t i = 0; i < streams.size(); ++i) {
183 SimulcastStream* sim_stream = &video_codec.simulcastStream[i];
kwibergaf476c72016-11-28 15:21:39 -0800184 RTC_DCHECK_GT(streams[i].width, 0);
185 RTC_DCHECK_GT(streams[i].height, 0);
Erik Språng08127a92016-11-16 16:41:30 +0100186 RTC_DCHECK_GT(streams[i].max_framerate, 0);
sprang429600d2017-01-26 06:12:26 -0800187 // Different framerates not supported per stream at the moment, unless it's
188 // screenshare where there is an exception and a simulcast encoder adapter,
189 // which supports different framerates, is used instead.
190 if (config.content_type != VideoEncoderConfig::ContentType::kScreen) {
191 RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate);
192 }
Erik Språng08127a92016-11-16 16:41:30 +0100193 RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0);
194 RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps);
195 RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps);
196 RTC_DCHECK_GE(streams[i].max_qp, 0);
197
198 sim_stream->width = static_cast<uint16_t>(streams[i].width);
199 sim_stream->height = static_cast<uint16_t>(streams[i].height);
200 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000;
201 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000;
202 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000;
203 sim_stream->qpMax = streams[i].max_qp;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100204 sim_stream->numberOfTemporalLayers =
205 static_cast<unsigned char>(streams[i].num_temporal_layers.value_or(1));
Seth Hampson46e31ba2018-01-18 10:39:54 -0800206 sim_stream->active = streams[i].active;
Erik Språng08127a92016-11-16 16:41:30 +0100207
208 video_codec.width =
209 std::max(video_codec.width, static_cast<uint16_t>(streams[i].width));
210 video_codec.height =
211 std::max(video_codec.height, static_cast<uint16_t>(streams[i].height));
212 video_codec.minBitrate =
213 std::min(static_cast<uint16_t>(video_codec.minBitrate),
214 static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000));
215 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000;
216 video_codec.qpMax = std::max(video_codec.qpMax,
217 static_cast<unsigned int>(streams[i].max_qp));
218 }
219
220 if (video_codec.maxBitrate == 0) {
221 // Unset max bitrate -> cap to one bit per pixel.
222 video_codec.maxBitrate =
223 (video_codec.width * video_codec.height * video_codec.maxFramerate) /
224 1000;
225 }
226 if (video_codec.maxBitrate < kEncoderMinBitrateKbps)
227 video_codec.maxBitrate = kEncoderMinBitrateKbps;
228
229 RTC_DCHECK_GT(streams[0].max_framerate, 0);
230 video_codec.maxFramerate = streams[0].max_framerate;
231 return video_codec;
232}
233
234} // namespace webrtc