blob: d2d8486863618c8a452e3495c884ce80b017a14c [file] [log] [blame]
Danil Chapovalovb471ac72020-05-15 14:21:03 +02001/*
2 * Copyright (c) 2020 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 */
Danil Chapovalovda7fe392020-10-15 15:57:17 +020010#ifndef MODULES_VIDEO_CODING_SVC_SCALABLE_VIDEO_CONTROLLER_H_
11#define MODULES_VIDEO_CODING_SVC_SCALABLE_VIDEO_CONTROLLER_H_
Danil Chapovalovb471ac72020-05-15 14:21:03 +020012
13#include <vector>
14
15#include "absl/container/inlined_vector.h"
Danil Chapovalovb471ac72020-05-15 14:21:03 +020016#include "api/transport/rtp/dependency_descriptor.h"
Danil Chapovalovbf181612020-06-25 15:47:57 +020017#include "api/video/video_bitrate_allocation.h"
Danil Chapovalovb471ac72020-05-15 14:21:03 +020018#include "common_video/generic_frame_descriptor/generic_frame_info.h"
19
20namespace webrtc {
21
22// Controls how video should be encoded to be scalable. Outputs results as
23// buffer usage configuration for encoder and enough details to communicate the
24// scalability structure via dependency descriptor rtp header extension.
25class ScalableVideoController {
26 public:
27 struct StreamLayersConfig {
28 int num_spatial_layers = 1;
29 int num_temporal_layers = 1;
Danil Chapovalov1e10a612020-07-02 15:27:03 +020030 // Spatial layers scaling. Frames with spatial_id = i expected to be encoded
31 // with original_resolution * scaling_factor_num[i] / scaling_factor_den[i].
32 int scaling_factor_num[DependencyDescriptor::kMaxSpatialIds] = {1, 1, 1, 1};
33 int scaling_factor_den[DependencyDescriptor::kMaxSpatialIds] = {1, 1, 1, 1};
Danil Chapovalovb471ac72020-05-15 14:21:03 +020034 };
Danil Chapovalova48dd432020-06-08 19:47:08 +020035 class LayerFrameConfig {
36 public:
37 // Builders/setters.
38 LayerFrameConfig& Id(int value);
39 LayerFrameConfig& Keyframe();
40 LayerFrameConfig& S(int value);
41 LayerFrameConfig& T(int value);
42 LayerFrameConfig& Reference(int buffer_id);
43 LayerFrameConfig& Update(int buffer_id);
44 LayerFrameConfig& ReferenceAndUpdate(int buffer_id);
45
46 // Getters.
47 int Id() const { return id_; }
48 bool IsKeyframe() const { return is_keyframe_; }
49 int SpatialId() const { return spatial_id_; }
50 int TemporalId() const { return temporal_id_; }
51 const absl::InlinedVector<CodecBufferUsage, kMaxEncoderBuffers>& Buffers()
52 const {
53 return buffers_;
54 }
55
56 private:
Danil Chapovalovb471ac72020-05-15 14:21:03 +020057 // Id to match configuration returned by NextFrameConfig with
58 // (possibly modified) configuration passed back via OnEncoderDone.
59 // The meaning of the id is an implementation detail of
60 // the ScalableVideoController.
Danil Chapovalova48dd432020-06-08 19:47:08 +020061 int id_ = 0;
Danil Chapovalovb471ac72020-05-15 14:21:03 +020062
63 // Indication frame should be encoded as a key frame. In particular when
64 // `is_keyframe=true` property `CodecBufferUsage::referenced` should be
65 // ignored and treated as false.
Danil Chapovalova48dd432020-06-08 19:47:08 +020066 bool is_keyframe_ = false;
Danil Chapovalovb471ac72020-05-15 14:21:03 +020067
Danil Chapovalova48dd432020-06-08 19:47:08 +020068 int spatial_id_ = 0;
69 int temporal_id_ = 0;
Danil Chapovalovb471ac72020-05-15 14:21:03 +020070 // Describes how encoder which buffers encoder allowed to reference and
71 // which buffers encoder should update.
Danil Chapovalova48dd432020-06-08 19:47:08 +020072 absl::InlinedVector<CodecBufferUsage, kMaxEncoderBuffers> buffers_;
Danil Chapovalovb471ac72020-05-15 14:21:03 +020073 };
74
75 virtual ~ScalableVideoController() = default;
76
77 // Returns video structure description for encoder to configure itself.
78 virtual StreamLayersConfig StreamConfig() const = 0;
79
80 // Returns video structure description in format compatible with
81 // dependency descriptor rtp header extension.
82 virtual FrameDependencyStructure DependencyStructure() const = 0;
83
Danil Chapovalovbf181612020-06-25 15:47:57 +020084 // Notifies Controller with updated bitrates per layer. In particular notifies
85 // when certain layers should be disabled.
86 // Controller shouldn't produce LayerFrameConfig for disabled layers.
Danil Chapovalov347a3272020-10-26 11:40:26 +010087 virtual void OnRatesUpdated(const VideoBitrateAllocation& bitrates) = 0;
Danil Chapovalovbf181612020-06-25 15:47:57 +020088
Danil Chapovalovb471ac72020-05-15 14:21:03 +020089 // When `restart` is true, first `LayerFrameConfig` should have `is_keyframe`
90 // set to true.
91 // Returned vector shouldn't be empty.
92 virtual std::vector<LayerFrameConfig> NextFrameConfig(bool restart) = 0;
93
94 // Returns configuration to pass to EncoderCallback.
Danil Chapovalovc85baeb2020-10-14 18:09:58 +020095 virtual GenericFrameInfo OnEncodeDone(const LayerFrameConfig& config) = 0;
Danil Chapovalovb471ac72020-05-15 14:21:03 +020096};
97
Danil Chapovalova48dd432020-06-08 19:47:08 +020098// Below are implementation details.
99inline ScalableVideoController::LayerFrameConfig&
100ScalableVideoController::LayerFrameConfig::Id(int value) {
101 id_ = value;
102 return *this;
103}
104inline ScalableVideoController::LayerFrameConfig&
105ScalableVideoController::LayerFrameConfig::Keyframe() {
106 is_keyframe_ = true;
107 return *this;
108}
109inline ScalableVideoController::LayerFrameConfig&
110ScalableVideoController::LayerFrameConfig::S(int value) {
111 spatial_id_ = value;
112 return *this;
113}
114inline ScalableVideoController::LayerFrameConfig&
115ScalableVideoController::LayerFrameConfig::T(int value) {
116 temporal_id_ = value;
117 return *this;
118}
119inline ScalableVideoController::LayerFrameConfig&
120ScalableVideoController::LayerFrameConfig::Reference(int buffer_id) {
121 buffers_.emplace_back(buffer_id, /*referenced=*/true, /*updated=*/false);
122 return *this;
123}
124inline ScalableVideoController::LayerFrameConfig&
125ScalableVideoController::LayerFrameConfig::Update(int buffer_id) {
126 buffers_.emplace_back(buffer_id, /*referenced=*/false, /*updated=*/true);
127 return *this;
128}
129inline ScalableVideoController::LayerFrameConfig&
130ScalableVideoController::LayerFrameConfig::ReferenceAndUpdate(int buffer_id) {
131 buffers_.emplace_back(buffer_id, /*referenced=*/true, /*updated=*/true);
132 return *this;
133}
134
Danil Chapovalovb471ac72020-05-15 14:21:03 +0200135} // namespace webrtc
136
Danil Chapovalovda7fe392020-10-15 15:57:17 +0200137#endif // MODULES_VIDEO_CODING_SVC_SCALABLE_VIDEO_CONTROLLER_H_