blob: f3d2b1a6ce68be3c44d65402ef9602547eb8192a [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 Alon6c371ca2019-04-04 12:28:51 +020017#include "absl/types/optional.h"
Elad Aloncde8ab22019-03-20 11:56:20 +010018#include "api/video_codecs/video_codec.h"
Elad Alon6c371ca2019-04-04 12:28:51 +020019#include "api/video_codecs/video_encoder.h"
Elad Alonde3360e2019-03-06 21:14:54 +010020#include "api/video_codecs/vp8_frame_config.h"
21
22namespace 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
28// PopulateCodecSpecific() / FrameEncoded() must happen in the same order.
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
41// FrameEncoded() for a previous one, but calls themselves must be both
42// synchronized (e.g. run on a task queue) and in order (per type).
43
44struct CodecSpecificInfo;
45
46struct Vp8EncoderConfig {
47 static constexpr size_t kMaxPeriodicity = 16;
48 static constexpr size_t kMaxLayers = 5;
49
50 // Number of active temporal layers. Set to 0 if not used.
51 uint32_t ts_number_layers;
52 // Arrays of length |ts_number_layers|, indicating (cumulative) target bitrate
53 // and rate decimator (e.g. 4 if every 4th frame is in the given layer) for
54 // each active temporal layer, starting with temporal id 0.
55 uint32_t ts_target_bitrate[kMaxLayers];
56 uint32_t ts_rate_decimator[kMaxLayers];
57
58 // The periodicity of the temporal pattern. Set to 0 if not used.
59 uint32_t ts_periodicity;
60 // Array of length |ts_periodicity| indicating the sequence of temporal id's
61 // to assign to incoming frames.
62 uint32_t ts_layer_id[kMaxPeriodicity];
63
64 // Target bitrate, in bps.
65 uint32_t rc_target_bitrate;
66
67 // Clamp QP to min/max. Use 0 to disable clamping.
68 uint32_t rc_min_quantizer;
69 uint32_t rc_max_quantizer;
70};
71
72// This interface defines a way of delegating the logic of buffer management.
Elad Aloncde8ab22019-03-20 11:56:20 +010073// Multiple streams may be controlled by a single controller, demuxing between
74// them using stream_index.
Elad Alonde3360e2019-03-06 21:14:54 +010075class Vp8FrameBufferController {
76 public:
77 virtual ~Vp8FrameBufferController() = default;
78
Elad Aloncde8ab22019-03-20 11:56:20 +010079 // Number of streamed controlled by |this|.
80 virtual size_t StreamCount() const = 0;
81
Elad Alonde3360e2019-03-06 21:14:54 +010082 // If this method returns true, the encoder is free to drop frames for
83 // instance in an effort to uphold encoding bitrate.
84 // If this return false, the encoder must not drop any frames unless:
85 // 1. Requested to do so via Vp8FrameConfig.drop_frame
86 // 2. The frame to be encoded is requested to be a keyframe
87 // 3. The encoded detected a large overshoot and decided to drop and then
88 // re-encode the image at a low bitrate. In this case the encoder should
89 // call OnEncodeDone() once with size = 0 to indicate drop, and then call
90 // OnEncodeDone() again when the frame has actually been encoded.
Elad Aloncde8ab22019-03-20 11:56:20 +010091 virtual bool SupportsEncoderFrameDropping(size_t stream_index) const = 0;
Elad Alonde3360e2019-03-06 21:14:54 +010092
93 // New target bitrate, per temporal layer.
Elad Aloncde8ab22019-03-20 11:56:20 +010094 virtual void OnRatesUpdated(size_t stream_index,
95 const std::vector<uint32_t>& bitrates_bps,
Elad Alonde3360e2019-03-06 21:14:54 +010096 int framerate_fps) = 0;
97
98 // Called by the encoder before encoding a frame. |cfg| contains the current
99 // configuration. If the TemporalLayers instance wishes any part of that
100 // to be changed before the encode step, |cfg| should be changed and then
101 // return true. If false is returned, the encoder will proceed without
102 // updating the configuration.
Elad Aloncde8ab22019-03-20 11:56:20 +0100103 virtual bool UpdateConfiguration(size_t stream_index,
104 Vp8EncoderConfig* cfg) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100105
106 // Returns the recommended VP8 encode flags needed, and moves the temporal
107 // pattern to the next frame.
108 // The timestamp may be used as both a time and a unique identifier, and so
109 // the caller must make sure no two frames use the same timestamp.
110 // The timestamp uses a 90kHz RTP clock.
111 // After calling this method, first call the actual encoder with the provided
112 // frame configuration, and then OnEncodeDone() below.
Elad Aloncde8ab22019-03-20 11:56:20 +0100113 virtual Vp8FrameConfig UpdateLayerConfig(size_t stream_index,
114 uint32_t rtp_timestamp) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100115
116 // Called after the encode step is done. |rtp_timestamp| must match the
117 // parameter use in the UpdateLayerConfig() call.
118 // |is_keyframe| must be true iff the encoder decided to encode this frame as
119 // a keyframe.
Elad Alon6796ec22019-04-15 10:07:50 +0200120 // If |info| is not null, the TemporalLayers instance may update |info| with
121 // codec specific data such as temporal id.
122 // |qp| should indicate the frame-level QP this frame was encoded at. If the
123 // encoder does not support extracting this, |qp| should be set to 0.
Elad Aloncde8ab22019-03-20 11:56:20 +0100124 virtual void OnEncodeDone(size_t stream_index,
125 uint32_t rtp_timestamp,
Elad Alonde3360e2019-03-06 21:14:54 +0100126 size_t size_bytes,
127 bool is_keyframe,
128 int qp,
129 CodecSpecificInfo* info) = 0;
Elad Aloncde8ab22019-03-20 11:56:20 +0100130
Elad Alon6796ec22019-04-15 10:07:50 +0200131 // Called when a frame is dropped by the encoder.
132 virtual void OnFrameDropped(size_t stream_index, uint32_t rtp_timestamp) = 0;
133
Elad Aloncde8ab22019-03-20 11:56:20 +0100134 // 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;
Elad Alon6c371ca2019-04-04 12:28:51 +0200140
141 // Called when a loss notification is received.
142 virtual void OnLossNotification(
Elad Alon123ee9b2019-04-17 12:48:06 +0200143 const VideoEncoder::LossNotification& loss_notification) = 0;
Elad Aloncde8ab22019-03-20 11:56:20 +0100144};
145
146// Interface for a factory of Vp8FrameBufferController instances.
147class Vp8FrameBufferControllerFactory {
148 public:
149 virtual ~Vp8FrameBufferControllerFactory() = default;
150
151 virtual std::unique_ptr<Vp8FrameBufferController> Create(
152 const VideoCodec& codec) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100153};
154
155} // namespace webrtc
156
157#endif // API_VIDEO_CODECS_VP8_FRAME_BUFFER_CONTROLLER_H_