xref: /aosp_15_r20/external/webrtc/api/video/video_bitrate_allocation.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
12*d9f75844SAndroid Build Coastguard Worker #define API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
15*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include <limits>
18*d9f75844SAndroid Build Coastguard Worker #include <string>
19*d9f75844SAndroid Build Coastguard Worker #include <vector>
20*d9f75844SAndroid Build Coastguard Worker 
21*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_codec_constants.h"
23*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h"
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker // Class that describes how video bitrate, in bps, is allocated across temporal
28*d9f75844SAndroid Build Coastguard Worker // and spatial layers. Not that bitrates are NOT cumulative. Depending on if
29*d9f75844SAndroid Build Coastguard Worker // layers are dependent or not, it is up to the user to aggregate.
30*d9f75844SAndroid Build Coastguard Worker // For each index, the bitrate can also both set and unset. This is used with a
31*d9f75844SAndroid Build Coastguard Worker // set bps = 0 to signal an explicit "turn off" signal.
32*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT VideoBitrateAllocation {
33*d9f75844SAndroid Build Coastguard Worker  public:
34*d9f75844SAndroid Build Coastguard Worker   static constexpr uint32_t kMaxBitrateBps =
35*d9f75844SAndroid Build Coastguard Worker       std::numeric_limits<uint32_t>::max();
36*d9f75844SAndroid Build Coastguard Worker   VideoBitrateAllocation();
37*d9f75844SAndroid Build Coastguard Worker 
38*d9f75844SAndroid Build Coastguard Worker   bool SetBitrate(size_t spatial_index,
39*d9f75844SAndroid Build Coastguard Worker                   size_t temporal_index,
40*d9f75844SAndroid Build Coastguard Worker                   uint32_t bitrate_bps);
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker   bool HasBitrate(size_t spatial_index, size_t temporal_index) const;
43*d9f75844SAndroid Build Coastguard Worker 
44*d9f75844SAndroid Build Coastguard Worker   uint32_t GetBitrate(size_t spatial_index, size_t temporal_index) const;
45*d9f75844SAndroid Build Coastguard Worker 
46*d9f75844SAndroid Build Coastguard Worker   // Whether the specific spatial layers has the bitrate set in any of its
47*d9f75844SAndroid Build Coastguard Worker   // temporal layers.
48*d9f75844SAndroid Build Coastguard Worker   bool IsSpatialLayerUsed(size_t spatial_index) const;
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker   // Get the sum of all the temporal layer for a specific spatial layer.
51*d9f75844SAndroid Build Coastguard Worker   uint32_t GetSpatialLayerSum(size_t spatial_index) const;
52*d9f75844SAndroid Build Coastguard Worker 
53*d9f75844SAndroid Build Coastguard Worker   // Sum of bitrates of temporal layers, from layer 0 to `temporal_index`
54*d9f75844SAndroid Build Coastguard Worker   // inclusive, of specified spatial layer `spatial_index`. Bitrates of lower
55*d9f75844SAndroid Build Coastguard Worker   // spatial layers are not included.
56*d9f75844SAndroid Build Coastguard Worker   uint32_t GetTemporalLayerSum(size_t spatial_index,
57*d9f75844SAndroid Build Coastguard Worker                                size_t temporal_index) const;
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   // Returns a vector of the temporal layer bitrates for the specific spatial
60*d9f75844SAndroid Build Coastguard Worker   // layer. Length of the returned vector is cropped to the highest temporal
61*d9f75844SAndroid Build Coastguard Worker   // layer with a defined bitrate.
62*d9f75844SAndroid Build Coastguard Worker   std::vector<uint32_t> GetTemporalLayerAllocation(size_t spatial_index) const;
63*d9f75844SAndroid Build Coastguard Worker 
64*d9f75844SAndroid Build Coastguard Worker   // Returns one VideoBitrateAllocation for each spatial layer. This is used to
65*d9f75844SAndroid Build Coastguard Worker   // configure simulcast streams. Note that the length of the returned vector is
66*d9f75844SAndroid Build Coastguard Worker   // always kMaxSpatialLayers, the optional is unset for unused layers.
67*d9f75844SAndroid Build Coastguard Worker   std::vector<absl::optional<VideoBitrateAllocation>> GetSimulcastAllocations()
68*d9f75844SAndroid Build Coastguard Worker       const;
69*d9f75844SAndroid Build Coastguard Worker 
get_sum_bps()70*d9f75844SAndroid Build Coastguard Worker   uint32_t get_sum_bps() const { return sum_; }  // Sum of all bitrates.
get_sum_kbps()71*d9f75844SAndroid Build Coastguard Worker   uint32_t get_sum_kbps() const {
72*d9f75844SAndroid Build Coastguard Worker     // Round down to not exceed the allocated bitrate.
73*d9f75844SAndroid Build Coastguard Worker     return sum_ / 1000;
74*d9f75844SAndroid Build Coastguard Worker   }
75*d9f75844SAndroid Build Coastguard Worker 
76*d9f75844SAndroid Build Coastguard Worker   bool operator==(const VideoBitrateAllocation& other) const;
77*d9f75844SAndroid Build Coastguard Worker   inline bool operator!=(const VideoBitrateAllocation& other) const {
78*d9f75844SAndroid Build Coastguard Worker     return !(*this == other);
79*d9f75844SAndroid Build Coastguard Worker   }
80*d9f75844SAndroid Build Coastguard Worker 
81*d9f75844SAndroid Build Coastguard Worker   std::string ToString() const;
82*d9f75844SAndroid Build Coastguard Worker 
83*d9f75844SAndroid Build Coastguard Worker   // Indicates if the allocation has some layers/streams disabled due to
84*d9f75844SAndroid Build Coastguard Worker   // low available bandwidth.
set_bw_limited(bool limited)85*d9f75844SAndroid Build Coastguard Worker   void set_bw_limited(bool limited) { is_bw_limited_ = limited; }
is_bw_limited()86*d9f75844SAndroid Build Coastguard Worker   bool is_bw_limited() const { return is_bw_limited_; }
87*d9f75844SAndroid Build Coastguard Worker 
88*d9f75844SAndroid Build Coastguard Worker  private:
89*d9f75844SAndroid Build Coastguard Worker   uint32_t sum_;
90*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> bitrates_[kMaxSpatialLayers][kMaxTemporalStreams];
91*d9f75844SAndroid Build Coastguard Worker   bool is_bw_limited_;
92*d9f75844SAndroid Build Coastguard Worker };
93*d9f75844SAndroid Build Coastguard Worker 
94*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
95*d9f75844SAndroid Build Coastguard Worker 
96*d9f75844SAndroid Build Coastguard Worker #endif  // API_VIDEO_VIDEO_BITRATE_ALLOCATION_H_
97