video_bitrate_allocation.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_BASE_VIDEO_BITRATE_ALLOCATION_H_
  5. #define MEDIA_BASE_VIDEO_BITRATE_ALLOCATION_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. #include "media/base/bitrate.h"
  10. #include "media/base/media_export.h"
  11. namespace media {
  12. // Class that describes how video bitrate, in bps, is allocated across temporal
  13. // and spatial layers. Note that bitrates are NOT cumulative. Depending on if
  14. // layers are dependent or not, it is up to the user to aggregate.
  15. class MEDIA_EXPORT VideoBitrateAllocation {
  16. public:
  17. static constexpr size_t kMaxSpatialLayers = 5;
  18. static constexpr size_t kMaxTemporalLayers = 4;
  19. explicit VideoBitrateAllocation(
  20. Bitrate::Mode mode = Bitrate::Mode::kConstant);
  21. ~VideoBitrateAllocation() = default;
  22. // Returns true iff. the bitrate was set (sum within uint32_t max value). If
  23. // a variable bitrate is used and the previous peak bitrate was below the new
  24. // sum of bitrates across layers, this will automatically set the new peak to
  25. // equal the new sum. If you have a signed or 64-bit value you want to use as
  26. // input, you must explicitly convert to uint32_t before calling. This is
  27. // intended to prevent implicit and unsafe type conversion.
  28. bool SetBitrate(size_t spatial_index,
  29. size_t temporal_index,
  30. uint32_t bitrate_bps);
  31. // Deleted variants: you must SAFELY convert to uint32_t before calling.
  32. // See base/numerics/safe_conversions.h for functions to safely convert
  33. // between types.
  34. bool SetBitrate(size_t spatial_index,
  35. size_t temporal_index,
  36. int32_t bitrate_bps) = delete;
  37. bool SetBitrate(size_t spatial_index,
  38. size_t temporal_index,
  39. int64_t bitrate_bps) = delete;
  40. bool SetBitrate(size_t spatial_index,
  41. size_t temporal_index,
  42. uint64_t bitrate_bps) = delete;
  43. // True iff. this bitrate allocation can have its peak set to |peak_bps| (the
  44. // peak must be greater than the sum of the layers' bitrates, and the bitrate
  45. // mode must be variable bitrate).
  46. bool SetPeakBps(uint32_t peak_bps);
  47. // Returns the bitrate for specified spatial/temporal index, or 0 if not set.
  48. uint32_t GetBitrateBps(size_t spatial_index, size_t temporal_index) const;
  49. // Sum of all bitrates.
  50. uint32_t GetSumBps() const;
  51. // Non-layered bitrate allocation. If there are layers, this bitrate's target
  52. // bps equals the sum of the layers' bitrates.
  53. const Bitrate GetSumBitrate() const;
  54. // Returns the encoding rate control mode of this allocation.
  55. Bitrate::Mode GetMode() const;
  56. std::string ToString() const;
  57. bool operator==(const VideoBitrateAllocation& other) const;
  58. inline bool operator!=(const VideoBitrateAllocation& other) const {
  59. return !(*this == other);
  60. }
  61. private:
  62. // A bitrate representing a cached sum of the elements of |bitrates_|, for
  63. // performance.
  64. Bitrate sum_bitrate_;
  65. uint32_t bitrates_[kMaxSpatialLayers][kMaxTemporalLayers] = {};
  66. };
  67. } // namespace media
  68. #endif // MEDIA_BASE_VIDEO_BITRATE_ALLOCATION_H_