vp9_svc_layers.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2020 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_GPU_VP9_SVC_LAYERS_H_
  5. #define MEDIA_GPU_VP9_SVC_LAYERS_H_
  6. #include <stdint.h>
  7. #include <vector>
  8. #include "media/filters/vp9_parser.h"
  9. #include "media/gpu/media_gpu_export.h"
  10. #include "media/video/video_encode_accelerator.h"
  11. namespace media {
  12. class VideoBitrateAllocation;
  13. class VP9Picture;
  14. struct Vp9Metadata;
  15. // This class manages a state of K-SVC encoding up to three spatial and temporal
  16. // layers. This supports activating/deactivating spatial layers while the
  17. // temporal layer sizes must be unchanged. The temporal layer sizes among
  18. // spatial layers must be identical. Temporal layers and spatial layers are
  19. // described in https://tools.ietf.org/html/draft-ietf-payload-vp9-10#section-3.
  20. class MEDIA_GPU_EXPORT VP9SVCLayers {
  21. public:
  22. struct FrameConfig;
  23. constexpr static size_t kMaxSupportedTemporalLayers = 3u;
  24. constexpr static size_t kMaxSpatialLayers = 3u;
  25. constexpr static size_t kMaxNumUsedRefFramesEachSpatialLayer =
  26. kVp9NumRefFrames / kMaxSpatialLayers;
  27. static_assert(
  28. kMaxNumUsedRefFramesEachSpatialLayer == 2u,
  29. "VP9SVCLayers uses two reference frames for each spatial layer");
  30. constexpr static size_t kMaxNumUsedReferenceFrames =
  31. kMaxNumUsedRefFramesEachSpatialLayer * kMaxSpatialLayers;
  32. static_assert(kMaxNumUsedReferenceFrames == 6u,
  33. "VP9SVCLayers uses six reference frames");
  34. using SpatialLayer = VideoEncodeAccelerator::Config::SpatialLayer;
  35. explicit VP9SVCLayers(const std::vector<SpatialLayer>& spatial_layers);
  36. ~VP9SVCLayers();
  37. // Returns true if EncodeJob needs to produce key frame.
  38. bool UpdateEncodeJob(bool is_key_frame_requested, size_t kf_period_frames);
  39. // Activate/Deactivate spatial layers via |bitrate_allocation|.
  40. // Returns whether (de)updating is successful.
  41. bool MaybeUpdateActiveLayer(VideoBitrateAllocation* bitrate_allocation);
  42. // Sets |picture|'s used reference frames and |ref_frames_used| so that they
  43. // structure valid temporal layers. This also fills |picture|'s
  44. // |metadata_for_encoding|.
  45. void FillUsedRefFramesAndMetadata(
  46. VP9Picture* picture,
  47. std::array<bool, kVp9NumRefsPerFrame>* ref_frames_used);
  48. size_t num_temporal_layers() const { return num_temporal_layers_; }
  49. const std::vector<gfx::Size>& active_spatial_layer_resolutions() const {
  50. return active_spatial_layer_resolutions_;
  51. }
  52. private:
  53. // Useful functions to construct refresh flag and detect reference frames
  54. // from the flag.
  55. void FillVp9MetadataForEncoding(
  56. Vp9Metadata* metadata,
  57. const std::vector<uint8_t>& reference_frame_indices) const;
  58. void UpdateRefFramesPatternIndex(
  59. const std::vector<uint8_t>& refresh_frame_indices);
  60. // Following variables are configured upon construction, containing the amount
  61. // of temporal layers/spatial layers, the associated temporal layers indices
  62. // and the nature (reference, update, both, none) of each frame in the
  63. // temporal group, respectively.
  64. const size_t num_temporal_layers_;
  65. const std::vector<FrameConfig> temporal_layers_reference_pattern_;
  66. // The current index into the |temporal_layers_reference_pattern_|.
  67. uint8_t pattern_index_;
  68. const size_t temporal_pattern_size_;
  69. size_t spatial_idx_ = 0;
  70. size_t frame_num_ = 0;
  71. bool force_key_frame_ = false;
  72. // Resolutions for all spatial layers and active spatial layers.
  73. std::vector<gfx::Size> spatial_layer_resolutions_;
  74. std::vector<gfx::Size> active_spatial_layer_resolutions_;
  75. // Stores the active layer range, only used to judge whether active range has
  76. // changed in |MaybeUpdateActiveLayer|, then
  77. // |active_spatial_layer_resolutions_| needs update.
  78. size_t begin_active_layer_;
  79. size_t end_active_layer_;
  80. // The pattern index used for reference frames slots.
  81. uint8_t pattern_index_of_ref_frames_slots_[kMaxNumUsedReferenceFrames] = {};
  82. };
  83. } // namespace media
  84. #endif // MEDIA_GPU_VP9_SVC_LAYERS_H_