blob: ab946a26e0b7c6af4270bc9f05f741ec708641bd [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
Danil Chapovalov0490c372020-08-04 13:05:43 +020021EncodedImageCallback::Result EncodedImageCallback::OnEncodedImage(
22 const EncodedImage& encoded_image,
23 const CodecSpecificInfo* codec_specific_info,
24 const RTPFragmentationHeader* /*fragmentation*/) {
25 return OnEncodedImage(encoded_image, codec_specific_info);
26}
27
28EncodedImageCallback::Result EncodedImageCallback::OnEncodedImage(
29 const EncodedImage& encoded_image,
30 const CodecSpecificInfo* codec_specific_info) {
31 return OnEncodedImage(encoded_image, codec_specific_info, nullptr);
32}
33
mflodman351424e2017-08-10 02:43:14 -070034// TODO(mflodman): Add default complexity for VP9 and VP9.
35VideoCodecVP8 VideoEncoder::GetDefaultVp8Settings() {
36 VideoCodecVP8 vp8_settings;
37 memset(&vp8_settings, 0, sizeof(vp8_settings));
38
mflodman351424e2017-08-10 02:43:14 -070039 vp8_settings.numberOfTemporalLayers = 1;
40 vp8_settings.denoisingOn = true;
mflodman351424e2017-08-10 02:43:14 -070041 vp8_settings.automaticResizeOn = false;
42 vp8_settings.frameDroppingOn = true;
43 vp8_settings.keyFrameInterval = 3000;
44
45 return vp8_settings;
46}
47
48VideoCodecVP9 VideoEncoder::GetDefaultVp9Settings() {
49 VideoCodecVP9 vp9_settings;
50 memset(&vp9_settings, 0, sizeof(vp9_settings));
51
mflodman351424e2017-08-10 02:43:14 -070052 vp9_settings.numberOfTemporalLayers = 1;
53 vp9_settings.denoisingOn = true;
54 vp9_settings.frameDroppingOn = true;
55 vp9_settings.keyFrameInterval = 3000;
56 vp9_settings.adaptiveQpMode = true;
57 vp9_settings.automaticResizeOn = true;
58 vp9_settings.numberOfSpatialLayers = 1;
59 vp9_settings.flexibleMode = false;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +020060 vp9_settings.interLayerPred = InterLayerPredMode::kOn;
mflodman351424e2017-08-10 02:43:14 -070061
62 return vp9_settings;
63}
64
65VideoCodecH264 VideoEncoder::GetDefaultH264Settings() {
66 VideoCodecH264 h264_settings;
67 memset(&h264_settings, 0, sizeof(h264_settings));
68
69 h264_settings.frameDroppingOn = true;
70 h264_settings.keyFrameInterval = 3000;
Johnny Lee1a1c52b2019-02-08 14:25:40 -050071 h264_settings.numberOfTemporalLayers = 1;
mflodman351424e2017-08-10 02:43:14 -070072
73 return h264_settings;
74}
75
Niels Möller225c7872018-02-22 15:03:53 +010076VideoEncoder::ScalingSettings::ScalingSettings() = default;
mflodman351424e2017-08-10 02:43:14 -070077
Niels Möller225c7872018-02-22 15:03:53 +010078VideoEncoder::ScalingSettings::ScalingSettings(KOff) : ScalingSettings() {}
79
80VideoEncoder::ScalingSettings::ScalingSettings(int low, int high)
81 : thresholds(QpThresholds(low, high)) {}
82
83VideoEncoder::ScalingSettings::ScalingSettings(int low,
asapersson142fcc92017-08-17 08:58:54 -070084 int high,
85 int min_pixels)
Niels Möller225c7872018-02-22 15:03:53 +010086 : thresholds(QpThresholds(low, high)), min_pixels_per_frame(min_pixels) {}
asapersson142fcc92017-08-17 08:58:54 -070087
Niels Möller225c7872018-02-22 15:03:53 +010088VideoEncoder::ScalingSettings::ScalingSettings(const ScalingSettings&) =
89 default;
mflodman351424e2017-08-10 02:43:14 -070090
91VideoEncoder::ScalingSettings::~ScalingSettings() {}
92
Niels Möller225c7872018-02-22 15:03:53 +010093// static
94constexpr VideoEncoder::ScalingSettings::KOff
95 VideoEncoder::ScalingSettings::kOff;
Erik Språngdbdd8392019-01-17 15:27:50 +010096// static
97constexpr uint8_t VideoEncoder::EncoderInfo::kMaxFramerateFraction;
mflodman351424e2017-08-10 02:43:14 -070098
Erik Språng79685302019-11-27 17:26:58 +010099bool VideoEncoder::ResolutionBitrateLimits::operator==(
100 const ResolutionBitrateLimits& rhs) const {
101 return frame_size_pixels == rhs.frame_size_pixels &&
102 min_start_bitrate_bps == rhs.min_start_bitrate_bps &&
103 min_bitrate_bps == rhs.min_bitrate_bps &&
104 max_bitrate_bps == rhs.max_bitrate_bps;
105}
106
Erik Språnge2fd86a2018-10-24 11:32:39 +0200107VideoEncoder::EncoderInfo::EncoderInfo()
108 : scaling_settings(VideoEncoder::ScalingSettings::kOff),
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100109 requested_resolution_alignment(1),
Erik Språnge2fd86a2018-10-24 11:32:39 +0200110 supports_native_handle(false),
Erik Språngd3438aa2018-11-08 16:56:43 +0100111 implementation_name("unknown"),
Mirta Dvornicic897a9912018-11-30 13:12:21 +0100112 has_trusted_rate_controller(false),
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100113 is_hardware_accelerated(true),
Erik Språngdbdd8392019-01-17 15:27:50 +0100114 has_internal_source(false),
115 fps_allocation{absl::InlinedVector<uint8_t, kMaxTemporalStreams>(
116 1,
Erik Språngf4e0c292019-10-01 18:50:03 +0200117 kMaxFramerateFraction)},
118 supports_simulcast(false) {}
Mirta Dvornicic897a9912018-11-30 13:12:21 +0100119
120VideoEncoder::EncoderInfo::EncoderInfo(const EncoderInfo&) = default;
Erik Språnge2fd86a2018-10-24 11:32:39 +0200121
Erik Språnge2fd86a2018-10-24 11:32:39 +0200122VideoEncoder::EncoderInfo::~EncoderInfo() = default;
123
Erik Språng79685302019-11-27 17:26:58 +0100124std::string VideoEncoder::EncoderInfo::ToString() const {
125 char string_buf[2048];
126 rtc::SimpleStringBuilder oss(string_buf);
127
128 oss << "EncoderInfo { "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100129 "ScalingSettings { ";
Erik Språng79685302019-11-27 17:26:58 +0100130 if (scaling_settings.thresholds) {
131 oss << "Thresholds { "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100132 "low = "
133 << scaling_settings.thresholds->low
Erik Språng79685302019-11-27 17:26:58 +0100134 << ", high = " << scaling_settings.thresholds->high << "}, ";
135 }
136 oss << "min_pixels_per_frame = " << scaling_settings.min_pixels_per_frame
137 << " }";
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100138 oss << ", requested_resolution_alignment = " << requested_resolution_alignment
139 << ", supports_native_handle = " << supports_native_handle
Jonas Olssonb2b20312020-01-14 12:11:31 +0100140 << ", implementation_name = '" << implementation_name
141 << "'"
142 ", has_trusted_rate_controller = "
143 << has_trusted_rate_controller
Erik Språng79685302019-11-27 17:26:58 +0100144 << ", is_hardware_accelerated = " << is_hardware_accelerated
145 << ", has_internal_source = " << has_internal_source
146 << ", fps_allocation = [";
147 bool first = true;
148 for (size_t i = 0; i < fps_allocation->size(); ++i) {
149 if (!first) {
150 oss << ", ";
151 }
152 const absl::InlinedVector<uint8_t, kMaxTemporalStreams>& fractions =
153 fps_allocation[i];
154 if (!fractions.empty()) {
155 first = false;
156 oss << "[ ";
157 for (size_t i = 0; i < fractions.size(); ++i) {
158 if (i > 0) {
159 oss << ", ";
160 }
161 oss << (static_cast<double>(fractions[i]) / kMaxFramerateFraction);
162 }
163 oss << "] ";
164 }
165 }
166 oss << "]";
167 oss << ", resolution_bitrate_limits = [";
168 for (size_t i = 0; i < resolution_bitrate_limits.size(); ++i) {
169 if (i > 0) {
170 oss << ", ";
171 }
172 ResolutionBitrateLimits l = resolution_bitrate_limits[i];
173 oss << "Limits { "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100174 "frame_size_pixels = "
175 << l.frame_size_pixels
Erik Språng79685302019-11-27 17:26:58 +0100176 << ", min_start_bitrate_bps = " << l.min_start_bitrate_bps
177 << ", min_bitrate_bps = " << l.min_bitrate_bps
178 << ", max_bitrate_bps = " << l.max_bitrate_bps << "} ";
179 }
180 oss << "] "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100181 ", supports_simulcast = "
182 << supports_simulcast << "}";
Erik Språng79685302019-11-27 17:26:58 +0100183 return oss.str();
184}
185
186bool VideoEncoder::EncoderInfo::operator==(const EncoderInfo& rhs) const {
187 if (scaling_settings.thresholds.has_value() !=
188 rhs.scaling_settings.thresholds.has_value()) {
189 return false;
190 }
191 if (scaling_settings.thresholds.has_value()) {
192 QpThresholds l = *scaling_settings.thresholds;
193 QpThresholds r = *rhs.scaling_settings.thresholds;
194 if (l.low != r.low || l.high != r.high) {
195 return false;
196 }
197 }
198 if (scaling_settings.min_pixels_per_frame !=
199 rhs.scaling_settings.min_pixels_per_frame) {
200 return false;
201 }
202
203 if (supports_native_handle != rhs.supports_native_handle ||
204 implementation_name != rhs.implementation_name ||
205 has_trusted_rate_controller != rhs.has_trusted_rate_controller ||
206 is_hardware_accelerated != rhs.is_hardware_accelerated ||
207 has_internal_source != rhs.has_internal_source) {
208 return false;
209 }
210
211 for (size_t i = 0; i < kMaxSpatialLayers; ++i) {
212 if (fps_allocation[i] != rhs.fps_allocation[i]) {
213 return false;
214 }
215 }
216
217 if (resolution_bitrate_limits != rhs.resolution_bitrate_limits ||
218 supports_simulcast != rhs.supports_simulcast) {
219 return false;
220 }
221
222 return true;
223}
224
Henrik Boströmb0f2e0c2020-03-06 13:32:03 +0100225absl::optional<VideoEncoder::ResolutionBitrateLimits>
226VideoEncoder::EncoderInfo::GetEncoderBitrateLimitsForResolution(
227 int frame_size_pixels) const {
228 std::vector<ResolutionBitrateLimits> bitrate_limits =
229 resolution_bitrate_limits;
230
231 // Sort the list of bitrate limits by resolution.
232 sort(bitrate_limits.begin(), bitrate_limits.end(),
233 [](const ResolutionBitrateLimits& lhs,
234 const ResolutionBitrateLimits& rhs) {
235 return lhs.frame_size_pixels < rhs.frame_size_pixels;
236 });
237
238 for (size_t i = 0; i < bitrate_limits.size(); ++i) {
239 RTC_DCHECK_GE(bitrate_limits[i].min_bitrate_bps, 0);
240 RTC_DCHECK_GE(bitrate_limits[i].min_start_bitrate_bps, 0);
241 RTC_DCHECK_GE(bitrate_limits[i].max_bitrate_bps,
242 bitrate_limits[i].min_bitrate_bps);
243 if (i > 0) {
244 // The bitrate limits aren't expected to decrease with resolution.
245 RTC_DCHECK_GE(bitrate_limits[i].min_bitrate_bps,
246 bitrate_limits[i - 1].min_bitrate_bps);
247 RTC_DCHECK_GE(bitrate_limits[i].min_start_bitrate_bps,
248 bitrate_limits[i - 1].min_start_bitrate_bps);
249 RTC_DCHECK_GE(bitrate_limits[i].max_bitrate_bps,
250 bitrate_limits[i - 1].max_bitrate_bps);
251 }
252
253 if (bitrate_limits[i].frame_size_pixels >= frame_size_pixels) {
254 return absl::optional<ResolutionBitrateLimits>(bitrate_limits[i]);
255 }
256 }
257
258 return absl::nullopt;
259}
260
Erik Språng4c6ca302019-04-08 15:14:01 +0200261VideoEncoder::RateControlParameters::RateControlParameters()
262 : bitrate(VideoBitrateAllocation()),
263 framerate_fps(0.0),
264 bandwidth_allocation(DataRate::Zero()) {}
265
266VideoEncoder::RateControlParameters::RateControlParameters(
267 const VideoBitrateAllocation& bitrate,
Erik Språng16cb8f52019-04-12 13:59:09 +0200268 double framerate_fps)
269 : bitrate(bitrate),
270 framerate_fps(framerate_fps),
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100271 bandwidth_allocation(DataRate::BitsPerSec(bitrate.get_sum_bps())) {}
Erik Språng16cb8f52019-04-12 13:59:09 +0200272
273VideoEncoder::RateControlParameters::RateControlParameters(
274 const VideoBitrateAllocation& bitrate,
Erik Språng4c6ca302019-04-08 15:14:01 +0200275 double framerate_fps,
276 DataRate bandwidth_allocation)
277 : bitrate(bitrate),
278 framerate_fps(framerate_fps),
279 bandwidth_allocation(bandwidth_allocation) {}
280
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200281bool VideoEncoder::RateControlParameters::operator==(
282 const VideoEncoder::RateControlParameters& rhs) const {
283 return std::tie(bitrate, framerate_fps, bandwidth_allocation) ==
284 std::tie(rhs.bitrate, rhs.framerate_fps, rhs.bandwidth_allocation);
285}
286
287bool VideoEncoder::RateControlParameters::operator!=(
288 const VideoEncoder::RateControlParameters& rhs) const {
289 return !(rhs == *this);
290}
291
Erik Språng4c6ca302019-04-08 15:14:01 +0200292VideoEncoder::RateControlParameters::~RateControlParameters() = default;
293
Elad Alon8f01c4e2019-06-28 15:19:43 +0200294void VideoEncoder::SetFecControllerOverride(
295 FecControllerOverride* fec_controller_override) {}
296
Elad Alon370f93a2019-06-11 14:57:57 +0200297int32_t VideoEncoder::InitEncode(const VideoCodec* codec_settings,
298 int32_t number_of_cores,
299 size_t max_payload_size) {
300 const VideoEncoder::Capabilities capabilities(/* loss_notification= */ false);
301 const VideoEncoder::Settings settings(capabilities, number_of_cores,
302 max_payload_size);
303 // In theory, this and the other version of InitEncode() could end up calling
304 // each other in a loop until we get a stack overflow.
305 // In practice, any subclass of VideoEncoder would overload at least one
306 // of these, and we have a TODO in the header file to make this pure virtual.
307 return InitEncode(codec_settings, settings);
308}
309
310int VideoEncoder::InitEncode(const VideoCodec* codec_settings,
311 const VideoEncoder::Settings& settings) {
312 // In theory, this and the other version of InitEncode() could end up calling
313 // each other in a loop until we get a stack overflow.
314 // In practice, any subclass of VideoEncoder would overload at least one
315 // of these, and we have a TODO in the header file to make this pure virtual.
316 return InitEncode(codec_settings, settings.number_of_cores,
317 settings.max_payload_size);
318}
319
Elad Aloncde8ab22019-03-20 11:56:20 +0100320void VideoEncoder::OnPacketLossRateUpdate(float packet_loss_rate) {}
321
322void VideoEncoder::OnRttUpdate(int64_t rtt_ms) {}
323
Elad Alon6c371ca2019-04-04 12:28:51 +0200324void VideoEncoder::OnLossNotification(
325 const LossNotification& loss_notification) {}
326
Erik Språng6ed4f142018-11-26 13:42:39 +0100327// TODO(webrtc:9722): Remove and make pure virtual.
Erik Språnge2fd86a2018-10-24 11:32:39 +0200328VideoEncoder::EncoderInfo VideoEncoder::GetEncoderInfo() const {
Erik Språng6ed4f142018-11-26 13:42:39 +0100329 return EncoderInfo();
Erik Språnge2fd86a2018-10-24 11:32:39 +0200330}
Erik Språng6ed4f142018-11-26 13:42:39 +0100331
mflodman351424e2017-08-10 02:43:14 -0700332} // namespace webrtc