tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

vp8_frame_buffer_controller.h (8233B)


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