blob: 70223a42096545ac85727e8cce31ee7850783047 [file] [log] [blame]
Erik Språng8abd56c2018-10-01 18:47:03 +02001/*
2 * Copyright (c) 2018 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
Erik Språng4529fbc2018-10-12 10:30:31 +020011#ifndef API_VIDEO_CODECS_VP8_TEMPORAL_LAYERS_H_
12#define API_VIDEO_CODECS_VP8_TEMPORAL_LAYERS_H_
Erik Språng8abd56c2018-10-01 18:47:03 +020013
14#include <memory>
15#include <vector>
16
17namespace webrtc {
18
19// Some notes on the prerequisites of the TemporalLayers interface.
Erik Språng4529fbc2018-10-12 10:30:31 +020020// * Vp8TemporalLayers is not thread safe, synchronization is the caller's
21// responsibility.
Erik Språng8abd56c2018-10-01 18:47:03 +020022// * The encoder is assumed to encode all frames in order, and callbacks to
23// PopulateCodecSpecific() / FrameEncoded() must happen in the same order.
24//
25// This means that in the case of pipelining encoders, it is OK to have a chain
26// of calls such as this:
27// - UpdateLayerConfig(timestampA)
28// - UpdateLayerConfig(timestampB)
29// - PopulateCodecSpecific(timestampA, ...)
30// - UpdateLayerConfig(timestampC)
Erik Språng59021ba2018-10-03 11:05:16 +020031// - OnEncodeDone(timestampA, 1234, ...)
32// - UpdateLayerConfig(timestampC)
33// - OnEncodeDone(timestampB, 0, ...)
34// - OnEncodeDone(timestampC, 1234, ...)
Erik Språng8abd56c2018-10-01 18:47:03 +020035// Note that UpdateLayerConfig() for a new frame can happen before
36// FrameEncoded() for a previous one, but calls themselves must be both
37// synchronized (e.g. run on a task queue) and in order (per type).
38
Erik Språng4529fbc2018-10-12 10:30:31 +020039// Two different flavors of temporal layers are currently available:
40// kFixedPattern uses a fixed repeating pattern of 1-4 layers.
41// kBitrateDynamic can allocate frames dynamically to 1 or 2 layers, based on
42// the bitrate produced.
43enum class Vp8TemporalLayersType { kFixedPattern, kBitrateDynamic };
Erik Språng8abd56c2018-10-01 18:47:03 +020044
45struct CodecSpecificInfoVP8;
Erik Språng8abd56c2018-10-01 18:47:03 +020046
47struct Vp8EncoderConfig {
48 static constexpr size_t kMaxPeriodicity = 16;
49 static constexpr size_t kMaxLayers = 5;
50
51 // Number of active temporal layers. Set to 0 if not used.
52 uint32_t ts_number_layers;
53 // Arrays of length |ts_number_layers|, indicating (cumulative) target bitrate
54 // and rate decimator (e.g. 4 if every 4th frame is in the given layer) for
55 // each active temporal layer, starting with temporal id 0.
56 uint32_t ts_target_bitrate[kMaxLayers];
57 uint32_t ts_rate_decimator[kMaxLayers];
58
59 // The periodicity of the temporal pattern. Set to 0 if not used.
60 uint32_t ts_periodicity;
61 // Array of length |ts_periodicity| indicating the sequence of temporal id's
62 // to assign to incoming frames.
63 uint32_t ts_layer_id[kMaxPeriodicity];
64
65 // Target bitrate, in bps.
66 uint32_t rc_target_bitrate;
67
68 // Clamp QP to min/max. Use 0 to disable clamping.
69 uint32_t rc_min_quantizer;
70 uint32_t rc_max_quantizer;
71};
72
Erik Språng4529fbc2018-10-12 10:30:31 +020073// Defined bit-maskable reference to the three buffers available in VP8.
74enum class Vp8BufferReference : uint8_t {
75 kNone = 0,
76 kLast = 1,
77 kGolden = 2,
78 kAltref = 4
79};
80
Erik Språng8abd56c2018-10-01 18:47:03 +020081// This interface defines a way of getting the encoder settings needed to
Erik Språng4529fbc2018-10-12 10:30:31 +020082// realize a temporal layer structure.
83class Vp8TemporalLayers {
Erik Språng8abd56c2018-10-01 18:47:03 +020084 public:
85 enum BufferFlags : int {
86 kNone = 0,
87 kReference = 1,
88 kUpdate = 2,
89 kReferenceAndUpdate = kReference | kUpdate,
90 };
91 enum FreezeEntropy { kFreezeEntropy };
92
93 struct FrameConfig {
94 FrameConfig();
95
96 FrameConfig(BufferFlags last, BufferFlags golden, BufferFlags arf);
97 FrameConfig(BufferFlags last,
98 BufferFlags golden,
99 BufferFlags arf,
100 FreezeEntropy);
101
102 bool drop_frame;
103 BufferFlags last_buffer_flags;
104 BufferFlags golden_buffer_flags;
105 BufferFlags arf_buffer_flags;
106
107 // The encoder layer ID is used to utilize the correct bitrate allocator
108 // inside the encoder. It does not control references nor determine which
109 // "actual" temporal layer this is. The packetizer temporal index determines
110 // which layer the encoded frame should be packetized into.
111 // Normally these are the same, but current temporal-layer strategies for
112 // screenshare use one bitrate allocator for all layers, but attempt to
113 // packetize / utilize references to split a stream into multiple layers,
114 // with different quantizer settings, to hit target bitrate.
115 // TODO(pbos): Screenshare layers are being reconsidered at the time of
116 // writing, we might be able to remove this distinction, and have a temporal
117 // layer imply both (the normal case).
118 int encoder_layer_id;
119 int packetizer_temporal_idx;
120
121 bool layer_sync;
122
123 bool freeze_entropy;
124
125 // Indicates in which order the encoder should search the reference buffers
126 // when doing motion prediction. Set to kNone to use unspecified order. Any
127 // buffer indicated here must not have the corresponding no_ref bit set.
128 // If all three buffers can be reference, the one not listed here should be
129 // searched last.
130 Vp8BufferReference first_reference;
131 Vp8BufferReference second_reference;
132
Erik Språng8abd56c2018-10-01 18:47:03 +0200133 private:
134 FrameConfig(BufferFlags last,
135 BufferFlags golden,
136 BufferFlags arf,
137 bool freeze_entropy);
138 };
139
Erik Språng4529fbc2018-10-12 10:30:31 +0200140 virtual ~Vp8TemporalLayers() = default;
Erik Språng8abd56c2018-10-01 18:47:03 +0200141
Erik Språng59021ba2018-10-03 11:05:16 +0200142 // If this method returns true, the encoder is free to drop frames for
143 // instance in an effort to uphold encoding bitrate.
144 // If this return false, the encoder must not drop any frames unless:
145 // 1. Requested to do so via FrameConfig.drop_frame
146 // 2. The frame to be encoded is requested to be a keyframe
147 // 3. The encoded detected a large overshoot and decided to drop and then
148 // re-encode the image at a low bitrate. In this case the encoder should
149 // call OnEncodeDone() once with size = 0 to indicate drop, and then call
150 // OnEncodeDone() again when the frame has actually been encoded.
Erik Språng8abd56c2018-10-01 18:47:03 +0200151 virtual bool SupportsEncoderFrameDropping() const = 0;
152
153 // New target bitrate, per temporal layer.
154 virtual void OnRatesUpdated(const std::vector<uint32_t>& bitrates_bps,
155 int framerate_fps) = 0;
156
Erik Språng59021ba2018-10-03 11:05:16 +0200157 // Called by the encoder before encoding a frame. |cfg| contains the current
158 // configuration. If the TemporalLayers instance wishes any part of that
159 // to be changed before the encode step, |cfg| should be changed and then
160 // return true. If false is returned, the encoder will proceed without
161 // updating the configuration.
Erik Språng8abd56c2018-10-01 18:47:03 +0200162 virtual bool UpdateConfiguration(Vp8EncoderConfig* cfg) = 0;
163
164 // Returns the recommended VP8 encode flags needed, and moves the temporal
165 // pattern to the next frame.
166 // The timestamp may be used as both a time and a unique identifier, and so
167 // the caller must make sure no two frames use the same timestamp.
168 // The timestamp uses a 90kHz RTP clock.
Erik Språng59021ba2018-10-03 11:05:16 +0200169 // After calling this method, first call the actual encoder with the provided
170 // frame configuration, and then OnEncodeDone() below.
Erik Språng8abd56c2018-10-01 18:47:03 +0200171 virtual FrameConfig UpdateLayerConfig(uint32_t rtp_timestamp) = 0;
172
Erik Språng59021ba2018-10-03 11:05:16 +0200173 // Called after the encode step is done. |rtp_timestamp| must match the
174 // parameter use in the UpdateLayerConfig() call.
175 // |is_keyframe| must be true iff the encoder decided to encode this frame as
176 // a keyframe.
177 // If the encoder decided to drop this frame, |size_bytes| must be set to 0,
178 // otherwise it should indicate the size in bytes of the encoded frame.
179 // If |size_bytes| > 0, and |vp8_info| is not null, the TemporalLayers
180 // instance my update |vp8_info| with codec specific data such as temporal id.
181 // Some fields of this struct may have already been populated by the encoder,
182 // check before overwriting.
183 // If |size_bytes| > 0, |qp| should indicate the frame-level QP this frame was
184 // encoded at. If the encoder does not support extracting this, |qp| should be
185 // set to 0.
186 virtual void OnEncodeDone(uint32_t rtp_timestamp,
Erik Språng8abd56c2018-10-01 18:47:03 +0200187 size_t size_bytes,
Erik Språng59021ba2018-10-03 11:05:16 +0200188 bool is_keyframe,
189 int qp,
190 CodecSpecificInfoVP8* vp8_info) = 0;
Erik Språng8abd56c2018-10-01 18:47:03 +0200191};
192
193} // namespace webrtc
194
Erik Språng4529fbc2018-10-12 10:30:31 +0200195#endif // API_VIDEO_CODECS_VP8_TEMPORAL_LAYERS_H_