encoder_bitrate_adjuster.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2019 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. #ifndef VIDEO_ENCODER_BITRATE_ADJUSTER_H_
  11. #define VIDEO_ENCODER_BITRATE_ADJUSTER_H_
  12. #include <memory>
  13. #include "api/video/encoded_image.h"
  14. #include "api/video/video_bitrate_allocation.h"
  15. #include "api/video_codecs/video_encoder.h"
  16. #include "video/encoder_overshoot_detector.h"
  17. namespace webrtc {
  18. class EncoderBitrateAdjuster {
  19. public:
  20. // Size of sliding window used to track overshoot rate.
  21. static constexpr int64_t kWindowSizeMs = 3000;
  22. // Minimum number of frames since last layout change required to trust the
  23. // overshoot statistics. Otherwise falls back to default utilization.
  24. // By layout change, we mean any spatial/temporal layer being either enabled
  25. // or disabled.
  26. static constexpr size_t kMinFramesSinceLayoutChange = 30;
  27. // Default utilization, before reliable metrics are available, is set to 20%
  28. // overshoot. This is conservative so that badly misbehaving encoders don't
  29. // build too much queue at the very start.
  30. static constexpr double kDefaultUtilizationFactor = 1.2;
  31. explicit EncoderBitrateAdjuster(const VideoCodec& codec_settings);
  32. ~EncoderBitrateAdjuster();
  33. // Adjusts the given rate allocation to make it paceable within the target
  34. // rates.
  35. VideoBitrateAllocation AdjustRateAllocation(
  36. const VideoEncoder::RateControlParameters& rates);
  37. // Updated overuse detectors with data about the encoder, specifically about
  38. // the temporal layer frame rate allocation.
  39. void OnEncoderInfo(const VideoEncoder::EncoderInfo& encoder_info);
  40. // Updates the overuse detectors according to the encoded image size.
  41. void OnEncodedFrame(const EncodedImage& encoded_image, int temporal_index);
  42. void Reset();
  43. private:
  44. const bool utilize_bandwidth_headroom_;
  45. VideoEncoder::RateControlParameters current_rate_control_parameters_;
  46. // FPS allocation of temporal layers, per spatial layer. Represented as a Q8
  47. // fraction; 0 = 0%, 255 = 100%. See VideoEncoder::EncoderInfo.fps_allocation.
  48. absl::InlinedVector<uint8_t, kMaxTemporalStreams>
  49. current_fps_allocation_[kMaxSpatialLayers];
  50. // Frames since layout was changed, mean that any spatial or temporal layer
  51. // was either disabled or enabled.
  52. size_t frames_since_layout_change_;
  53. std::unique_ptr<EncoderOvershootDetector>
  54. overshoot_detectors_[kMaxSpatialLayers][kMaxTemporalStreams];
  55. // Minimum bitrates allowed, per spatial layer.
  56. uint32_t min_bitrates_bps_[kMaxSpatialLayers];
  57. };
  58. } // namespace webrtc
  59. #endif // VIDEO_ENCODER_BITRATE_ADJUSTER_H_