blob: e7125ce2782313f8fbc2606b96dbe46ab3ec4e5c [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);
Qingsi Wang59844ce2018-11-01 04:45:53 +000044 *bitrate_allocator = CreateBitrateAllocator(*codec);
45
Erik Språng08127a92016-11-16 16:41:30 +010046 return true;
47}
48
Qingsi Wang59844ce2018-11-01 04:45:53 +000049std::unique_ptr<VideoBitrateAllocator>
50VideoCodecInitializer::CreateBitrateAllocator(const VideoCodec& codec) {
51 std::unique_ptr<VideoBitrateAllocator> rate_allocator;
52
53 switch (codec.codecType) {
54 case kVideoCodecVP8:
55 RTC_FALLTHROUGH();
56 case kVideoCodecH264:
57 rate_allocator.reset(new SimulcastRateAllocator(codec));
58 break;
59 case kVideoCodecVP9:
60 rate_allocator.reset(new SvcRateAllocator(codec));
61 break;
62 default:
63 rate_allocator.reset(new DefaultVideoBitrateAllocator(codec));
64 }
65
66 return rate_allocator;
67}
68
Erik Språng08127a92016-11-16 16:41:30 +010069// TODO(sprang): Split this up and separate the codec specific parts.
70VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
71 const VideoEncoderConfig& config,
Niels Möllerf1338562018-04-26 09:51:47 +020072 const std::vector<VideoStream>& streams) {
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öller259a4972018-04-05 15:36:51 +020079 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:
Niels Möllere3cf3d02018-06-13 11:52:16 +020083 video_codec.mode = VideoCodecMode::kRealtimeVideo;
Erik Språng08127a92016-11-16 16:41:30 +010084 break;
85 case VideoEncoderConfig::ContentType::kScreen:
Niels Möllere3cf3d02018-06-13 11:52:16 +020086 video_codec.mode = VideoCodecMode::kScreensharing;
Erik Språng08127a92016-11-16 16:41:30 +010087 break;
88 }
89
Niels Möller24a842a2018-03-22 08:52:50 +010090 // TODO(nisse): The plType field should be deleted. Luckily, our
91 // callers don't need it.
92 video_codec.plType = 0;
Erik Språng08127a92016-11-16 16:41:30 +010093 video_codec.numberOfSimulcastStreams =
94 static_cast<unsigned char>(streams.size());
95 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000;
Seth Hampson46e31ba2018-01-18 10:39:54 -080096 bool codec_active = false;
97 for (const VideoStream& stream : streams) {
98 if (stream.active) {
99 codec_active = true;
100 break;
101 }
102 }
103 // Set active for the entire video codec for the non simulcast case.
104 video_codec.active = codec_active;
Erik Språng08127a92016-11-16 16:41:30 +0100105 if (video_codec.minBitrate < kEncoderMinBitrateKbps)
106 video_codec.minBitrate = kEncoderMinBitrateKbps;
ilnik04f4d122017-06-19 07:18:55 -0700107 video_codec.timing_frame_thresholds = {kDefaultTimingFramesDelayMs,
108 kDefaultOutlierFrameSizePercent};
kwiberg352444f2016-11-28 15:58:53 -0800109 RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams);
Sergey Silkin86684962018-03-28 19:32:37 +0200110
Erik Språng08127a92016-11-16 16:41:30 +0100111 for (size_t i = 0; i < streams.size(); ++i) {
112 SimulcastStream* sim_stream = &video_codec.simulcastStream[i];
kwibergaf476c72016-11-28 15:21:39 -0800113 RTC_DCHECK_GT(streams[i].width, 0);
114 RTC_DCHECK_GT(streams[i].height, 0);
Erik Språng08127a92016-11-16 16:41:30 +0100115 RTC_DCHECK_GT(streams[i].max_framerate, 0);
sprang429600d2017-01-26 06:12:26 -0800116 // Different framerates not supported per stream at the moment, unless it's
117 // screenshare where there is an exception and a simulcast encoder adapter,
118 // which supports different framerates, is used instead.
119 if (config.content_type != VideoEncoderConfig::ContentType::kScreen) {
120 RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate);
121 }
Erik Språng08127a92016-11-16 16:41:30 +0100122 RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0);
123 RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps);
124 RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps);
125 RTC_DCHECK_GE(streams[i].max_qp, 0);
126
127 sim_stream->width = static_cast<uint16_t>(streams[i].width);
128 sim_stream->height = static_cast<uint16_t>(streams[i].height);
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200129 sim_stream->maxFramerate = streams[i].max_framerate;
Erik Språng08127a92016-11-16 16:41:30 +0100130 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000;
131 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000;
132 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000;
133 sim_stream->qpMax = streams[i].max_qp;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100134 sim_stream->numberOfTemporalLayers =
135 static_cast<unsigned char>(streams[i].num_temporal_layers.value_or(1));
Seth Hampson46e31ba2018-01-18 10:39:54 -0800136 sim_stream->active = streams[i].active;
Erik Språng08127a92016-11-16 16:41:30 +0100137
138 video_codec.width =
Danil Chapovalov350531e2018-06-08 11:04:04 +0000139 std::max(video_codec.width, static_cast<uint16_t>(streams[i].width));
Erik Språng08127a92016-11-16 16:41:30 +0100140 video_codec.height =
Danil Chapovalov350531e2018-06-08 11:04:04 +0000141 std::max(video_codec.height, static_cast<uint16_t>(streams[i].height));
Erik Språng08127a92016-11-16 16:41:30 +0100142 video_codec.minBitrate =
143 std::min(static_cast<uint16_t>(video_codec.minBitrate),
144 static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000));
145 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000;
146 video_codec.qpMax = std::max(video_codec.qpMax,
147 static_cast<unsigned int>(streams[i].max_qp));
148 }
149
150 if (video_codec.maxBitrate == 0) {
151 // Unset max bitrate -> cap to one bit per pixel.
152 video_codec.maxBitrate =
153 (video_codec.width * video_codec.height * video_codec.maxFramerate) /
154 1000;
155 }
156 if (video_codec.maxBitrate < kEncoderMinBitrateKbps)
157 video_codec.maxBitrate = kEncoderMinBitrateKbps;
158
159 RTC_DCHECK_GT(streams[0].max_framerate, 0);
160 video_codec.maxFramerate = streams[0].max_framerate;
Sergey Silkin86684962018-03-28 19:32:37 +0200161
162 // Set codec specific options
163 if (config.encoder_specific_settings)
164 config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec);
165
166 switch (video_codec.codecType) {
167 case kVideoCodecVP8: {
168 if (!config.encoder_specific_settings) {
169 *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings();
170 }
171
172 video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>(
173 streams.back().num_temporal_layers.value_or(
174 video_codec.VP8()->numberOfTemporalLayers));
175 RTC_DCHECK_GE(video_codec.VP8()->numberOfTemporalLayers, 1);
176 RTC_DCHECK_LE(video_codec.VP8()->numberOfTemporalLayers,
177 kMaxTemporalStreams);
178
Sergey Silkin86684962018-03-28 19:32:37 +0200179 break;
180 }
181 case kVideoCodecVP9: {
182 if (!config.encoder_specific_settings) {
183 *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings();
184 }
185
186 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
187 streams.back().num_temporal_layers.value_or(
188 video_codec.VP9()->numberOfTemporalLayers));
189 RTC_DCHECK_GE(video_codec.VP9()->numberOfTemporalLayers, 1);
190 RTC_DCHECK_LE(video_codec.VP9()->numberOfTemporalLayers,
191 kMaxTemporalStreams);
192
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200193 RTC_DCHECK(config.spatial_layers.empty() ||
194 config.spatial_layers.size() ==
195 video_codec.VP9()->numberOfSpatialLayers);
196
197 std::vector<SpatialLayer> spatial_layers;
198 if (!config.spatial_layers.empty()) {
199 // Layering is set explicitly.
200 spatial_layers = config.spatial_layers;
Sergey Silkin86684962018-03-28 19:32:37 +0200201 } else {
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200202 spatial_layers = GetSvcConfig(
203 video_codec.width, video_codec.height, video_codec.maxFramerate,
204 video_codec.VP9()->numberOfSpatialLayers,
205 video_codec.VP9()->numberOfTemporalLayers,
206 video_codec.mode == VideoCodecMode::kScreensharing);
Sergey Silkin86684962018-03-28 19:32:37 +0200207
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200208 const bool no_spatial_layering = (spatial_layers.size() == 1);
209 if (no_spatial_layering) {
210 // Use codec's bitrate limits.
211 spatial_layers.back().minBitrate = video_codec.minBitrate;
212 spatial_layers.back().maxBitrate = video_codec.maxBitrate;
Sergey Silkin86684962018-03-28 19:32:37 +0200213 }
Sergey Silkin86684962018-03-28 19:32:37 +0200214 }
215
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200216 RTC_DCHECK(!spatial_layers.empty());
217 for (size_t i = 0; i < spatial_layers.size(); ++i) {
218 video_codec.spatialLayers[i] = spatial_layers[i];
219 }
220
221 // Update layering settings.
222 video_codec.VP9()->numberOfSpatialLayers =
223 static_cast<unsigned char>(spatial_layers.size());
224 RTC_DCHECK_GE(video_codec.VP9()->numberOfSpatialLayers, 1);
225 RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers,
226 kMaxSpatialLayers);
227
228 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
229 spatial_layers.back().numberOfTemporalLayers);
230 RTC_DCHECK_GE(video_codec.VP9()->numberOfTemporalLayers, 1);
231 RTC_DCHECK_LE(video_codec.VP9()->numberOfTemporalLayers,
232 kMaxTemporalStreams);
233
Sergey Silkin86684962018-03-28 19:32:37 +0200234 break;
235 }
236 case kVideoCodecH264: {
237 if (!config.encoder_specific_settings)
238 *video_codec.H264() = VideoEncoder::GetDefaultH264Settings();
239 break;
240 }
241 default:
242 // TODO(pbos): Support encoder_settings codec-agnostically.
243 RTC_DCHECK(!config.encoder_specific_settings)
244 << "Encoder-specific settings for codec type not wired up.";
245 break;
246 }
247
Erik Språng08127a92016-11-16 16:41:30 +0100248 return video_codec;
249}
250
251} // namespace webrtc