| 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 "api/video_codecs/video_encoder.h" |
| 12 |
| 13 | #include <string.h> |
| 14 | #include <algorithm> |
| 15 |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/strings/string_builder.h" |
| 18 |
| 19 | namespace webrtc { |
| 20 |
| 21 | EncodedImageCallback::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 |
| 28 | EncodedImageCallback::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 |
| 34 | // TODO(mflodman): Add default complexity for VP9 and VP9. |
| 35 | VideoCodecVP8 VideoEncoder::GetDefaultVp8Settings() { |
| 36 | VideoCodecVP8 vp8_settings; |
| 37 | memset(&vp8_settings, 0, sizeof(vp8_settings)); |
| 38 |
| 39 | vp8_settings.numberOfTemporalLayers = 1; |
| 40 | vp8_settings.denoisingOn = true; |
| 41 | vp8_settings.automaticResizeOn = false; |
| 42 | vp8_settings.frameDroppingOn = true; |
| 43 | vp8_settings.keyFrameInterval = 3000; |
| 44 |
| 45 | return vp8_settings; |
| 46 | } |
| 47 |
| 48 | VideoCodecVP9 VideoEncoder::GetDefaultVp9Settings() { |
| 49 | VideoCodecVP9 vp9_settings; |
| 50 | memset(&vp9_settings, 0, sizeof(vp9_settings)); |
| 51 |
| 52 | 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; |
| 60 | vp9_settings.interLayerPred = InterLayerPredMode::kOn; |
| 61 |
| 62 | return vp9_settings; |
| 63 | } |
| 64 |
| 65 | VideoCodecH264 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; |
| 71 | h264_settings.numberOfTemporalLayers = 1; |
| 72 |
| 73 | return h264_settings; |
| 74 | } |
| 75 |
| 76 | #ifndef DISABLE_H265 |
| 77 | VideoCodecH265 VideoEncoder::GetDefaultH265Settings() { |
| 78 | VideoCodecH265 h265_settings; |
| 79 | memset(&h265_settings, 0, sizeof(h265_settings)); |
| 80 |
| 81 | // h265_settings.profile = kProfileBase; |
| 82 | h265_settings.frameDroppingOn = true; |
| 83 | h265_settings.keyFrameInterval = 3000; |
| 84 | h265_settings.spsData = nullptr; |
| 85 | h265_settings.spsLen = 0; |
| 86 | h265_settings.ppsData = nullptr; |
| 87 | h265_settings.ppsLen = 0; |
| 88 |
| 89 | return h265_settings; |
| 90 | } |
| 91 | #endif |
| 92 |
| 93 | VideoEncoder::ScalingSettings::ScalingSettings() = default; |
| 94 |
| 95 | VideoEncoder::ScalingSettings::ScalingSettings(KOff) : ScalingSettings() {} |
| 96 |
| 97 | VideoEncoder::ScalingSettings::ScalingSettings(int low, int high) |
| 98 | : thresholds(QpThresholds(low, high)) {} |
| 99 |
| 100 | VideoEncoder::ScalingSettings::ScalingSettings(int low, |
| 101 | int high, |
| 102 | int min_pixels) |
| 103 | : thresholds(QpThresholds(low, high)), min_pixels_per_frame(min_pixels) {} |
| 104 |
| 105 | VideoEncoder::ScalingSettings::ScalingSettings(const ScalingSettings&) = |
| 106 | default; |
| 107 |
| 108 | VideoEncoder::ScalingSettings::~ScalingSettings() {} |
| 109 |
| 110 | // static |
| 111 | constexpr VideoEncoder::ScalingSettings::KOff |
| 112 | VideoEncoder::ScalingSettings::kOff; |
| 113 | // static |
| 114 | constexpr uint8_t VideoEncoder::EncoderInfo::kMaxFramerateFraction; |
| 115 |
| 116 | bool VideoEncoder::ResolutionBitrateLimits::operator==( |
| 117 | const ResolutionBitrateLimits& rhs) const { |
| 118 | return frame_size_pixels == rhs.frame_size_pixels && |
| 119 | min_start_bitrate_bps == rhs.min_start_bitrate_bps && |
| 120 | min_bitrate_bps == rhs.min_bitrate_bps && |
| 121 | max_bitrate_bps == rhs.max_bitrate_bps; |
| 122 | } |
| 123 |
| 124 | VideoEncoder::EncoderInfo::EncoderInfo() |
| 125 | : scaling_settings(VideoEncoder::ScalingSettings::kOff), |
| 126 | requested_resolution_alignment(1), |
| 127 | supports_native_handle(false), |
| 128 | implementation_name("unknown"), |
| 129 | has_trusted_rate_controller(false), |
| 130 | is_hardware_accelerated(true), |
| 131 | has_internal_source(false), |
| 132 | fps_allocation{absl::InlinedVector<uint8_t, kMaxTemporalStreams>( |
| 133 | 1, |
| 134 | kMaxFramerateFraction)}, |
| 135 | supports_simulcast(false) {} |
| 136 |
| 137 | VideoEncoder::EncoderInfo::EncoderInfo(const EncoderInfo&) = default; |
| 138 |
| 139 | VideoEncoder::EncoderInfo::~EncoderInfo() = default; |
| 140 |
| 141 | std::string VideoEncoder::EncoderInfo::ToString() const { |
| 142 | char string_buf[2048]; |
| 143 | rtc::SimpleStringBuilder oss(string_buf); |
| 144 |
| 145 | oss << "EncoderInfo { " |
| 146 | "ScalingSettings { "; |
| 147 | if (scaling_settings.thresholds) { |
| 148 | oss << "Thresholds { " |
| 149 | "low = " |
| 150 | << scaling_settings.thresholds->low |
| 151 | << ", high = " << scaling_settings.thresholds->high << "}, "; |
| 152 | } |
| 153 | oss << "min_pixels_per_frame = " << scaling_settings.min_pixels_per_frame |
| 154 | << " }"; |
| 155 | oss << ", requested_resolution_alignment = " << requested_resolution_alignment |
| 156 | << ", supports_native_handle = " << supports_native_handle |
| 157 | << ", implementation_name = '" << implementation_name |
| 158 | << "'" |
| 159 | ", has_trusted_rate_controller = " |
| 160 | << has_trusted_rate_controller |
| 161 | << ", is_hardware_accelerated = " << is_hardware_accelerated |
| 162 | << ", has_internal_source = " << has_internal_source |
| 163 | << ", fps_allocation = ["; |
| 164 | bool first = true; |
| 165 | for (size_t i = 0; i < fps_allocation->size(); ++i) { |
| 166 | if (!first) { |
| 167 | oss << ", "; |
| 168 | } |
| 169 | const absl::InlinedVector<uint8_t, kMaxTemporalStreams>& fractions = |
| 170 | fps_allocation[i]; |
| 171 | if (!fractions.empty()) { |
| 172 | first = false; |
| 173 | oss << "[ "; |
| 174 | for (size_t i = 0; i < fractions.size(); ++i) { |
| 175 | if (i > 0) { |
| 176 | oss << ", "; |
| 177 | } |
| 178 | oss << (static_cast<double>(fractions[i]) / kMaxFramerateFraction); |
| 179 | } |
| 180 | oss << "] "; |
| 181 | } |
| 182 | } |
| 183 | oss << "]"; |
| 184 | oss << ", resolution_bitrate_limits = ["; |
| 185 | for (size_t i = 0; i < resolution_bitrate_limits.size(); ++i) { |
| 186 | if (i > 0) { |
| 187 | oss << ", "; |
| 188 | } |
| 189 | ResolutionBitrateLimits l = resolution_bitrate_limits[i]; |
| 190 | oss << "Limits { " |
| 191 | "frame_size_pixels = " |
| 192 | << l.frame_size_pixels |
| 193 | << ", min_start_bitrate_bps = " << l.min_start_bitrate_bps |
| 194 | << ", min_bitrate_bps = " << l.min_bitrate_bps |
| 195 | << ", max_bitrate_bps = " << l.max_bitrate_bps << "} "; |
| 196 | } |
| 197 | oss << "] " |
| 198 | ", supports_simulcast = " |
| 199 | << supports_simulcast << "}"; |
| 200 | return oss.str(); |
| 201 | } |
| 202 |
| 203 | bool VideoEncoder::EncoderInfo::operator==(const EncoderInfo& rhs) const { |
| 204 | if (scaling_settings.thresholds.has_value() != |
| 205 | rhs.scaling_settings.thresholds.has_value()) { |
| 206 | return false; |
| 207 | } |
| 208 | if (scaling_settings.thresholds.has_value()) { |
| 209 | QpThresholds l = *scaling_settings.thresholds; |
| 210 | QpThresholds r = *rhs.scaling_settings.thresholds; |
| 211 | if (l.low != r.low || l.high != r.high) { |
| 212 | return false; |
| 213 | } |
| 214 | } |
| 215 | if (scaling_settings.min_pixels_per_frame != |
| 216 | rhs.scaling_settings.min_pixels_per_frame) { |
| 217 | return false; |
| 218 | } |
| 219 |
| 220 | if (supports_native_handle != rhs.supports_native_handle || |
| 221 | implementation_name != rhs.implementation_name || |
| 222 | has_trusted_rate_controller != rhs.has_trusted_rate_controller || |
| 223 | is_hardware_accelerated != rhs.is_hardware_accelerated || |
| 224 | has_internal_source != rhs.has_internal_source) { |
| 225 | return false; |
| 226 | } |
| 227 |
| 228 | for (size_t i = 0; i < kMaxSpatialLayers; ++i) { |
| 229 | if (fps_allocation[i] != rhs.fps_allocation[i]) { |
| 230 | return false; |
| 231 | } |
| 232 | } |
| 233 |
| 234 | if (resolution_bitrate_limits != rhs.resolution_bitrate_limits || |
| 235 | supports_simulcast != rhs.supports_simulcast) { |
| 236 | return false; |
| 237 | } |
| 238 |
| 239 | return true; |
| 240 | } |
| 241 |
| 242 | absl::optional<VideoEncoder::ResolutionBitrateLimits> |
| 243 | VideoEncoder::EncoderInfo::GetEncoderBitrateLimitsForResolution( |
| 244 | int frame_size_pixels) const { |
| 245 | std::vector<ResolutionBitrateLimits> bitrate_limits = |
| 246 | resolution_bitrate_limits; |
| 247 |
| 248 | // Sort the list of bitrate limits by resolution. |
| 249 | sort(bitrate_limits.begin(), bitrate_limits.end(), |
| 250 | [](const ResolutionBitrateLimits& lhs, |
| 251 | const ResolutionBitrateLimits& rhs) { |
| 252 | return lhs.frame_size_pixels < rhs.frame_size_pixels; |
| 253 | }); |
| 254 |
| 255 | for (size_t i = 0; i < bitrate_limits.size(); ++i) { |
| 256 | RTC_DCHECK_GE(bitrate_limits[i].min_bitrate_bps, 0); |
| 257 | RTC_DCHECK_GE(bitrate_limits[i].min_start_bitrate_bps, 0); |
| 258 | RTC_DCHECK_GE(bitrate_limits[i].max_bitrate_bps, |
| 259 | bitrate_limits[i].min_bitrate_bps); |
| 260 | if (i > 0) { |
| 261 | // The bitrate limits aren't expected to decrease with resolution. |
| 262 | RTC_DCHECK_GE(bitrate_limits[i].min_bitrate_bps, |
| 263 | bitrate_limits[i - 1].min_bitrate_bps); |
| 264 | RTC_DCHECK_GE(bitrate_limits[i].min_start_bitrate_bps, |
| 265 | bitrate_limits[i - 1].min_start_bitrate_bps); |
| 266 | RTC_DCHECK_GE(bitrate_limits[i].max_bitrate_bps, |
| 267 | bitrate_limits[i - 1].max_bitrate_bps); |
| 268 | } |
| 269 |
| 270 | if (bitrate_limits[i].frame_size_pixels >= frame_size_pixels) { |
| 271 | return absl::optional<ResolutionBitrateLimits>(bitrate_limits[i]); |
| 272 | } |
| 273 | } |
| 274 |
| 275 | return absl::nullopt; |
| 276 | } |
| 277 |
| 278 | VideoEncoder::RateControlParameters::RateControlParameters() |
| 279 | : bitrate(VideoBitrateAllocation()), |
| 280 | framerate_fps(0.0), |
| 281 | bandwidth_allocation(DataRate::Zero()) {} |
| 282 |
| 283 | VideoEncoder::RateControlParameters::RateControlParameters( |
| 284 | const VideoBitrateAllocation& bitrate, |
| 285 | double framerate_fps) |
| 286 | : bitrate(bitrate), |
| 287 | framerate_fps(framerate_fps), |
| 288 | bandwidth_allocation(DataRate::BitsPerSec(bitrate.get_sum_bps())) {} |
| 289 |
| 290 | VideoEncoder::RateControlParameters::RateControlParameters( |
| 291 | const VideoBitrateAllocation& bitrate, |
| 292 | double framerate_fps, |
| 293 | DataRate bandwidth_allocation) |
| 294 | : bitrate(bitrate), |
| 295 | framerate_fps(framerate_fps), |
| 296 | bandwidth_allocation(bandwidth_allocation) {} |
| 297 |
| 298 | bool VideoEncoder::RateControlParameters::operator==( |
| 299 | const VideoEncoder::RateControlParameters& rhs) const { |
| 300 | return std::tie(bitrate, framerate_fps, bandwidth_allocation) == |
| 301 | std::tie(rhs.bitrate, rhs.framerate_fps, rhs.bandwidth_allocation); |
| 302 | } |
| 303 |
| 304 | bool VideoEncoder::RateControlParameters::operator!=( |
| 305 | const VideoEncoder::RateControlParameters& rhs) const { |
| 306 | return !(rhs == *this); |
| 307 | } |
| 308 |
| 309 | VideoEncoder::RateControlParameters::~RateControlParameters() = default; |
| 310 |
| 311 | void VideoEncoder::SetFecControllerOverride( |
| 312 | FecControllerOverride* fec_controller_override) {} |
| 313 |
| 314 | int32_t VideoEncoder::InitEncode(const VideoCodec* codec_settings, |
| 315 | int32_t number_of_cores, |
| 316 | size_t max_payload_size) { |
| 317 | const VideoEncoder::Capabilities capabilities(/* loss_notification= */ false); |
| 318 | const VideoEncoder::Settings settings(capabilities, number_of_cores, |
| 319 | max_payload_size); |
| 320 | // In theory, this and the other version of InitEncode() could end up calling |
| 321 | // each other in a loop until we get a stack overflow. |
| 322 | // In practice, any subclass of VideoEncoder would overload at least one |
| 323 | // of these, and we have a TODO in the header file to make this pure virtual. |
| 324 | return InitEncode(codec_settings, settings); |
| 325 | } |
| 326 |
| 327 | int VideoEncoder::InitEncode(const VideoCodec* codec_settings, |
| 328 | const VideoEncoder::Settings& settings) { |
| 329 | // In theory, this and the other version of InitEncode() could end up calling |
| 330 | // each other in a loop until we get a stack overflow. |
| 331 | // In practice, any subclass of VideoEncoder would overload at least one |
| 332 | // of these, and we have a TODO in the header file to make this pure virtual. |
| 333 | return InitEncode(codec_settings, settings.number_of_cores, |
| 334 | settings.max_payload_size); |
| 335 | } |
| 336 |
| 337 | void VideoEncoder::OnPacketLossRateUpdate(float packet_loss_rate) {} |
| 338 |
| 339 | void VideoEncoder::OnRttUpdate(int64_t rtt_ms) {} |
| 340 |
| 341 | void VideoEncoder::OnLossNotification( |
| 342 | const LossNotification& loss_notification) {} |
| 343 |
| 344 | // TODO(webrtc:9722): Remove and make pure virtual. |
| 345 | VideoEncoder::EncoderInfo VideoEncoder::GetEncoderInfo() const { |
| 346 | return EncoderInfo(); |
| 347 | } |
| 348 |
| 349 | } // namespace webrtc |
| 350 |