blob: b85b9328cff8c2d14a2a799233b799b7f2da9a1d [file] [log] [blame]
mflodman351424e2017-08-10 02:43:14 -07001/*
2 * Copyright (c) 2017 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 "api/video_codecs/video_encoder.h"
mflodman351424e2017-08-10 02:43:14 -070012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
Henrik Boströmb0f2e0c2020-03-06 13:32:03 +010014#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020015
16#include "rtc_base/checks.h"
Erik Språng79685302019-11-27 17:26:58 +010017#include "rtc_base/strings/string_builder.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018
mflodman351424e2017-08-10 02:43:14 -070019namespace webrtc {
20
21// TODO(mflodman): Add default complexity for VP9 and VP9.
22VideoCodecVP8 VideoEncoder::GetDefaultVp8Settings() {
23 VideoCodecVP8 vp8_settings;
24 memset(&vp8_settings, 0, sizeof(vp8_settings));
25
mflodman351424e2017-08-10 02:43:14 -070026 vp8_settings.numberOfTemporalLayers = 1;
27 vp8_settings.denoisingOn = true;
mflodman351424e2017-08-10 02:43:14 -070028 vp8_settings.automaticResizeOn = false;
mflodman351424e2017-08-10 02:43:14 -070029 vp8_settings.keyFrameInterval = 3000;
30
31 return vp8_settings;
32}
33
34VideoCodecVP9 VideoEncoder::GetDefaultVp9Settings() {
35 VideoCodecVP9 vp9_settings;
36 memset(&vp9_settings, 0, sizeof(vp9_settings));
37
mflodman351424e2017-08-10 02:43:14 -070038 vp9_settings.numberOfTemporalLayers = 1;
39 vp9_settings.denoisingOn = true;
mflodman351424e2017-08-10 02:43:14 -070040 vp9_settings.keyFrameInterval = 3000;
41 vp9_settings.adaptiveQpMode = true;
42 vp9_settings.automaticResizeOn = true;
43 vp9_settings.numberOfSpatialLayers = 1;
44 vp9_settings.flexibleMode = false;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +020045 vp9_settings.interLayerPred = InterLayerPredMode::kOn;
mflodman351424e2017-08-10 02:43:14 -070046
47 return vp9_settings;
48}
49
50VideoCodecH264 VideoEncoder::GetDefaultH264Settings() {
51 VideoCodecH264 h264_settings;
52 memset(&h264_settings, 0, sizeof(h264_settings));
53
mflodman351424e2017-08-10 02:43:14 -070054 h264_settings.keyFrameInterval = 3000;
Johnny Lee1a1c52b2019-02-08 14:25:40 -050055 h264_settings.numberOfTemporalLayers = 1;
mflodman351424e2017-08-10 02:43:14 -070056
57 return h264_settings;
58}
59
Niels Möller225c7872018-02-22 15:03:53 +010060VideoEncoder::ScalingSettings::ScalingSettings() = default;
mflodman351424e2017-08-10 02:43:14 -070061
Niels Möller225c7872018-02-22 15:03:53 +010062VideoEncoder::ScalingSettings::ScalingSettings(KOff) : ScalingSettings() {}
63
64VideoEncoder::ScalingSettings::ScalingSettings(int low, int high)
65 : thresholds(QpThresholds(low, high)) {}
66
67VideoEncoder::ScalingSettings::ScalingSettings(int low,
asapersson142fcc92017-08-17 08:58:54 -070068 int high,
69 int min_pixels)
Niels Möller225c7872018-02-22 15:03:53 +010070 : thresholds(QpThresholds(low, high)), min_pixels_per_frame(min_pixels) {}
asapersson142fcc92017-08-17 08:58:54 -070071
Niels Möller225c7872018-02-22 15:03:53 +010072VideoEncoder::ScalingSettings::ScalingSettings(const ScalingSettings&) =
73 default;
mflodman351424e2017-08-10 02:43:14 -070074
75VideoEncoder::ScalingSettings::~ScalingSettings() {}
76
Niels Möller225c7872018-02-22 15:03:53 +010077// static
78constexpr VideoEncoder::ScalingSettings::KOff
79 VideoEncoder::ScalingSettings::kOff;
Erik Språngdbdd8392019-01-17 15:27:50 +010080// static
81constexpr uint8_t VideoEncoder::EncoderInfo::kMaxFramerateFraction;
mflodman351424e2017-08-10 02:43:14 -070082
Erik Språng79685302019-11-27 17:26:58 +010083bool VideoEncoder::ResolutionBitrateLimits::operator==(
84 const ResolutionBitrateLimits& rhs) const {
85 return frame_size_pixels == rhs.frame_size_pixels &&
86 min_start_bitrate_bps == rhs.min_start_bitrate_bps &&
87 min_bitrate_bps == rhs.min_bitrate_bps &&
88 max_bitrate_bps == rhs.max_bitrate_bps;
89}
90
Erik Språnge2fd86a2018-10-24 11:32:39 +020091VideoEncoder::EncoderInfo::EncoderInfo()
92 : scaling_settings(VideoEncoder::ScalingSettings::kOff),
Rasmus Brandt5cad55b2019-12-19 09:47:11 +010093 requested_resolution_alignment(1),
Åsa Perssonc5a74ff2020-09-20 17:50:00 +020094 apply_alignment_to_all_simulcast_layers(false),
Erik Språnge2fd86a2018-10-24 11:32:39 +020095 supports_native_handle(false),
Erik Språngd3438aa2018-11-08 16:56:43 +010096 implementation_name("unknown"),
Mirta Dvornicic897a9912018-11-30 13:12:21 +010097 has_trusted_rate_controller(false),
Mirta Dvornicicccc1b572019-01-15 12:42:18 +010098 is_hardware_accelerated(true),
Erik Språngdbdd8392019-01-17 15:27:50 +010099 fps_allocation{absl::InlinedVector<uint8_t, kMaxTemporalStreams>(
100 1,
Erik Språngf4e0c292019-10-01 18:50:03 +0200101 kMaxFramerateFraction)},
Evan Shrubsoleb556b082020-10-08 14:56:45 +0200102 supports_simulcast(false),
103 preferred_pixel_formats{VideoFrameBuffer::Type::kI420} {}
Mirta Dvornicic897a9912018-11-30 13:12:21 +0100104
105VideoEncoder::EncoderInfo::EncoderInfo(const EncoderInfo&) = default;
Erik Språnge2fd86a2018-10-24 11:32:39 +0200106
Erik Språnge2fd86a2018-10-24 11:32:39 +0200107VideoEncoder::EncoderInfo::~EncoderInfo() = default;
108
Erik Språng79685302019-11-27 17:26:58 +0100109std::string VideoEncoder::EncoderInfo::ToString() const {
110 char string_buf[2048];
111 rtc::SimpleStringBuilder oss(string_buf);
112
113 oss << "EncoderInfo { "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100114 "ScalingSettings { ";
Erik Språng79685302019-11-27 17:26:58 +0100115 if (scaling_settings.thresholds) {
116 oss << "Thresholds { "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100117 "low = "
118 << scaling_settings.thresholds->low
Erik Språng79685302019-11-27 17:26:58 +0100119 << ", high = " << scaling_settings.thresholds->high << "}, ";
120 }
121 oss << "min_pixels_per_frame = " << scaling_settings.min_pixels_per_frame
122 << " }";
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100123 oss << ", requested_resolution_alignment = " << requested_resolution_alignment
Åsa Perssonc5a74ff2020-09-20 17:50:00 +0200124 << ", apply_alignment_to_all_simulcast_layers = "
125 << apply_alignment_to_all_simulcast_layers
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100126 << ", supports_native_handle = " << supports_native_handle
Jonas Olssonb2b20312020-01-14 12:11:31 +0100127 << ", implementation_name = '" << implementation_name
128 << "'"
129 ", has_trusted_rate_controller = "
130 << has_trusted_rate_controller
Erik Språng79685302019-11-27 17:26:58 +0100131 << ", is_hardware_accelerated = " << is_hardware_accelerated
Erik Språng79685302019-11-27 17:26:58 +0100132 << ", fps_allocation = [";
Erik Språng75941452021-05-31 17:08:02 +0200133 size_t num_spatial_layer_with_fps_allocation = 0;
134 for (size_t i = 0; i < kMaxSpatialLayers; ++i) {
135 if (!fps_allocation[i].empty()) {
136 num_spatial_layer_with_fps_allocation = i + 1;
137 }
138 }
Erik Språng79685302019-11-27 17:26:58 +0100139 bool first = true;
Erik Språng75941452021-05-31 17:08:02 +0200140 for (size_t i = 0; i < num_spatial_layer_with_fps_allocation; ++i) {
141 if (fps_allocation[i].empty()) {
142 break;
143 }
Erik Språng79685302019-11-27 17:26:58 +0100144 if (!first) {
145 oss << ", ";
146 }
147 const absl::InlinedVector<uint8_t, kMaxTemporalStreams>& fractions =
148 fps_allocation[i];
149 if (!fractions.empty()) {
150 first = false;
151 oss << "[ ";
152 for (size_t i = 0; i < fractions.size(); ++i) {
153 if (i > 0) {
154 oss << ", ";
155 }
156 oss << (static_cast<double>(fractions[i]) / kMaxFramerateFraction);
157 }
158 oss << "] ";
159 }
160 }
161 oss << "]";
162 oss << ", resolution_bitrate_limits = [";
163 for (size_t i = 0; i < resolution_bitrate_limits.size(); ++i) {
164 if (i > 0) {
165 oss << ", ";
166 }
167 ResolutionBitrateLimits l = resolution_bitrate_limits[i];
168 oss << "Limits { "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100169 "frame_size_pixels = "
170 << l.frame_size_pixels
Erik Språng79685302019-11-27 17:26:58 +0100171 << ", min_start_bitrate_bps = " << l.min_start_bitrate_bps
172 << ", min_bitrate_bps = " << l.min_bitrate_bps
173 << ", max_bitrate_bps = " << l.max_bitrate_bps << "} ";
174 }
175 oss << "] "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100176 ", supports_simulcast = "
Evan Shrubsoleb556b082020-10-08 14:56:45 +0200177 << supports_simulcast;
178 oss << ", preferred_pixel_formats = [";
179 for (size_t i = 0; i < preferred_pixel_formats.size(); ++i) {
180 if (i > 0)
181 oss << ", ";
182 oss << VideoFrameBufferTypeToString(preferred_pixel_formats.at(i));
183 }
184 oss << "]";
Qiu Jianlinb54cfde2021-07-30 06:48:03 +0800185 if (is_qp_trusted.has_value()) {
186 oss << ", is_qp_trusted = " << is_qp_trusted.value();
187 }
Evan Shrubsoleb556b082020-10-08 14:56:45 +0200188 oss << "}";
Erik Språng79685302019-11-27 17:26:58 +0100189 return oss.str();
190}
191
192bool VideoEncoder::EncoderInfo::operator==(const EncoderInfo& rhs) const {
193 if (scaling_settings.thresholds.has_value() !=
194 rhs.scaling_settings.thresholds.has_value()) {
195 return false;
196 }
197 if (scaling_settings.thresholds.has_value()) {
198 QpThresholds l = *scaling_settings.thresholds;
199 QpThresholds r = *rhs.scaling_settings.thresholds;
200 if (l.low != r.low || l.high != r.high) {
201 return false;
202 }
203 }
204 if (scaling_settings.min_pixels_per_frame !=
205 rhs.scaling_settings.min_pixels_per_frame) {
206 return false;
207 }
208
209 if (supports_native_handle != rhs.supports_native_handle ||
210 implementation_name != rhs.implementation_name ||
211 has_trusted_rate_controller != rhs.has_trusted_rate_controller ||
Niels Möller13d16362021-11-16 11:39:27 +0100212 is_hardware_accelerated != rhs.is_hardware_accelerated) {
Erik Språng79685302019-11-27 17:26:58 +0100213 return false;
214 }
215
216 for (size_t i = 0; i < kMaxSpatialLayers; ++i) {
217 if (fps_allocation[i] != rhs.fps_allocation[i]) {
218 return false;
219 }
220 }
221
222 if (resolution_bitrate_limits != rhs.resolution_bitrate_limits ||
223 supports_simulcast != rhs.supports_simulcast) {
224 return false;
225 }
226
227 return true;
228}
229
Henrik Boströmb0f2e0c2020-03-06 13:32:03 +0100230absl::optional<VideoEncoder::ResolutionBitrateLimits>
231VideoEncoder::EncoderInfo::GetEncoderBitrateLimitsForResolution(
232 int frame_size_pixels) const {
233 std::vector<ResolutionBitrateLimits> bitrate_limits =
234 resolution_bitrate_limits;
235
236 // Sort the list of bitrate limits by resolution.
237 sort(bitrate_limits.begin(), bitrate_limits.end(),
238 [](const ResolutionBitrateLimits& lhs,
239 const ResolutionBitrateLimits& rhs) {
240 return lhs.frame_size_pixels < rhs.frame_size_pixels;
241 });
242
243 for (size_t i = 0; i < bitrate_limits.size(); ++i) {
244 RTC_DCHECK_GE(bitrate_limits[i].min_bitrate_bps, 0);
245 RTC_DCHECK_GE(bitrate_limits[i].min_start_bitrate_bps, 0);
246 RTC_DCHECK_GE(bitrate_limits[i].max_bitrate_bps,
247 bitrate_limits[i].min_bitrate_bps);
248 if (i > 0) {
249 // The bitrate limits aren't expected to decrease with resolution.
250 RTC_DCHECK_GE(bitrate_limits[i].min_bitrate_bps,
251 bitrate_limits[i - 1].min_bitrate_bps);
252 RTC_DCHECK_GE(bitrate_limits[i].min_start_bitrate_bps,
253 bitrate_limits[i - 1].min_start_bitrate_bps);
254 RTC_DCHECK_GE(bitrate_limits[i].max_bitrate_bps,
255 bitrate_limits[i - 1].max_bitrate_bps);
256 }
257
258 if (bitrate_limits[i].frame_size_pixels >= frame_size_pixels) {
259 return absl::optional<ResolutionBitrateLimits>(bitrate_limits[i]);
260 }
261 }
262
263 return absl::nullopt;
264}
265
Erik Språng4c6ca302019-04-08 15:14:01 +0200266VideoEncoder::RateControlParameters::RateControlParameters()
267 : bitrate(VideoBitrateAllocation()),
268 framerate_fps(0.0),
269 bandwidth_allocation(DataRate::Zero()) {}
270
271VideoEncoder::RateControlParameters::RateControlParameters(
272 const VideoBitrateAllocation& bitrate,
Erik Språng16cb8f52019-04-12 13:59:09 +0200273 double framerate_fps)
274 : bitrate(bitrate),
275 framerate_fps(framerate_fps),
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100276 bandwidth_allocation(DataRate::BitsPerSec(bitrate.get_sum_bps())) {}
Erik Språng16cb8f52019-04-12 13:59:09 +0200277
278VideoEncoder::RateControlParameters::RateControlParameters(
279 const VideoBitrateAllocation& bitrate,
Erik Språng4c6ca302019-04-08 15:14:01 +0200280 double framerate_fps,
281 DataRate bandwidth_allocation)
282 : bitrate(bitrate),
283 framerate_fps(framerate_fps),
284 bandwidth_allocation(bandwidth_allocation) {}
285
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200286bool VideoEncoder::RateControlParameters::operator==(
287 const VideoEncoder::RateControlParameters& rhs) const {
288 return std::tie(bitrate, framerate_fps, bandwidth_allocation) ==
289 std::tie(rhs.bitrate, rhs.framerate_fps, rhs.bandwidth_allocation);
290}
291
292bool VideoEncoder::RateControlParameters::operator!=(
293 const VideoEncoder::RateControlParameters& rhs) const {
294 return !(rhs == *this);
295}
296
Erik Språng4c6ca302019-04-08 15:14:01 +0200297VideoEncoder::RateControlParameters::~RateControlParameters() = default;
298
Elad Alon8f01c4e2019-06-28 15:19:43 +0200299void VideoEncoder::SetFecControllerOverride(
300 FecControllerOverride* fec_controller_override) {}
301
Elad Alon370f93a2019-06-11 14:57:57 +0200302int32_t VideoEncoder::InitEncode(const VideoCodec* codec_settings,
303 int32_t number_of_cores,
304 size_t max_payload_size) {
305 const VideoEncoder::Capabilities capabilities(/* loss_notification= */ false);
306 const VideoEncoder::Settings settings(capabilities, number_of_cores,
307 max_payload_size);
308 // In theory, this and the other version of InitEncode() could end up calling
309 // each other in a loop until we get a stack overflow.
310 // In practice, any subclass of VideoEncoder would overload at least one
311 // of these, and we have a TODO in the header file to make this pure virtual.
312 return InitEncode(codec_settings, settings);
313}
314
315int VideoEncoder::InitEncode(const VideoCodec* codec_settings,
316 const VideoEncoder::Settings& settings) {
317 // In theory, this and the other version of InitEncode() could end up calling
318 // each other in a loop until we get a stack overflow.
319 // In practice, any subclass of VideoEncoder would overload at least one
320 // of these, and we have a TODO in the header file to make this pure virtual.
321 return InitEncode(codec_settings, settings.number_of_cores,
322 settings.max_payload_size);
323}
324
Elad Aloncde8ab22019-03-20 11:56:20 +0100325void VideoEncoder::OnPacketLossRateUpdate(float packet_loss_rate) {}
326
327void VideoEncoder::OnRttUpdate(int64_t rtt_ms) {}
328
Elad Alon6c371ca2019-04-04 12:28:51 +0200329void VideoEncoder::OnLossNotification(
330 const LossNotification& loss_notification) {}
331
Erik Språng6ed4f142018-11-26 13:42:39 +0100332// TODO(webrtc:9722): Remove and make pure virtual.
Erik Språnge2fd86a2018-10-24 11:32:39 +0200333VideoEncoder::EncoderInfo VideoEncoder::GetEncoderInfo() const {
Erik Språng6ed4f142018-11-26 13:42:39 +0100334 return EncoderInfo();
Erik Språnge2fd86a2018-10-24 11:32:39 +0200335}
Erik Språng6ed4f142018-11-26 13:42:39 +0100336
mflodman351424e2017-08-10 02:43:14 -0700337} // namespace webrtc