mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/video_codecs/video_encoder.h" |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
| 15 | #include "rtc_base/checks.h" |
Erik Språng | 7968530 | 2019-11-27 17:26:58 +0100 | [diff] [blame] | 16 | #include "rtc_base/strings/string_builder.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 17 | |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
| 20 | // TODO(mflodman): Add default complexity for VP9 and VP9. |
| 21 | VideoCodecVP8 VideoEncoder::GetDefaultVp8Settings() { |
| 22 | VideoCodecVP8 vp8_settings; |
| 23 | memset(&vp8_settings, 0, sizeof(vp8_settings)); |
| 24 | |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 25 | vp8_settings.numberOfTemporalLayers = 1; |
| 26 | vp8_settings.denoisingOn = true; |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 27 | vp8_settings.automaticResizeOn = false; |
| 28 | vp8_settings.frameDroppingOn = true; |
| 29 | vp8_settings.keyFrameInterval = 3000; |
| 30 | |
| 31 | return vp8_settings; |
| 32 | } |
| 33 | |
| 34 | VideoCodecVP9 VideoEncoder::GetDefaultVp9Settings() { |
| 35 | VideoCodecVP9 vp9_settings; |
| 36 | memset(&vp9_settings, 0, sizeof(vp9_settings)); |
| 37 | |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 38 | vp9_settings.numberOfTemporalLayers = 1; |
| 39 | vp9_settings.denoisingOn = true; |
| 40 | vp9_settings.frameDroppingOn = true; |
| 41 | vp9_settings.keyFrameInterval = 3000; |
| 42 | vp9_settings.adaptiveQpMode = true; |
| 43 | vp9_settings.automaticResizeOn = true; |
| 44 | vp9_settings.numberOfSpatialLayers = 1; |
| 45 | vp9_settings.flexibleMode = false; |
Sergey Silkin | 6a8f30e | 2018-04-26 11:03:49 +0200 | [diff] [blame] | 46 | vp9_settings.interLayerPred = InterLayerPredMode::kOn; |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 47 | |
| 48 | return vp9_settings; |
| 49 | } |
| 50 | |
| 51 | VideoCodecH264 VideoEncoder::GetDefaultH264Settings() { |
| 52 | VideoCodecH264 h264_settings; |
| 53 | memset(&h264_settings, 0, sizeof(h264_settings)); |
| 54 | |
| 55 | h264_settings.frameDroppingOn = true; |
| 56 | h264_settings.keyFrameInterval = 3000; |
Johnny Lee | 1a1c52b | 2019-02-08 14:25:40 -0500 | [diff] [blame] | 57 | h264_settings.numberOfTemporalLayers = 1; |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 58 | |
| 59 | return h264_settings; |
| 60 | } |
| 61 | |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 62 | VideoEncoder::ScalingSettings::ScalingSettings() = default; |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 63 | |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 64 | VideoEncoder::ScalingSettings::ScalingSettings(KOff) : ScalingSettings() {} |
| 65 | |
| 66 | VideoEncoder::ScalingSettings::ScalingSettings(int low, int high) |
| 67 | : thresholds(QpThresholds(low, high)) {} |
| 68 | |
| 69 | VideoEncoder::ScalingSettings::ScalingSettings(int low, |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 70 | int high, |
| 71 | int min_pixels) |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 72 | : thresholds(QpThresholds(low, high)), min_pixels_per_frame(min_pixels) {} |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 73 | |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 74 | VideoEncoder::ScalingSettings::ScalingSettings(const ScalingSettings&) = |
| 75 | default; |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 76 | |
| 77 | VideoEncoder::ScalingSettings::~ScalingSettings() {} |
| 78 | |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 79 | // static |
| 80 | constexpr VideoEncoder::ScalingSettings::KOff |
| 81 | VideoEncoder::ScalingSettings::kOff; |
Erik Språng | dbdd839 | 2019-01-17 15:27:50 +0100 | [diff] [blame] | 82 | // static |
| 83 | constexpr uint8_t VideoEncoder::EncoderInfo::kMaxFramerateFraction; |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 84 | |
Erik Språng | 7968530 | 2019-11-27 17:26:58 +0100 | [diff] [blame] | 85 | bool VideoEncoder::ResolutionBitrateLimits::operator==( |
| 86 | const ResolutionBitrateLimits& rhs) const { |
| 87 | return frame_size_pixels == rhs.frame_size_pixels && |
| 88 | min_start_bitrate_bps == rhs.min_start_bitrate_bps && |
| 89 | min_bitrate_bps == rhs.min_bitrate_bps && |
| 90 | max_bitrate_bps == rhs.max_bitrate_bps; |
| 91 | } |
| 92 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 93 | VideoEncoder::EncoderInfo::EncoderInfo() |
| 94 | : scaling_settings(VideoEncoder::ScalingSettings::kOff), |
| 95 | supports_native_handle(false), |
Erik Språng | d3438aa | 2018-11-08 16:56:43 +0100 | [diff] [blame] | 96 | implementation_name("unknown"), |
Mirta Dvornicic | 897a991 | 2018-11-30 13:12:21 +0100 | [diff] [blame] | 97 | has_trusted_rate_controller(false), |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 98 | is_hardware_accelerated(true), |
Erik Språng | dbdd839 | 2019-01-17 15:27:50 +0100 | [diff] [blame] | 99 | has_internal_source(false), |
| 100 | fps_allocation{absl::InlinedVector<uint8_t, kMaxTemporalStreams>( |
| 101 | 1, |
Erik Språng | f4e0c29 | 2019-10-01 18:50:03 +0200 | [diff] [blame] | 102 | kMaxFramerateFraction)}, |
| 103 | supports_simulcast(false) {} |
Mirta Dvornicic | 897a991 | 2018-11-30 13:12:21 +0100 | [diff] [blame] | 104 | |
| 105 | VideoEncoder::EncoderInfo::EncoderInfo(const EncoderInfo&) = default; |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 106 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 107 | VideoEncoder::EncoderInfo::~EncoderInfo() = default; |
| 108 | |
Erik Språng | 7968530 | 2019-11-27 17:26:58 +0100 | [diff] [blame] | 109 | std::string VideoEncoder::EncoderInfo::ToString() const { |
| 110 | char string_buf[2048]; |
| 111 | rtc::SimpleStringBuilder oss(string_buf); |
| 112 | |
| 113 | oss << "EncoderInfo { " |
| 114 | << "ScalingSettings { "; |
| 115 | if (scaling_settings.thresholds) { |
| 116 | oss << "Thresholds { " |
| 117 | << "low = " << scaling_settings.thresholds->low |
| 118 | << ", high = " << scaling_settings.thresholds->high << "}, "; |
| 119 | } |
| 120 | oss << "min_pixels_per_frame = " << scaling_settings.min_pixels_per_frame |
| 121 | << " }"; |
| 122 | oss << ", supports_native_handle = " << supports_native_handle |
| 123 | << ", implementation_name = '" << implementation_name << "'" |
| 124 | << ", has_trusted_rate_controller = " << has_trusted_rate_controller |
| 125 | << ", is_hardware_accelerated = " << is_hardware_accelerated |
| 126 | << ", has_internal_source = " << has_internal_source |
| 127 | << ", fps_allocation = ["; |
| 128 | bool first = true; |
| 129 | for (size_t i = 0; i < fps_allocation->size(); ++i) { |
| 130 | if (!first) { |
| 131 | oss << ", "; |
| 132 | } |
| 133 | const absl::InlinedVector<uint8_t, kMaxTemporalStreams>& fractions = |
| 134 | fps_allocation[i]; |
| 135 | if (!fractions.empty()) { |
| 136 | first = false; |
| 137 | oss << "[ "; |
| 138 | for (size_t i = 0; i < fractions.size(); ++i) { |
| 139 | if (i > 0) { |
| 140 | oss << ", "; |
| 141 | } |
| 142 | oss << (static_cast<double>(fractions[i]) / kMaxFramerateFraction); |
| 143 | } |
| 144 | oss << "] "; |
| 145 | } |
| 146 | } |
| 147 | oss << "]"; |
| 148 | oss << ", resolution_bitrate_limits = ["; |
| 149 | for (size_t i = 0; i < resolution_bitrate_limits.size(); ++i) { |
| 150 | if (i > 0) { |
| 151 | oss << ", "; |
| 152 | } |
| 153 | ResolutionBitrateLimits l = resolution_bitrate_limits[i]; |
| 154 | oss << "Limits { " |
| 155 | << "frame_size_pixels = " << l.frame_size_pixels |
| 156 | << ", min_start_bitrate_bps = " << l.min_start_bitrate_bps |
| 157 | << ", min_bitrate_bps = " << l.min_bitrate_bps |
| 158 | << ", max_bitrate_bps = " << l.max_bitrate_bps << "} "; |
| 159 | } |
| 160 | oss << "] " |
| 161 | << ", supports_simulcast = " << supports_simulcast << "}"; |
| 162 | return oss.str(); |
| 163 | } |
| 164 | |
| 165 | bool VideoEncoder::EncoderInfo::operator==(const EncoderInfo& rhs) const { |
| 166 | if (scaling_settings.thresholds.has_value() != |
| 167 | rhs.scaling_settings.thresholds.has_value()) { |
| 168 | return false; |
| 169 | } |
| 170 | if (scaling_settings.thresholds.has_value()) { |
| 171 | QpThresholds l = *scaling_settings.thresholds; |
| 172 | QpThresholds r = *rhs.scaling_settings.thresholds; |
| 173 | if (l.low != r.low || l.high != r.high) { |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | if (scaling_settings.min_pixels_per_frame != |
| 178 | rhs.scaling_settings.min_pixels_per_frame) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | if (supports_native_handle != rhs.supports_native_handle || |
| 183 | implementation_name != rhs.implementation_name || |
| 184 | has_trusted_rate_controller != rhs.has_trusted_rate_controller || |
| 185 | is_hardware_accelerated != rhs.is_hardware_accelerated || |
| 186 | has_internal_source != rhs.has_internal_source) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | for (size_t i = 0; i < kMaxSpatialLayers; ++i) { |
| 191 | if (fps_allocation[i] != rhs.fps_allocation[i]) { |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (resolution_bitrate_limits != rhs.resolution_bitrate_limits || |
| 197 | supports_simulcast != rhs.supports_simulcast) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 204 | VideoEncoder::RateControlParameters::RateControlParameters() |
| 205 | : bitrate(VideoBitrateAllocation()), |
| 206 | framerate_fps(0.0), |
| 207 | bandwidth_allocation(DataRate::Zero()) {} |
| 208 | |
| 209 | VideoEncoder::RateControlParameters::RateControlParameters( |
| 210 | const VideoBitrateAllocation& bitrate, |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 211 | double framerate_fps) |
| 212 | : bitrate(bitrate), |
| 213 | framerate_fps(framerate_fps), |
| 214 | bandwidth_allocation(DataRate::bps(bitrate.get_sum_bps())) {} |
| 215 | |
| 216 | VideoEncoder::RateControlParameters::RateControlParameters( |
| 217 | const VideoBitrateAllocation& bitrate, |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 218 | double framerate_fps, |
| 219 | DataRate bandwidth_allocation) |
| 220 | : bitrate(bitrate), |
| 221 | framerate_fps(framerate_fps), |
| 222 | bandwidth_allocation(bandwidth_allocation) {} |
| 223 | |
Evan Shrubsole | 7c079f6 | 2019-09-26 09:55:03 +0200 | [diff] [blame] | 224 | bool VideoEncoder::RateControlParameters::operator==( |
| 225 | const VideoEncoder::RateControlParameters& rhs) const { |
| 226 | return std::tie(bitrate, framerate_fps, bandwidth_allocation) == |
| 227 | std::tie(rhs.bitrate, rhs.framerate_fps, rhs.bandwidth_allocation); |
| 228 | } |
| 229 | |
| 230 | bool VideoEncoder::RateControlParameters::operator!=( |
| 231 | const VideoEncoder::RateControlParameters& rhs) const { |
| 232 | return !(rhs == *this); |
| 233 | } |
| 234 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 235 | VideoEncoder::RateControlParameters::~RateControlParameters() = default; |
| 236 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 237 | void VideoEncoder::SetFecControllerOverride( |
| 238 | FecControllerOverride* fec_controller_override) {} |
| 239 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 240 | int32_t VideoEncoder::InitEncode(const VideoCodec* codec_settings, |
| 241 | int32_t number_of_cores, |
| 242 | size_t max_payload_size) { |
| 243 | const VideoEncoder::Capabilities capabilities(/* loss_notification= */ false); |
| 244 | const VideoEncoder::Settings settings(capabilities, number_of_cores, |
| 245 | max_payload_size); |
| 246 | // In theory, this and the other version of InitEncode() could end up calling |
| 247 | // each other in a loop until we get a stack overflow. |
| 248 | // In practice, any subclass of VideoEncoder would overload at least one |
| 249 | // of these, and we have a TODO in the header file to make this pure virtual. |
| 250 | return InitEncode(codec_settings, settings); |
| 251 | } |
| 252 | |
| 253 | int VideoEncoder::InitEncode(const VideoCodec* codec_settings, |
| 254 | const VideoEncoder::Settings& settings) { |
| 255 | // In theory, this and the other version of InitEncode() could end up calling |
| 256 | // each other in a loop until we get a stack overflow. |
| 257 | // In practice, any subclass of VideoEncoder would overload at least one |
| 258 | // of these, and we have a TODO in the header file to make this pure virtual. |
| 259 | return InitEncode(codec_settings, settings.number_of_cores, |
| 260 | settings.max_payload_size); |
| 261 | } |
| 262 | |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 263 | void VideoEncoder::OnPacketLossRateUpdate(float packet_loss_rate) {} |
| 264 | |
| 265 | void VideoEncoder::OnRttUpdate(int64_t rtt_ms) {} |
| 266 | |
Elad Alon | 6c371ca | 2019-04-04 12:28:51 +0200 | [diff] [blame] | 267 | void VideoEncoder::OnLossNotification( |
| 268 | const LossNotification& loss_notification) {} |
| 269 | |
Erik Språng | 6ed4f14 | 2018-11-26 13:42:39 +0100 | [diff] [blame] | 270 | // TODO(webrtc:9722): Remove and make pure virtual. |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 271 | VideoEncoder::EncoderInfo VideoEncoder::GetEncoderInfo() const { |
Erik Språng | 6ed4f14 | 2018-11-26 13:42:39 +0100 | [diff] [blame] | 272 | return EncoderInfo(); |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 273 | } |
Erik Språng | 6ed4f14 | 2018-11-26 13:42:39 +0100 | [diff] [blame] | 274 | |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 275 | } // namespace webrtc |