blob: 7f4c282ded9773dec179e9d0285b10def2790eb8 [file] [log] [blame]
Elad Alonde3360e2019-03-06 21:14:54 +01001/*
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 Aloncde8ab22019-03-20 11:56:20 +010014#include <memory>
Elad Alonde3360e2019-03-06 21:14:54 +010015#include <vector>
16
Elad Aloncde8ab22019-03-20 11:56:20 +010017#include "api/video_codecs/video_codec.h"
Elad Alonde3360e2019-03-06 21:14:54 +010018#include "api/video_codecs/vp8_frame_config.h"
19
20namespace webrtc {
21
22// Some notes on the prerequisites of the TemporalLayers interface.
23// * Vp8FrameBufferController is not thread safe, synchronization is the
24// caller's responsibility.
25// * The encoder is assumed to encode all frames in order, and callbacks to
26// PopulateCodecSpecific() / FrameEncoded() must happen in the same order.
27//
28// This means that in the case of pipelining encoders, it is OK to have a chain
29// of calls such as this:
30// - UpdateLayerConfig(timestampA)
31// - UpdateLayerConfig(timestampB)
32// - PopulateCodecSpecific(timestampA, ...)
33// - UpdateLayerConfig(timestampC)
34// - OnEncodeDone(timestampA, 1234, ...)
35// - UpdateLayerConfig(timestampC)
36// - OnEncodeDone(timestampB, 0, ...)
37// - OnEncodeDone(timestampC, 1234, ...)
38// Note that UpdateLayerConfig() for a new frame can happen before
39// FrameEncoded() for a previous one, but calls themselves must be both
40// synchronized (e.g. run on a task queue) and in order (per type).
41
42struct CodecSpecificInfo;
43
44struct Vp8EncoderConfig {
45 static constexpr size_t kMaxPeriodicity = 16;
46 static constexpr size_t kMaxLayers = 5;
47
48 // Number of active temporal layers. Set to 0 if not used.
49 uint32_t ts_number_layers;
50 // Arrays of length |ts_number_layers|, indicating (cumulative) target bitrate
51 // and rate decimator (e.g. 4 if every 4th frame is in the given layer) for
52 // each active temporal layer, starting with temporal id 0.
53 uint32_t ts_target_bitrate[kMaxLayers];
54 uint32_t ts_rate_decimator[kMaxLayers];
55
56 // The periodicity of the temporal pattern. Set to 0 if not used.
57 uint32_t ts_periodicity;
58 // Array of length |ts_periodicity| indicating the sequence of temporal id's
59 // to assign to incoming frames.
60 uint32_t ts_layer_id[kMaxPeriodicity];
61
62 // Target bitrate, in bps.
63 uint32_t rc_target_bitrate;
64
65 // Clamp QP to min/max. Use 0 to disable clamping.
66 uint32_t rc_min_quantizer;
67 uint32_t rc_max_quantizer;
68};
69
70// This interface defines a way of delegating the logic of buffer management.
Elad Aloncde8ab22019-03-20 11:56:20 +010071// Multiple streams may be controlled by a single controller, demuxing between
72// them using stream_index.
Elad Alonde3360e2019-03-06 21:14:54 +010073class Vp8FrameBufferController {
74 public:
75 virtual ~Vp8FrameBufferController() = default;
76
Elad Aloncde8ab22019-03-20 11:56:20 +010077 // Number of streamed controlled by |this|.
78 virtual size_t StreamCount() const = 0;
79
Elad Alonde3360e2019-03-06 21:14:54 +010080 // If this method returns true, the encoder is free to drop frames for
81 // instance in an effort to uphold encoding bitrate.
82 // If this return false, the encoder must not drop any frames unless:
83 // 1. Requested to do so via Vp8FrameConfig.drop_frame
84 // 2. The frame to be encoded is requested to be a keyframe
85 // 3. The encoded detected a large overshoot and decided to drop and then
86 // re-encode the image at a low bitrate. In this case the encoder should
87 // call OnEncodeDone() once with size = 0 to indicate drop, and then call
88 // OnEncodeDone() again when the frame has actually been encoded.
Elad Aloncde8ab22019-03-20 11:56:20 +010089 virtual bool SupportsEncoderFrameDropping(size_t stream_index) const = 0;
Elad Alonde3360e2019-03-06 21:14:54 +010090
91 // New target bitrate, per temporal layer.
Elad Aloncde8ab22019-03-20 11:56:20 +010092 virtual void OnRatesUpdated(size_t stream_index,
93 const std::vector<uint32_t>& bitrates_bps,
Elad Alonde3360e2019-03-06 21:14:54 +010094 int framerate_fps) = 0;
95
96 // Called by the encoder before encoding a frame. |cfg| contains the current
97 // configuration. If the TemporalLayers instance wishes any part of that
98 // to be changed before the encode step, |cfg| should be changed and then
99 // return true. If false is returned, the encoder will proceed without
100 // updating the configuration.
Elad Aloncde8ab22019-03-20 11:56:20 +0100101 virtual bool UpdateConfiguration(size_t stream_index,
102 Vp8EncoderConfig* cfg) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100103
104 // Returns the recommended VP8 encode flags needed, and moves the temporal
105 // pattern to the next frame.
106 // The timestamp may be used as both a time and a unique identifier, and so
107 // the caller must make sure no two frames use the same timestamp.
108 // The timestamp uses a 90kHz RTP clock.
109 // After calling this method, first call the actual encoder with the provided
110 // frame configuration, and then OnEncodeDone() below.
Elad Aloncde8ab22019-03-20 11:56:20 +0100111 virtual Vp8FrameConfig UpdateLayerConfig(size_t stream_index,
112 uint32_t rtp_timestamp) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100113
114 // Called after the encode step is done. |rtp_timestamp| must match the
115 // parameter use in the UpdateLayerConfig() call.
116 // |is_keyframe| must be true iff the encoder decided to encode this frame as
117 // a keyframe.
118 // If the encoder decided to drop this frame, |size_bytes| must be set to 0,
119 // otherwise it should indicate the size in bytes of the encoded frame.
120 // If |size_bytes| > 0, and |info| is not null, the TemporalLayers
121 // instance my update |info| with codec specific data such as temporal id.
122 // Some fields of this struct may have already been populated by the encoder,
123 // check before overwriting.
124 // If |size_bytes| > 0, |qp| should indicate the frame-level QP this frame was
125 // encoded at. If the encoder does not support extracting this, |qp| should be
126 // set to 0.
Elad Aloncde8ab22019-03-20 11:56:20 +0100127 virtual void OnEncodeDone(size_t stream_index,
128 uint32_t rtp_timestamp,
Elad Alonde3360e2019-03-06 21:14:54 +0100129 size_t size_bytes,
130 bool is_keyframe,
131 int qp,
132 CodecSpecificInfo* info) = 0;
Elad Aloncde8ab22019-03-20 11:56:20 +0100133
134 // Called by the encoder when the packet loss rate changes.
135 // |packet_loss_rate| runs between 0.0 (no loss) and 1.0 (everything lost).
136 virtual void OnPacketLossRateUpdate(float packet_loss_rate) = 0;
137
138 // Called by the encoder when the round trip time changes.
139 virtual void OnRttUpdate(int64_t rtt_ms) = 0;
140};
141
142// Interface for a factory of Vp8FrameBufferController instances.
143class Vp8FrameBufferControllerFactory {
144 public:
145 virtual ~Vp8FrameBufferControllerFactory() = default;
146
147 virtual std::unique_ptr<Vp8FrameBufferController> Create(
148 const VideoCodec& codec) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100149};
150
151} // namespace webrtc
152
153#endif // API_VIDEO_CODECS_VP8_FRAME_BUFFER_CONTROLLER_H_