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 "modules/video_coding/include/video_codec_initializer.h"
12
13 #include <stdint.h>
14 #include <string.h>
15
16 #include <algorithm>
17
18 #include "absl/types/optional.h"
19 #include "api/scoped_refptr.h"
20 #include "api/units/data_rate.h"
21 #include "api/video/video_bitrate_allocation.h"
22 #include "api/video_codecs/video_encoder.h"
23 #include "modules/video_coding/codecs/vp9/svc_config.h"
24 #include "modules/video_coding/include/video_coding_defines.h"
25 #include "rtc_base/checks.h"
26 #include "rtc_base/experiments/min_video_bitrate_experiment.h"
27 #include "rtc_base/logging.h"
28 #include "rtc_base/numerics/safe_conversions.h"
29
30 namespace webrtc {
31
SetupCodec(const VideoEncoderConfig & config,const std::vector<VideoStream> & streams,VideoCodec * codec)32 bool VideoCodecInitializer::SetupCodec(const VideoEncoderConfig& config,
33 const std::vector<VideoStream>& streams,
34 VideoCodec* codec) {
35 if (config.codec_type == kVideoCodecMultiplex) {
36 VideoEncoderConfig associated_config = config.Copy();
37 associated_config.codec_type = kVideoCodecVP9;
38 if (!SetupCodec(associated_config, streams, codec)) {
39 RTC_LOG(LS_ERROR) << "Failed to create stereo encoder configuration.";
40 return false;
41 }
42 codec->codecType = kVideoCodecMultiplex;
43 return true;
44 }
45
46 *codec = VideoEncoderConfigToVideoCodec(config, streams);
47 return true;
48 }
49
50 // TODO(sprang): Split this up and separate the codec specific parts.
VideoEncoderConfigToVideoCodec(const VideoEncoderConfig & config,const std::vector<VideoStream> & streams)51 VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
52 const VideoEncoderConfig& config,
53 const std::vector<VideoStream>& streams) {
54 static const int kEncoderMinBitrateKbps = 30;
55 RTC_DCHECK(!streams.empty());
56 RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0);
57
58 VideoCodec video_codec;
59 memset(&video_codec, 0, sizeof(video_codec));
60 video_codec.codecType = config.codec_type;
61
62 switch (config.content_type) {
63 case VideoEncoderConfig::ContentType::kRealtimeVideo:
64 video_codec.mode = VideoCodecMode::kRealtimeVideo;
65 break;
66 case VideoEncoderConfig::ContentType::kScreen:
67 video_codec.mode = VideoCodecMode::kScreensharing;
68 break;
69 }
70
71 // TODO(nisse): The plType field should be deleted. Luckily, our
72 // callers don't need it.
73 video_codec.plType = 0;
74 video_codec.numberOfSimulcastStreams =
75 static_cast<unsigned char>(streams.size());
76 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000;
77 bool codec_active = false;
78 // Active configuration might not be fully copied to |streams| for SVC yet.
79 // Therefore the |config| is checked here.
80 for (const VideoStream& stream : config.simulcast_layers) {
81 if (stream.active) {
82 codec_active = true;
83 break;
84 }
85 }
86 // Set active for the entire video codec for the non simulcast case.
87 video_codec.active = codec_active;
88 if (video_codec.minBitrate < kEncoderMinBitrateKbps)
89 video_codec.minBitrate = kEncoderMinBitrateKbps;
90 video_codec.timing_frame_thresholds = {kDefaultTimingFramesDelayMs,
91 kDefaultOutlierFrameSizePercent};
92 RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams);
93
94 int max_framerate = 0;
95
96 for (size_t i = 0; i < streams.size(); ++i) {
97 SimulcastStream* sim_stream = &video_codec.simulcastStream[i];
98 RTC_DCHECK_GT(streams[i].width, 0);
99 RTC_DCHECK_GT(streams[i].height, 0);
100 RTC_DCHECK_GT(streams[i].max_framerate, 0);
101 RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0);
102 RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps);
103 RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps);
104 RTC_DCHECK_GE(streams[i].max_qp, 0);
105
106 sim_stream->width = static_cast<uint16_t>(streams[i].width);
107 sim_stream->height = static_cast<uint16_t>(streams[i].height);
108 sim_stream->maxFramerate = streams[i].max_framerate;
109 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000;
110 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000;
111 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000;
112 sim_stream->qpMax = streams[i].max_qp;
113 sim_stream->numberOfTemporalLayers =
114 static_cast<unsigned char>(streams[i].num_temporal_layers.value_or(1));
115 sim_stream->active = streams[i].active;
116
117 video_codec.width =
118 std::max(video_codec.width, static_cast<uint16_t>(streams[i].width));
119 video_codec.height =
120 std::max(video_codec.height, static_cast<uint16_t>(streams[i].height));
121 video_codec.minBitrate =
122 std::min(static_cast<uint16_t>(video_codec.minBitrate),
123 static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000));
124 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000;
125 video_codec.qpMax = std::max(video_codec.qpMax,
126 static_cast<unsigned int>(streams[i].max_qp));
127 max_framerate = std::max(max_framerate, streams[i].max_framerate);
128 }
129
130 if (video_codec.maxBitrate == 0) {
131 // Unset max bitrate -> cap to one bit per pixel.
132 video_codec.maxBitrate =
133 (video_codec.width * video_codec.height * video_codec.maxFramerate) /
134 1000;
135 }
136 if (video_codec.maxBitrate < kEncoderMinBitrateKbps)
137 video_codec.maxBitrate = kEncoderMinBitrateKbps;
138
139 video_codec.maxFramerate = max_framerate;
140
141 // Set codec specific options
142 if (config.encoder_specific_settings)
143 config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec);
144
145 switch (video_codec.codecType) {
146 case kVideoCodecVP8: {
147 if (!config.encoder_specific_settings) {
148 *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings();
149 }
150
151 video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>(
152 streams.back().num_temporal_layers.value_or(
153 video_codec.VP8()->numberOfTemporalLayers));
154 RTC_DCHECK_GE(video_codec.VP8()->numberOfTemporalLayers, 1);
155 RTC_DCHECK_LE(video_codec.VP8()->numberOfTemporalLayers,
156 kMaxTemporalStreams);
157
158 break;
159 }
160 case kVideoCodecVP9: {
161 // Force the first stream to always be active.
162 video_codec.simulcastStream[0].active = codec_active;
163
164 if (!config.encoder_specific_settings) {
165 *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings();
166 }
167
168 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
169 streams.back().num_temporal_layers.value_or(
170 video_codec.VP9()->numberOfTemporalLayers));
171 RTC_DCHECK_GE(video_codec.VP9()->numberOfTemporalLayers, 1);
172 RTC_DCHECK_LE(video_codec.VP9()->numberOfTemporalLayers,
173 kMaxTemporalStreams);
174
175 RTC_DCHECK(config.spatial_layers.empty() ||
176 config.spatial_layers.size() ==
177 video_codec.VP9()->numberOfSpatialLayers);
178
179 std::vector<SpatialLayer> spatial_layers;
180 if (!config.spatial_layers.empty()) {
181 // Layering is set explicitly.
182 spatial_layers = config.spatial_layers;
183 } else {
184 size_t first_active_layer = 0;
185 for (size_t spatial_idx = 0;
186 spatial_idx < config.simulcast_layers.size(); ++spatial_idx) {
187 if (config.simulcast_layers[spatial_idx].active) {
188 first_active_layer = spatial_idx;
189 break;
190 }
191 }
192
193 spatial_layers = GetSvcConfig(
194 video_codec.width, video_codec.height, video_codec.maxFramerate,
195 first_active_layer, video_codec.VP9()->numberOfSpatialLayers,
196 video_codec.VP9()->numberOfTemporalLayers,
197 video_codec.mode == VideoCodecMode::kScreensharing);
198
199 // If there was no request for spatial layering, don't limit bitrate
200 // of single spatial layer.
201 const bool no_spatial_layering =
202 video_codec.VP9()->numberOfSpatialLayers <= 1;
203 if (no_spatial_layering) {
204 // Use codec's bitrate limits.
205 spatial_layers.back().minBitrate = video_codec.minBitrate;
206 spatial_layers.back().targetBitrate = video_codec.maxBitrate;
207 spatial_layers.back().maxBitrate = video_codec.maxBitrate;
208 }
209
210 for (size_t spatial_idx = first_active_layer;
211 spatial_idx < config.simulcast_layers.size() &&
212 spatial_idx < spatial_layers.size();
213 ++spatial_idx) {
214 spatial_layers[spatial_idx - first_active_layer].active =
215 config.simulcast_layers[spatial_idx].active;
216 }
217 }
218
219 RTC_DCHECK(!spatial_layers.empty());
220 for (size_t i = 0; i < spatial_layers.size(); ++i) {
221 video_codec.spatialLayers[i] = spatial_layers[i];
222 }
223
224 // The top spatial layer dimensions may not be equal to the input
225 // resolution because of the rounding or explicit configuration.
226 // This difference must be propagated to the stream configuration.
227 video_codec.width = spatial_layers.back().width;
228 video_codec.height = spatial_layers.back().height;
229 video_codec.simulcastStream[0].width = spatial_layers.back().width;
230 video_codec.simulcastStream[0].height = spatial_layers.back().height;
231
232 // Update layering settings.
233 video_codec.VP9()->numberOfSpatialLayers =
234 static_cast<unsigned char>(spatial_layers.size());
235 RTC_DCHECK_GE(video_codec.VP9()->numberOfSpatialLayers, 1);
236 RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers,
237 kMaxSpatialLayers);
238
239 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
240 spatial_layers.back().numberOfTemporalLayers);
241 RTC_DCHECK_GE(video_codec.VP9()->numberOfTemporalLayers, 1);
242 RTC_DCHECK_LE(video_codec.VP9()->numberOfTemporalLayers,
243 kMaxTemporalStreams);
244
245 break;
246 }
247 case kVideoCodecH264: {
248 if (!config.encoder_specific_settings)
249 *video_codec.H264() = VideoEncoder::GetDefaultH264Settings();
250 video_codec.H264()->numberOfTemporalLayers = static_cast<unsigned char>(
251 streams.back().num_temporal_layers.value_or(
252 video_codec.H264()->numberOfTemporalLayers));
253 RTC_DCHECK_GE(video_codec.H264()->numberOfTemporalLayers, 1);
254 RTC_DCHECK_LE(video_codec.H264()->numberOfTemporalLayers,
255 kMaxTemporalStreams);
256 break;
257 }
258 default:
259 // TODO(pbos): Support encoder_settings codec-agnostically.
260 RTC_DCHECK(!config.encoder_specific_settings)
261 << "Encoder-specific settings for codec type not wired up.";
262 break;
263 }
264
265 const absl::optional<DataRate> experimental_min_bitrate =
266 GetExperimentalMinVideoBitrate(video_codec.codecType);
267 if (experimental_min_bitrate) {
268 const int experimental_min_bitrate_kbps =
269 rtc::saturated_cast<int>(experimental_min_bitrate->kbps());
270 video_codec.minBitrate = experimental_min_bitrate_kbps;
271 video_codec.simulcastStream[0].minBitrate = experimental_min_bitrate_kbps;
272 if (video_codec.codecType == kVideoCodecVP9) {
273 video_codec.spatialLayers[0].minBitrate = experimental_min_bitrate_kbps;
274 }
275 }
276
277 return video_codec;
278 }
279
280 } // namespace webrtc
281