vp9_decoder.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2015 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_DECODER_H_
  5. #define MEDIA_GPU_VP9_DECODER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/callback_forward.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "media/base/video_types.h"
  13. #include "media/filters/vp9_parser.h"
  14. #include "media/gpu/accelerated_video_decoder.h"
  15. #include "media/gpu/vp9_picture.h"
  16. #include "media/gpu/vp9_reference_frame_vector.h"
  17. #include "ui/gfx/geometry/size.h"
  18. namespace media {
  19. // This class implements an AcceleratedVideoDecoder for VP9 decoding.
  20. // Clients of this class are expected to pass raw VP9 stream and are expected
  21. // to provide an implementation of VP9Accelerator for offloading final steps
  22. // of the decoding process.
  23. //
  24. // This class must be created, called and destroyed on a single thread, and
  25. // does nothing internally on any other thread.
  26. class MEDIA_GPU_EXPORT VP9Decoder : public AcceleratedVideoDecoder {
  27. public:
  28. class MEDIA_GPU_EXPORT VP9Accelerator {
  29. public:
  30. // Methods may return kTryAgain if they need additional data (provided
  31. // independently) in order to proceed. Examples are things like not having
  32. // an appropriate key to decode encrypted content. This is not considered an
  33. // unrecoverable error, but rather a pause to allow an application to
  34. // independently provide the required data. When VP9Decoder::Decode()
  35. // is called again, it will attempt to resume processing of the stream
  36. // by calling the same method again.
  37. enum class Status {
  38. // Operation completed successfully.
  39. kOk,
  40. // Operation failed.
  41. kFail,
  42. // Operation failed because some external data is missing. Retry the same
  43. // operation later, once the data has been provided.
  44. kTryAgain,
  45. };
  46. VP9Accelerator();
  47. VP9Accelerator(const VP9Accelerator&) = delete;
  48. VP9Accelerator& operator=(const VP9Accelerator&) = delete;
  49. virtual ~VP9Accelerator();
  50. // Create a new VP9Picture that the decoder client can use for initial
  51. // stages of the decoding process and pass back to this accelerator for
  52. // final, accelerated stages of it, or for reference when decoding other
  53. // pictures.
  54. //
  55. // When a picture is no longer needed by the decoder, it will just drop
  56. // its reference to it, and it may do so at any time.
  57. //
  58. // Note that this may return nullptr if the accelerator is not able to
  59. // provide any new pictures at the given time. The decoder must handle this
  60. // case and treat it as normal, returning kRanOutOfSurfaces from Decode().
  61. virtual scoped_refptr<VP9Picture> CreateVP9Picture() = 0;
  62. // Submit decode for |pic| to be run in accelerator, taking as arguments
  63. // information contained in it, as well as current segmentation and loop
  64. // filter state in |segm_params| and |lf_params|, respectively, and using
  65. // pictures in |ref_pictures| for reference.
  66. // If done_cb_ is not null, it will be run once decode is done in hardware.
  67. //
  68. // Note that returning from this method does not mean that the decode
  69. // process is finished, but the caller may drop its references to |pic|
  70. // and |ref_pictures| immediately, and the data in |segm_params| and
  71. // |lf_params| does not need to remain valid after this method returns.
  72. //
  73. // Return true when successful, false otherwise.
  74. virtual Status SubmitDecode(scoped_refptr<VP9Picture> pic,
  75. const Vp9SegmentationParams& segm_params,
  76. const Vp9LoopFilterParams& lf_params,
  77. const Vp9ReferenceFrameVector& reference_frames,
  78. const base::OnceClosure done_cb) = 0;
  79. // Schedule output (display) of |pic|.
  80. //
  81. // Note that returning from this method does not mean that |pic| has already
  82. // been outputted (displayed), but guarantees that all pictures will be
  83. // outputted in the same order as this method was called for them, and that
  84. // they are decoded before outputting (assuming SubmitDecode() has been
  85. // called for them beforehand). Decoder may drop its references to |pic|
  86. // immediately after calling this method.
  87. //
  88. // Return true when successful, false otherwise.
  89. virtual bool OutputPicture(scoped_refptr<VP9Picture> pic) = 0;
  90. // Return true if the accelerator requires us to provide the compressed
  91. // header fully parsed.
  92. virtual bool NeedsCompressedHeaderParsed() const = 0;
  93. // Set |frame_ctx| to the state after decoding |pic|, returning true on
  94. // success, false otherwise.
  95. virtual bool GetFrameContext(scoped_refptr<VP9Picture> pic,
  96. Vp9FrameContext* frame_ctx) = 0;
  97. // VP9Parser can update the context probabilities or can query the driver
  98. // to get the updated numbers. By default drivers don't support it, and in
  99. // particular it's true for legacy (unstable) V4L2 API versions.
  100. virtual bool SupportsContextProbabilityReadback() const;
  101. };
  102. explicit VP9Decoder(
  103. std::unique_ptr<VP9Accelerator> accelerator,
  104. VideoCodecProfile profile,
  105. const VideoColorSpace& container_color_space = VideoColorSpace());
  106. VP9Decoder(const VP9Decoder&) = delete;
  107. VP9Decoder& operator=(const VP9Decoder&) = delete;
  108. ~VP9Decoder() override;
  109. // AcceleratedVideoDecoder implementation.
  110. void SetStream(int32_t id, const DecoderBuffer& decoder_buffer) override;
  111. [[nodiscard]] bool Flush() override;
  112. void Reset() override;
  113. [[nodiscard]] DecodeResult Decode() override;
  114. gfx::Size GetPicSize() const override;
  115. gfx::Rect GetVisibleRect() const override;
  116. VideoCodecProfile GetProfile() const override;
  117. uint8_t GetBitDepth() const override;
  118. VideoChromaSampling GetChromaSampling() const override;
  119. size_t GetRequiredNumOfPictures() const override;
  120. size_t GetNumReferenceFrames() const override;
  121. private:
  122. // Decode and possibly output |pic| (if the picture is to be shown).
  123. // Return kOk on success, kTryAgain if this should be attempted again on the
  124. // next Decode call, and kFail otherwise.
  125. VP9Accelerator::Status DecodeAndOutputPicture(scoped_refptr<VP9Picture> pic);
  126. // Get frame context state after decoding |pic| from the accelerator, and call
  127. // |context_refresh_cb| with the acquired state.
  128. void UpdateFrameContext(scoped_refptr<VP9Picture> pic,
  129. Vp9Parser::ContextRefreshCallback context_refresh_cb);
  130. // Called on error, when decoding cannot continue. Sets state_ to kError and
  131. // releases current state.
  132. void SetError();
  133. enum State {
  134. kNeedStreamMetadata, // After initialization, need a keyframe.
  135. kDecoding, // Ready to decode from any point.
  136. kAfterReset, // After Reset(), need a resume point.
  137. kError, // Error in decode, can't continue.
  138. };
  139. // Current decoder state.
  140. State state_;
  141. // Current stream buffer id; to be assigned to pictures decoded from it.
  142. int32_t stream_id_ = -1;
  143. // Current frame header and decrypt config to be used in decoding the next
  144. // picture.
  145. std::unique_ptr<Vp9FrameHeader> curr_frame_hdr_;
  146. std::unique_ptr<DecryptConfig> decrypt_config_;
  147. // Current frame size that is necessary to decode |curr_frame_hdr_|.
  148. gfx::Size curr_frame_size_;
  149. // Color space provided by the container.
  150. const VideoColorSpace container_color_space_;
  151. // Reference frames currently in use.
  152. Vp9ReferenceFrameVector ref_frames_;
  153. // Current coded resolution.
  154. gfx::Size pic_size_;
  155. // Visible rectangle on the most recent allocation.
  156. gfx::Rect visible_rect_;
  157. // Profile of input bitstream.
  158. VideoCodecProfile profile_;
  159. // Bit depth of input bitstream.
  160. uint8_t bit_depth_ = 0;
  161. // Chroma subsampling format of input bitstream.
  162. VideoChromaSampling chroma_sampling_ = VideoChromaSampling::kUnknown;
  163. // Pending picture for decode when accelerator returns kTryAgain.
  164. scoped_refptr<VP9Picture> pending_pic_;
  165. size_t size_change_failure_counter_ = 0;
  166. const std::unique_ptr<VP9Accelerator> accelerator_;
  167. Vp9Parser parser_;
  168. };
  169. } // namespace media
  170. #endif // MEDIA_GPU_VP9_DECODER_H_