vp8_frame_buffer_controller.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 API_VIDEO_CODECS_VP8_FRAME_BUFFER_CONTROLLER_H_
  11. #define API_VIDEO_CODECS_VP8_FRAME_BUFFER_CONTROLLER_H_
  12. #include <array>
  13. #include <memory>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. #include "api/fec_controller_override.h"
  17. #include "api/video_codecs/video_codec.h"
  18. #include "api/video_codecs/video_encoder.h"
  19. #include "api/video_codecs/vp8_frame_config.h"
  20. namespace webrtc {
  21. // Some notes on the prerequisites of the TemporalLayers interface.
  22. // * Vp8FrameBufferController is not thread safe, synchronization is the
  23. // caller's responsibility.
  24. // * The encoder is assumed to encode all frames in order, and callbacks to
  25. // PopulateCodecSpecific() / OnEncodeDone() must happen in the same order.
  26. //
  27. // This means that in the case of pipelining encoders, it is OK to have a chain
  28. // of calls such as this:
  29. // - NextFrameConfig(timestampA)
  30. // - NextFrameConfig(timestampB)
  31. // - PopulateCodecSpecific(timestampA, ...)
  32. // - NextFrameConfig(timestampC)
  33. // - OnEncodeDone(timestampA, 1234, ...)
  34. // - NextFrameConfig(timestampC)
  35. // - OnEncodeDone(timestampB, 0, ...)
  36. // - OnEncodeDone(timestampC, 1234, ...)
  37. // Note that NextFrameConfig() for a new frame can happen before
  38. // OnEncodeDone() for a previous one, but calls themselves must be both
  39. // synchronized (e.g. run on a task queue) and in order (per type).
  40. //
  41. // TODO(eladalon): Revise comment (referring to PopulateCodecSpecific in this
  42. // context is not very meaningful).
  43. struct CodecSpecificInfo;
  44. // Each member represents an override of the VPX configuration if the optional
  45. // value is set.
  46. struct Vp8EncoderConfig {
  47. struct TemporalLayerConfig {
  48. bool operator!=(const TemporalLayerConfig& other) const {
  49. return ts_number_layers != other.ts_number_layers ||
  50. ts_target_bitrate != other.ts_target_bitrate ||
  51. ts_rate_decimator != other.ts_rate_decimator ||
  52. ts_periodicity != other.ts_periodicity ||
  53. ts_layer_id != other.ts_layer_id;
  54. }
  55. static constexpr size_t kMaxPeriodicity = 16;
  56. static constexpr size_t kMaxLayers = 5;
  57. // Number of active temporal layers. Set to 0 if not used.
  58. uint32_t ts_number_layers;
  59. // Arrays of length |ts_number_layers|, indicating (cumulative) target
  60. // bitrate and rate decimator (e.g. 4 if every 4th frame is in the given
  61. // layer) for each active temporal layer, starting with temporal id 0.
  62. std::array<uint32_t, kMaxLayers> ts_target_bitrate;
  63. std::array<uint32_t, kMaxLayers> ts_rate_decimator;
  64. // The periodicity of the temporal pattern. Set to 0 if not used.
  65. uint32_t ts_periodicity;
  66. // Array of length |ts_periodicity| indicating the sequence of temporal id's
  67. // to assign to incoming frames.
  68. std::array<uint32_t, kMaxPeriodicity> ts_layer_id;
  69. };
  70. absl::optional<TemporalLayerConfig> temporal_layer_config;
  71. // Target bitrate, in bps.
  72. absl::optional<uint32_t> rc_target_bitrate;
  73. // Clamp QP to max. Use 0 to disable clamping.
  74. absl::optional<uint32_t> rc_max_quantizer;
  75. // Error resilience mode.
  76. absl::optional<uint32_t> g_error_resilient;
  77. // If set to true, all previous configuration overrides should be reset.
  78. bool reset_previous_configuration_overrides = false;
  79. };
  80. // This interface defines a way of delegating the logic of buffer management.
  81. // Multiple streams may be controlled by a single controller, demuxing between
  82. // them using stream_index.
  83. class Vp8FrameBufferController {
  84. public:
  85. virtual ~Vp8FrameBufferController() = default;
  86. // Set limits on QP.
  87. // The limits are suggestion-only; the controller is allowed to exceed them.
  88. virtual void SetQpLimits(size_t stream_index, int min_qp, int max_qp) = 0;
  89. // Number of streamed controlled by |this|.
  90. virtual size_t StreamCount() const = 0;
  91. // If this method returns true, the encoder is free to drop frames for
  92. // instance in an effort to uphold encoding bitrate.
  93. // If this return false, the encoder must not drop any frames unless:
  94. // 1. Requested to do so via Vp8FrameConfig.drop_frame
  95. // 2. The frame to be encoded is requested to be a keyframe
  96. // 3. The encoder detected a large overshoot and decided to drop and then
  97. // re-encode the image at a low bitrate. In this case the encoder should
  98. // call OnFrameDropped() once to indicate drop, and then call
  99. // OnEncodeDone() again when the frame has actually been encoded.
  100. virtual bool SupportsEncoderFrameDropping(size_t stream_index) const = 0;
  101. // New target bitrate for a stream (each entry in
  102. // |bitrates_bps| is for another temporal layer).
  103. virtual void OnRatesUpdated(size_t stream_index,
  104. const std::vector<uint32_t>& bitrates_bps,
  105. int framerate_fps) = 0;
  106. // Called by the encoder before encoding a frame. Returns a set of overrides
  107. // the controller wishes to enact in the encoder's configuration.
  108. // If a value is not overridden, previous overrides are still in effect.
  109. // However, if |Vp8EncoderConfig::reset_previous_configuration_overrides|
  110. // is set to |true|, all previous overrides are reset.
  111. virtual Vp8EncoderConfig UpdateConfiguration(size_t stream_index) = 0;
  112. // Returns the recommended VP8 encode flags needed.
  113. // The timestamp may be used as both a time and a unique identifier, and so
  114. // the caller must make sure no two frames use the same timestamp.
  115. // The timestamp uses a 90kHz RTP clock.
  116. // After calling this method, first call the actual encoder with the provided
  117. // frame configuration, and then OnEncodeDone() below.
  118. virtual Vp8FrameConfig NextFrameConfig(size_t stream_index,
  119. uint32_t rtp_timestamp) = 0;
  120. // Called after the encode step is done. |rtp_timestamp| must match the
  121. // parameter use in the NextFrameConfig() call.
  122. // |is_keyframe| must be true iff the encoder decided to encode this frame as
  123. // a keyframe.
  124. // If |info| is not null, the encoder may update |info| with codec specific
  125. // data such as temporal id. |qp| should indicate the frame-level QP this
  126. // frame was encoded at. If the encoder does not support extracting this, |qp|
  127. // should be set to 0.
  128. virtual void OnEncodeDone(size_t stream_index,
  129. uint32_t rtp_timestamp,
  130. size_t size_bytes,
  131. bool is_keyframe,
  132. int qp,
  133. CodecSpecificInfo* info) = 0;
  134. // Called when a frame is dropped by the encoder.
  135. virtual void OnFrameDropped(size_t stream_index, uint32_t rtp_timestamp) = 0;
  136. // Called by the encoder when the packet loss rate changes.
  137. // |packet_loss_rate| runs between 0.0 (no loss) and 1.0 (everything lost).
  138. virtual void OnPacketLossRateUpdate(float packet_loss_rate) = 0;
  139. // Called by the encoder when the round trip time changes.
  140. virtual void OnRttUpdate(int64_t rtt_ms) = 0;
  141. // Called when a loss notification is received.
  142. virtual void OnLossNotification(
  143. const VideoEncoder::LossNotification& loss_notification) = 0;
  144. };
  145. // Interface for a factory of Vp8FrameBufferController instances.
  146. class Vp8FrameBufferControllerFactory {
  147. public:
  148. virtual ~Vp8FrameBufferControllerFactory() = default;
  149. // Clones oneself. (Avoids Vp8FrameBufferControllerFactoryFactory.)
  150. virtual std::unique_ptr<Vp8FrameBufferControllerFactory> Clone() const = 0;
  151. // Create a Vp8FrameBufferController instance.
  152. virtual std::unique_ptr<Vp8FrameBufferController> Create(
  153. const VideoCodec& codec,
  154. const VideoEncoder::Settings& settings,
  155. FecControllerOverride* fec_controller_override) = 0;
  156. };
  157. } // namespace webrtc
  158. #endif // API_VIDEO_CODECS_VP8_FRAME_BUFFER_CONTROLLER_H_