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