scalable_video_controller.h (5419B)
1 /* 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 */ 10 #ifndef MODULES_VIDEO_CODING_SVC_SCALABLE_VIDEO_CONTROLLER_H_ 11 #define MODULES_VIDEO_CODING_SVC_SCALABLE_VIDEO_CONTROLLER_H_ 12 13 #include <vector> 14 15 #include "absl/container/inlined_vector.h" 16 #include "api/transport/rtp/dependency_descriptor.h" 17 #include "api/video/video_bitrate_allocation.h" 18 #include "api/video/video_codec_constants.h" 19 #include "common_video/generic_frame_descriptor/generic_frame_info.h" 20 21 namespace webrtc { 22 23 // Controls how video should be encoded to be scalable. Outputs results as 24 // buffer usage configuration for encoder and enough details to communicate the 25 // scalability structure via dependency descriptor rtp header extension. 26 class ScalableVideoController { 27 public: 28 struct StreamLayersConfig { 29 int num_spatial_layers = 1; 30 int num_temporal_layers = 1; 31 // Indicates if frames can reference frames of a different resolution. 32 bool uses_reference_scaling = true; 33 // Spatial layers scaling. Frames with spatial_id = i expected to be encoded 34 // with original_resolution * scaling_factor_num[i] / scaling_factor_den[i]. 35 int scaling_factor_num[DependencyDescriptor::kMaxSpatialIds] = {1, 1, 1, 1}; 36 int scaling_factor_den[DependencyDescriptor::kMaxSpatialIds] = {1, 1, 1, 1}; 37 }; 38 class LayerFrameConfig { 39 public: 40 // Builders/setters. 41 LayerFrameConfig& Id(int value); 42 LayerFrameConfig& Keyframe(); 43 LayerFrameConfig& S(int value); 44 LayerFrameConfig& T(int value); 45 LayerFrameConfig& Reference(int buffer_id); 46 LayerFrameConfig& Update(int buffer_id); 47 LayerFrameConfig& ReferenceAndUpdate(int buffer_id); 48 49 // Getters. 50 int Id() const { return id_; } 51 bool IsKeyframe() const { return is_keyframe_; } 52 int SpatialId() const { return spatial_id_; } 53 int TemporalId() const { return temporal_id_; } 54 const absl::InlinedVector<CodecBufferUsage, kMaxEncoderBuffers>& Buffers() 55 const { 56 return buffers_; 57 } 58 59 private: 60 // Id to match configuration returned by NextFrameConfig with 61 // (possibly modified) configuration passed back via OnEncoderDone. 62 // The meaning of the id is an implementation detail of 63 // the ScalableVideoController. 64 int id_ = 0; 65 66 // Indication frame should be encoded as a key frame. In particular when 67 // `is_keyframe=true` property `CodecBufferUsage::referenced` should be 68 // ignored and treated as false. 69 bool is_keyframe_ = false; 70 71 int spatial_id_ = 0; 72 int temporal_id_ = 0; 73 // Describes how encoder which buffers encoder allowed to reference and 74 // which buffers encoder should update. 75 absl::InlinedVector<CodecBufferUsage, kMaxEncoderBuffers> buffers_; 76 }; 77 78 virtual ~ScalableVideoController() = default; 79 80 // Returns video structure description for encoder to configure itself. 81 virtual StreamLayersConfig StreamConfig() const = 0; 82 83 // Returns video structure description in format compatible with 84 // dependency descriptor rtp header extension. 85 virtual FrameDependencyStructure DependencyStructure() const = 0; 86 87 // Notifies Controller with updated bitrates per layer. In particular notifies 88 // when certain layers should be disabled. 89 // Controller shouldn't produce LayerFrameConfig for disabled layers. 90 virtual void OnRatesUpdated(const VideoBitrateAllocation& bitrates) = 0; 91 92 // When `restart` is true, first `LayerFrameConfig` should have `is_keyframe` 93 // set to true. 94 // Returned vector shouldn't be empty. 95 virtual std::vector<LayerFrameConfig> NextFrameConfig(bool restart) = 0; 96 97 // Returns configuration to pass to EncoderCallback. 98 virtual GenericFrameInfo OnEncodeDone(const LayerFrameConfig& config) = 0; 99 }; 100 101 // Below are implementation details. 102 inline ScalableVideoController::LayerFrameConfig& 103 ScalableVideoController::LayerFrameConfig::Id(int value) { 104 id_ = value; 105 return *this; 106 } 107 inline ScalableVideoController::LayerFrameConfig& 108 ScalableVideoController::LayerFrameConfig::Keyframe() { 109 is_keyframe_ = true; 110 return *this; 111 } 112 inline ScalableVideoController::LayerFrameConfig& 113 ScalableVideoController::LayerFrameConfig::S(int value) { 114 spatial_id_ = value; 115 return *this; 116 } 117 inline ScalableVideoController::LayerFrameConfig& 118 ScalableVideoController::LayerFrameConfig::T(int value) { 119 temporal_id_ = value; 120 return *this; 121 } 122 inline ScalableVideoController::LayerFrameConfig& 123 ScalableVideoController::LayerFrameConfig::Reference(int buffer_id) { 124 buffers_.emplace_back(buffer_id, /*referenced=*/true, /*updated=*/false); 125 return *this; 126 } 127 inline ScalableVideoController::LayerFrameConfig& 128 ScalableVideoController::LayerFrameConfig::Update(int buffer_id) { 129 buffers_.emplace_back(buffer_id, /*referenced=*/false, /*updated=*/true); 130 return *this; 131 } 132 inline ScalableVideoController::LayerFrameConfig& 133 ScalableVideoController::LayerFrameConfig::ReferenceAndUpdate(int buffer_id) { 134 buffers_.emplace_back(buffer_id, /*referenced=*/true, /*updated=*/true); 135 return *this; 136 } 137 138 } // namespace webrtc 139 140 #endif // MODULES_VIDEO_CODING_SVC_SCALABLE_VIDEO_CONTROLLER_H_