av1_decoder.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_AV1_DECODER_H_
  5. #define MEDIA_GPU_AV1_DECODER_H_
  6. #include <array>
  7. #include <memory>
  8. #include "base/containers/span.h"
  9. #include "base/sequence_checker.h"
  10. #include "media/base/video_codecs.h"
  11. #include "media/base/video_color_space.h"
  12. #include "media/base/video_types.h"
  13. #include "media/gpu/accelerated_video_decoder.h"
  14. #include "media/gpu/media_gpu_export.h"
  15. #include "third_party/libgav1/src/src/utils/constants.h"
  16. // For libgav1::RefCountedBufferPtr.
  17. #include "third_party/libgav1/src/src/buffer_pool.h"
  18. // For libgav1::ObuSequenceHeader. absl::optional demands ObuSequenceHeader to
  19. // fulfill std::is_trivially_constructible if it is forward-declared. But
  20. // ObuSequenceHeader doesn't.
  21. #include "third_party/libgav1/src/src/obu_parser.h"
  22. namespace libgav1 {
  23. struct DecoderState;
  24. struct ObuFrameHeader;
  25. template <typename T>
  26. class Vector;
  27. } // namespace libgav1
  28. namespace media {
  29. class AV1Picture;
  30. using AV1ReferenceFrameVector =
  31. std::array<scoped_refptr<AV1Picture>, libgav1::kNumReferenceFrameTypes>;
  32. // Clients of this class are expected to pass an AV1 OBU stream and are expected
  33. // to provide an implementation of AV1Accelerator for offloading final steps
  34. // of the decoding process.
  35. //
  36. // This class must be created, called and destroyed on a single thread, and
  37. // does nothing internally on any other thread.
  38. class MEDIA_GPU_EXPORT AV1Decoder : public AcceleratedVideoDecoder {
  39. public:
  40. class MEDIA_GPU_EXPORT AV1Accelerator {
  41. public:
  42. // Methods may return kTryAgain if they need additional data (provided
  43. // independently) in order to proceed. Examples are things like not having
  44. // an appropriate key to decode encrypted content. This is not considered an
  45. // unrecoverable error, but rather a pause to allow an application to
  46. // independently provide the required data. When AV1Decoder::Decode()
  47. // is called again, it will attempt to resume processing of the stream
  48. // by calling the same method again.
  49. enum class Status {
  50. // Operation completed successfully.
  51. kOk,
  52. // Operation failed.
  53. kFail,
  54. // Operation failed because some external data is missing. Retry the same
  55. // operation later, once the data has been provided.
  56. kTryAgain,
  57. };
  58. AV1Accelerator() = default;
  59. virtual ~AV1Accelerator() = default;
  60. AV1Accelerator(const AV1Accelerator&) = delete;
  61. AV1Accelerator& operator=(const AV1Accelerator&) = delete;
  62. // Creates an AV1Picture that the AV1Decoder can use to store some of the
  63. // information needed to request accelerated decoding. This picture is later
  64. // passed when calling SubmitDecode() so that the AV1Accelerator can submit
  65. // the decode request to the driver. It may also be stored for use as
  66. // reference to decode other pictures.
  67. // When a picture is no longer needed by the decoder, it will just drop
  68. // its reference to it, and it may do so at any time.
  69. // Note that this may return nullptr if the accelerator is not able to
  70. // provide any new pictures at the given time. The decoder must handle this
  71. // case and treat it as normal, returning kRanOutOfSurfaces from Decode().
  72. virtual scoped_refptr<AV1Picture> CreateAV1Picture(bool apply_grain) = 0;
  73. // Submits |pic| to the driver for accelerated decoding. The following
  74. // parameters are also passed:
  75. // - |sequence_header|: the current OBU sequence header.
  76. // - |ref_frames|: the pictures used as reference for decoding |pic|.
  77. // - |tile_buffers|: tile information.
  78. // - |data|: the entire data of the DecoderBuffer set by
  79. // AV1Decoder::SetStream().
  80. // Note that returning from this method does not mean that the decode
  81. // process is finished, but the caller may drop its references to |pic|
  82. // and |ref_frames| immediately, and |data| does not need to remain valid
  83. // after this method returns.
  84. virtual Status SubmitDecode(
  85. const AV1Picture& pic,
  86. const libgav1::ObuSequenceHeader& sequence_header,
  87. const AV1ReferenceFrameVector& ref_frames,
  88. const libgav1::Vector<libgav1::TileBuffer>& tile_buffers,
  89. base::span<const uint8_t> data) = 0;
  90. // Schedules output (display) of |pic|.
  91. // Note that returning from this method does not mean that |pic| has already
  92. // been outputted (displayed), but guarantees that all pictures will be
  93. // outputted in the same order as this method was called for them, and that
  94. // they are decoded before outputting (assuming SubmitDecode() has been
  95. // called for them beforehand).
  96. // Returns true when successful, false otherwise.
  97. virtual bool OutputPicture(const AV1Picture& pic) = 0;
  98. };
  99. AV1Decoder(std::unique_ptr<AV1Accelerator> accelerator,
  100. VideoCodecProfile profile,
  101. const VideoColorSpace& container_color_space = VideoColorSpace());
  102. ~AV1Decoder() override;
  103. AV1Decoder(const AV1Decoder&) = delete;
  104. AV1Decoder& operator=(const AV1Decoder&) = delete;
  105. // AcceleratedVideoDecoder implementation.
  106. void SetStream(int32_t id, const DecoderBuffer& decoder_buffer) override;
  107. [[nodiscard]] bool Flush() override;
  108. void Reset() override;
  109. [[nodiscard]] DecodeResult Decode() override;
  110. gfx::Size GetPicSize() const override;
  111. gfx::Rect GetVisibleRect() const override;
  112. VideoCodecProfile GetProfile() const override;
  113. uint8_t GetBitDepth() const override;
  114. VideoChromaSampling GetChromaSampling() const override;
  115. size_t GetRequiredNumOfPictures() const override;
  116. size_t GetNumReferenceFrames() const override;
  117. private:
  118. friend class AV1DecoderTest;
  119. AV1Accelerator::Status DecodeAndOutputPicture(
  120. scoped_refptr<AV1Picture> pic,
  121. const libgav1::Vector<libgav1::TileBuffer>& tile_buffers);
  122. void UpdateReferenceFrames(scoped_refptr<AV1Picture> pic);
  123. void ClearReferenceFrames();
  124. // Checks that |ref_frames_| is consistent with libgav1's reference frame
  125. // state (returns false if not) and cleans old reference frames from
  126. // |ref_frames_| as needed. Also asserts that all reference frames needed by
  127. // |current_frame_header_| are in |ref_frames_|. This method should be called
  128. // prior to using |ref_frames_| (which includes calling
  129. // |accelerator_|->SubmitDecode());
  130. bool CheckAndCleanUpReferenceFrames();
  131. void ClearCurrentFrame();
  132. DecodeResult DecodeInternal();
  133. bool on_error_ = false;
  134. std::unique_ptr<libgav1::BufferPool> buffer_pool_;
  135. std::unique_ptr<libgav1::DecoderState> state_;
  136. std::unique_ptr<libgav1::ObuParser> parser_;
  137. const std::unique_ptr<AV1Accelerator> accelerator_;
  138. AV1ReferenceFrameVector ref_frames_;
  139. absl::optional<libgav1::ObuSequenceHeader> current_sequence_header_;
  140. absl::optional<libgav1::ObuFrameHeader> current_frame_header_;
  141. libgav1::RefCountedBufferPtr current_frame_;
  142. gfx::Rect visible_rect_;
  143. gfx::Size frame_size_;
  144. VideoCodecProfile profile_;
  145. VideoColorSpace container_color_space_;
  146. uint8_t bit_depth_ = 0;
  147. VideoChromaSampling chroma_sampling_ = VideoChromaSampling::kUnknown;
  148. int32_t stream_id_ = 0;
  149. const uint8_t* stream_ = nullptr;
  150. size_t stream_size_ = 0;
  151. std::unique_ptr<DecryptConfig> decrypt_config_;
  152. // Pending picture for decode when accelerator returns kTryAgain.
  153. scoped_refptr<AV1Picture> pending_pic_;
  154. SEQUENCE_CHECKER(sequence_checker_);
  155. };
  156. } // namespace media
  157. #endif // MEDIA_GPU_AV1_DECODER_H_