blob: b676a95e4f119072609a01fb1824510e02e4f0ec [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
Jiawei Ou4206a0a2018-07-20 15:49:43 -070013#include "api/video/video_bitrate_allocator.h"
Anders Carlssondd8c1652018-01-30 10:32:13 +010014#include "api/video_codecs/video_encoder.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020015#include "common_types.h" // NOLINT(build/include)
Sergey Silkin86684962018-03-28 19:32:37 +020016#include "modules/video_coding/codecs/vp9/svc_config.h"
17#include "modules/video_coding/codecs/vp9/svc_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/include/video_coding_defines.h"
19#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020020#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/logging.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020022#include "rtc_base/system/fallthrough.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "system_wrappers/include/clock.h"
Erik Språng08127a92016-11-16 16:41:30 +010024
25namespace webrtc {
26
Qingsi Wang59844ce2018-11-01 04:45:53 +000027bool VideoCodecInitializer::SetupCodec(
28 const VideoEncoderConfig& config,
29 const std::vector<VideoStream>& streams,
30 VideoCodec* codec,
31 std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator) {
Niels Möller259a4972018-04-05 15:36:51 +020032 if (config.codec_type == kVideoCodecMultiplex) {
Niels Möller24a842a2018-03-22 08:52:50 +010033 VideoEncoderConfig associated_config = config.Copy();
34 associated_config.codec_type = kVideoCodecVP9;
Qingsi Wang59844ce2018-11-01 04:45:53 +000035 if (!SetupCodec(associated_config, streams, codec, bitrate_allocator)) {
Emircan Uysaler0a375472017-12-11 12:21:02 +053036 RTC_LOG(LS_ERROR) << "Failed to create stereo encoder configuration.";
37 return false;
38 }
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080039 codec->codecType = kVideoCodecMultiplex;
Emircan Uysaler0a375472017-12-11 12:21:02 +053040 return true;
41 }
42
Yves Gerey665174f2018-06-19 15:03:05 +020043 *codec = VideoEncoderConfigToVideoCodec(config, streams);
Oleh Prypin2a742632018-11-08 12:01:17 +010044 if (bitrate_allocator) {
45 *bitrate_allocator = CreateBitrateAllocator(*codec);
46 }
Qingsi Wang59844ce2018-11-01 04:45:53 +000047
Erik Språng08127a92016-11-16 16:41:30 +010048 return true;
49}
50
Qingsi Wang59844ce2018-11-01 04:45:53 +000051std::unique_ptr<VideoBitrateAllocator>
52VideoCodecInitializer::CreateBitrateAllocator(const VideoCodec& codec) {
53 std::unique_ptr<VideoBitrateAllocator> rate_allocator;
54
55 switch (codec.codecType) {
56 case kVideoCodecVP8:
57 RTC_FALLTHROUGH();
58 case kVideoCodecH264:
59 rate_allocator.reset(new SimulcastRateAllocator(codec));
60 break;
61 case kVideoCodecVP9:
62 rate_allocator.reset(new SvcRateAllocator(codec));
63 break;
64 default:
65 rate_allocator.reset(new DefaultVideoBitrateAllocator(codec));
66 }
67
68 return rate_allocator;
69}
70
Erik Språng08127a92016-11-16 16:41:30 +010071// TODO(sprang): Split this up and separate the codec specific parts.
72VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
73 const VideoEncoderConfig& config,
Niels Möllerf1338562018-04-26 09:51:47 +020074 const std::vector<VideoStream>& streams) {
Erik Språng08127a92016-11-16 16:41:30 +010075 static const int kEncoderMinBitrateKbps = 30;
76 RTC_DCHECK(!streams.empty());
77 RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0);
78
79 VideoCodec video_codec;
80 memset(&video_codec, 0, sizeof(video_codec));
Niels Möller259a4972018-04-05 15:36:51 +020081 video_codec.codecType = config.codec_type;
Erik Språng08127a92016-11-16 16:41:30 +010082
83 switch (config.content_type) {
84 case VideoEncoderConfig::ContentType::kRealtimeVideo:
Niels Möllere3cf3d02018-06-13 11:52:16 +020085 video_codec.mode = VideoCodecMode::kRealtimeVideo;
Erik Språng08127a92016-11-16 16:41:30 +010086 break;
87 case VideoEncoderConfig::ContentType::kScreen:
Niels Möllere3cf3d02018-06-13 11:52:16 +020088 video_codec.mode = VideoCodecMode::kScreensharing;
Erik Språng08127a92016-11-16 16:41:30 +010089 break;
90 }
91
Niels Möller24a842a2018-03-22 08:52:50 +010092 // TODO(nisse): The plType field should be deleted. Luckily, our
93 // callers don't need it.
94 video_codec.plType = 0;
Erik Språng08127a92016-11-16 16:41:30 +010095 video_codec.numberOfSimulcastStreams =
96 static_cast<unsigned char>(streams.size());
97 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000;
Seth Hampson46e31ba2018-01-18 10:39:54 -080098 bool codec_active = false;
99 for (const VideoStream& stream : streams) {
100 if (stream.active) {
101 codec_active = true;
102 break;
103 }
104 }
105 // Set active for the entire video codec for the non simulcast case.
106 video_codec.active = codec_active;
Erik Språng08127a92016-11-16 16:41:30 +0100107 if (video_codec.minBitrate < kEncoderMinBitrateKbps)
108 video_codec.minBitrate = kEncoderMinBitrateKbps;
ilnik04f4d122017-06-19 07:18:55 -0700109 video_codec.timing_frame_thresholds = {kDefaultTimingFramesDelayMs,
110 kDefaultOutlierFrameSizePercent};
kwiberg352444f2016-11-28 15:58:53 -0800111 RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams);
Sergey Silkin86684962018-03-28 19:32:37 +0200112
Erik Språng08127a92016-11-16 16:41:30 +0100113 for (size_t i = 0; i < streams.size(); ++i) {
114 SimulcastStream* sim_stream = &video_codec.simulcastStream[i];
kwibergaf476c72016-11-28 15:21:39 -0800115 RTC_DCHECK_GT(streams[i].width, 0);
116 RTC_DCHECK_GT(streams[i].height, 0);
Erik Språng08127a92016-11-16 16:41:30 +0100117 RTC_DCHECK_GT(streams[i].max_framerate, 0);
sprang429600d2017-01-26 06:12:26 -0800118 // Different framerates not supported per stream at the moment, unless it's
119 // screenshare where there is an exception and a simulcast encoder adapter,
120 // which supports different framerates, is used instead.
121 if (config.content_type != VideoEncoderConfig::ContentType::kScreen) {
122 RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate);
123 }
Erik Språng08127a92016-11-16 16:41:30 +0100124 RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0);
125 RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps);
126 RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps);
127 RTC_DCHECK_GE(streams[i].max_qp, 0);
128
129 sim_stream->width = static_cast<uint16_t>(streams[i].width);
130 sim_stream->height = static_cast<uint16_t>(streams[i].height);
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200131 sim_stream->maxFramerate = streams[i].max_framerate;
Erik Språng08127a92016-11-16 16:41:30 +0100132 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000;
133 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000;
134 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000;
135 sim_stream->qpMax = streams[i].max_qp;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100136 sim_stream->numberOfTemporalLayers =
137 static_cast<unsigned char>(streams[i].num_temporal_layers.value_or(1));
Seth Hampson46e31ba2018-01-18 10:39:54 -0800138 sim_stream->active = streams[i].active;
Erik Språng08127a92016-11-16 16:41:30 +0100139
140 video_codec.width =
Danil Chapovalov350531e2018-06-08 11:04:04 +0000141 std::max(video_codec.width, static_cast<uint16_t>(streams[i].width));
Erik Språng08127a92016-11-16 16:41:30 +0100142 video_codec.height =
Danil Chapovalov350531e2018-06-08 11:04:04 +0000143 std::max(video_codec.height, static_cast<uint16_t>(streams[i].height));
Erik Språng08127a92016-11-16 16:41:30 +0100144 video_codec.minBitrate =
145 std::min(static_cast<uint16_t>(video_codec.minBitrate),
146 static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000));
147 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000;
148 video_codec.qpMax = std::max(video_codec.qpMax,
149 static_cast<unsigned int>(streams[i].max_qp));
150 }
151
152 if (video_codec.maxBitrate == 0) {
153 // Unset max bitrate -> cap to one bit per pixel.
154 video_codec.maxBitrate =
155 (video_codec.width * video_codec.height * video_codec.maxFramerate) /
156 1000;
157 }
158 if (video_codec.maxBitrate < kEncoderMinBitrateKbps)
159 video_codec.maxBitrate = kEncoderMinBitrateKbps;
160
161 RTC_DCHECK_GT(streams[0].max_framerate, 0);
162 video_codec.maxFramerate = streams[0].max_framerate;
Sergey Silkin86684962018-03-28 19:32:37 +0200163
164 // Set codec specific options
165 if (config.encoder_specific_settings)
166 config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec);
167
168 switch (video_codec.codecType) {
169 case kVideoCodecVP8: {
170 if (!config.encoder_specific_settings) {
171 *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings();
172 }
173
174 video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>(
175 streams.back().num_temporal_layers.value_or(
176 video_codec.VP8()->numberOfTemporalLayers));
177 RTC_DCHECK_GE(video_codec.VP8()->numberOfTemporalLayers, 1);
178 RTC_DCHECK_LE(video_codec.VP8()->numberOfTemporalLayers,
179 kMaxTemporalStreams);
180
Sergey Silkin86684962018-03-28 19:32:37 +0200181 break;
182 }
183 case kVideoCodecVP9: {
184 if (!config.encoder_specific_settings) {
185 *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings();
186 }
187
188 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
189 streams.back().num_temporal_layers.value_or(
190 video_codec.VP9()->numberOfTemporalLayers));
191 RTC_DCHECK_GE(video_codec.VP9()->numberOfTemporalLayers, 1);
192 RTC_DCHECK_LE(video_codec.VP9()->numberOfTemporalLayers,
193 kMaxTemporalStreams);
194
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200195 RTC_DCHECK(config.spatial_layers.empty() ||
196 config.spatial_layers.size() ==
197 video_codec.VP9()->numberOfSpatialLayers);
198
199 std::vector<SpatialLayer> spatial_layers;
200 if (!config.spatial_layers.empty()) {
201 // Layering is set explicitly.
202 spatial_layers = config.spatial_layers;
Sergey Silkin86684962018-03-28 19:32:37 +0200203 } else {
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200204 spatial_layers = GetSvcConfig(
205 video_codec.width, video_codec.height, video_codec.maxFramerate,
206 video_codec.VP9()->numberOfSpatialLayers,
207 video_codec.VP9()->numberOfTemporalLayers,
208 video_codec.mode == VideoCodecMode::kScreensharing);
Sergey Silkin86684962018-03-28 19:32:37 +0200209
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200210 const bool no_spatial_layering = (spatial_layers.size() == 1);
211 if (no_spatial_layering) {
212 // Use codec's bitrate limits.
213 spatial_layers.back().minBitrate = video_codec.minBitrate;
214 spatial_layers.back().maxBitrate = video_codec.maxBitrate;
Sergey Silkin86684962018-03-28 19:32:37 +0200215 }
Sergey Silkin86684962018-03-28 19:32:37 +0200216 }
217
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200218 RTC_DCHECK(!spatial_layers.empty());
219 for (size_t i = 0; i < spatial_layers.size(); ++i) {
220 video_codec.spatialLayers[i] = spatial_layers[i];
221 }
222
223 // Update layering settings.
224 video_codec.VP9()->numberOfSpatialLayers =
225 static_cast<unsigned char>(spatial_layers.size());
226 RTC_DCHECK_GE(video_codec.VP9()->numberOfSpatialLayers, 1);
227 RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers,
228 kMaxSpatialLayers);
229
230 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
231 spatial_layers.back().numberOfTemporalLayers);
232 RTC_DCHECK_GE(video_codec.VP9()->numberOfTemporalLayers, 1);
233 RTC_DCHECK_LE(video_codec.VP9()->numberOfTemporalLayers,
234 kMaxTemporalStreams);
235
Sergey Silkin86684962018-03-28 19:32:37 +0200236 break;
237 }
238 case kVideoCodecH264: {
239 if (!config.encoder_specific_settings)
240 *video_codec.H264() = VideoEncoder::GetDefaultH264Settings();
241 break;
242 }
243 default:
244 // TODO(pbos): Support encoder_settings codec-agnostically.
245 RTC_DCHECK(!config.encoder_specific_settings)
246 << "Encoder-specific settings for codec type not wired up.";
247 break;
248 }
249
Erik Språng08127a92016-11-16 16:41:30 +0100250 return video_codec;
251}
252
253} // namespace webrtc