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