blob: e41df8e7249ff339e924c189d781298f0c7b5bf7 [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
Elad Alon979c4422019-04-17 12:53:08 +020028// PopulateCodecSpecific() / OnEncodeDone() must happen in the same order.
Elad Alonde3360e2019-03-06 21:14:54 +010029//
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 Alon979c4422019-04-17 12:53:08 +020041// OnEncodeDone() for a previous one, but calls themselves must be both
Elad Alonde3360e2019-03-06 21:14:54 +010042// synchronized (e.g. run on a task queue) and in order (per type).
Elad Alon979c4422019-04-17 12:53:08 +020043//
44// TODO(eladalon): Revise comment (referring to PopulateCodecSpecific in this
45// context is not very meaningful).
Elad Alonde3360e2019-03-06 21:14:54 +010046
47struct CodecSpecificInfo;
48
Elad Alon979c4422019-04-17 12:53:08 +020049// TODO(eladalon): This configuration is temporal-layers specific; refactor.
Elad Alonde3360e2019-03-06 21:14:54 +010050struct 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 Aloncde8ab22019-03-20 11:56:20 +010077// Multiple streams may be controlled by a single controller, demuxing between
78// them using stream_index.
Elad Alonde3360e2019-03-06 21:14:54 +010079class Vp8FrameBufferController {
80 public:
81 virtual ~Vp8FrameBufferController() = default;
82
Elad Aloncde8ab22019-03-20 11:56:20 +010083 // Number of streamed controlled by |this|.
84 virtual size_t StreamCount() const = 0;
85
Elad Alonde3360e2019-03-06 21:14:54 +010086 // 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 Aloncde8ab22019-03-20 11:56:20 +010095 virtual bool SupportsEncoderFrameDropping(size_t stream_index) const = 0;
Elad Alonde3360e2019-03-06 21:14:54 +010096
Elad Alon979c4422019-04-17 12:53:08 +020097 // New target bitrate for a stream (each entry in
98 // |bitrates_bps| is for another temporal layer).
Elad Aloncde8ab22019-03-20 11:56:20 +010099 virtual void OnRatesUpdated(size_t stream_index,
100 const std::vector<uint32_t>& bitrates_bps,
Elad Alonde3360e2019-03-06 21:14:54 +0100101 int framerate_fps) = 0;
102
103 // Called by the encoder before encoding a frame. |cfg| contains the current
Elad Alon979c4422019-04-17 12:53:08 +0200104 // 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 Aloncde8ab22019-03-20 11:56:20 +0100107 virtual bool UpdateConfiguration(size_t stream_index,
108 Vp8EncoderConfig* cfg) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100109
Elad Alon979c4422019-04-17 12:53:08 +0200110 // Returns the recommended VP8 encode flags needed.
Elad Alonde3360e2019-03-06 21:14:54 +0100111 // 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 Alon979c4422019-04-17 12:53:08 +0200116 virtual Vp8FrameConfig NextFrameConfig(size_t stream_index,
117 uint32_t rtp_timestamp) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100118
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 Alon979c4422019-04-17 12:53:08 +0200123 // 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 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
Elad Alon6796ec22019-04-15 10:07:50 +0200134 // Called when a frame is dropped by the encoder.
135 virtual void OnFrameDropped(size_t stream_index, uint32_t rtp_timestamp) = 0;
136
Elad Aloncde8ab22019-03-20 11:56:20 +0100137 // 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 Alon6c371ca2019-04-04 12:28:51 +0200143
144 // Called when a loss notification is received.
145 virtual void OnLossNotification(
Elad Alon123ee9b2019-04-17 12:48:06 +0200146 const VideoEncoder::LossNotification& loss_notification) = 0;
Elad Aloncde8ab22019-03-20 11:56:20 +0100147};
148
149// Interface for a factory of Vp8FrameBufferController instances.
150class Vp8FrameBufferControllerFactory {
151 public:
152 virtual ~Vp8FrameBufferControllerFactory() = default;
153
154 virtual std::unique_ptr<Vp8FrameBufferController> Create(
155 const VideoCodec& codec) = 0;
Elad Alonde3360e2019-03-06 21:14:54 +0100156};
157
158} // namespace webrtc
159
160#endif // API_VIDEO_CODECS_VP8_FRAME_BUFFER_CONTROLLER_H_