vp8_decoder.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_VP8_DECODER_H_
  5. #define MEDIA_GPU_VP8_DECODER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include "base/memory/ref_counted.h"
  10. #include "media/gpu/accelerated_video_decoder.h"
  11. #include "media/gpu/vp8_picture.h"
  12. #include "media/gpu/vp8_reference_frame_vector.h"
  13. #include "media/parsers/vp8_parser.h"
  14. namespace media {
  15. // Clients of this class are expected to pass raw VP8 stream and are expected
  16. // to provide an implementation of VP8Accelerator for offloading final steps
  17. // of the decoding process.
  18. //
  19. // This class must be created, called and destroyed on a single thread, and
  20. // does nothing internally on any other thread.
  21. class MEDIA_GPU_EXPORT VP8Decoder : public AcceleratedVideoDecoder {
  22. public:
  23. class MEDIA_GPU_EXPORT VP8Accelerator {
  24. public:
  25. VP8Accelerator();
  26. VP8Accelerator(const VP8Accelerator&) = delete;
  27. VP8Accelerator& operator=(const VP8Accelerator&) = delete;
  28. virtual ~VP8Accelerator();
  29. // Create a new VP8Picture that the decoder client can use for decoding
  30. // and pass back to this accelerator for decoding or reference.
  31. // When the picture is no longer needed by decoder, it will just drop
  32. // its reference to it, and it may do so at any time.
  33. // Note that this may return nullptr if accelerator is not able to provide
  34. // any new pictures at given time. The decoder is expected to handle
  35. // this situation as normal and return from Decode() with kRanOutOfSurfaces.
  36. virtual scoped_refptr<VP8Picture> CreateVP8Picture() = 0;
  37. // Submits decode for |pic|, using |reference_frames| as references, as per
  38. // VP8 specification. Returns true if successful.
  39. virtual bool SubmitDecode(
  40. scoped_refptr<VP8Picture> pic,
  41. const Vp8ReferenceFrameVector& reference_frames) = 0;
  42. // Schedule output (display) of |pic|. Note that returning from this
  43. // method does not mean that |pic| has already been outputted (displayed),
  44. // but guarantees that all pictures will be outputted in the same order
  45. // as this method was called for them. Decoder may drop its reference
  46. // to |pic| after calling this method.
  47. // Return true if successful.
  48. virtual bool OutputPicture(scoped_refptr<VP8Picture> pic) = 0;
  49. };
  50. explicit VP8Decoder(
  51. std::unique_ptr<VP8Accelerator> accelerator,
  52. const VideoColorSpace& container_color_space = VideoColorSpace());
  53. VP8Decoder(const VP8Decoder&) = delete;
  54. VP8Decoder& operator=(const VP8Decoder&) = delete;
  55. ~VP8Decoder() override;
  56. // AcceleratedVideoDecoder implementation.
  57. void SetStream(int32_t id, const DecoderBuffer& decoder_buffer) override;
  58. [[nodiscard]] bool Flush() override;
  59. void Reset() override;
  60. [[nodiscard]] DecodeResult Decode() override;
  61. gfx::Size GetPicSize() const override;
  62. gfx::Rect GetVisibleRect() const override;
  63. VideoCodecProfile GetProfile() const override;
  64. uint8_t GetBitDepth() const override;
  65. VideoChromaSampling GetChromaSampling() const override;
  66. size_t GetRequiredNumOfPictures() const override;
  67. size_t GetNumReferenceFrames() const override;
  68. private:
  69. bool DecodeAndOutputCurrentFrame(scoped_refptr<VP8Picture> pic);
  70. enum State {
  71. kNeedStreamMetadata, // After initialization, need a keyframe.
  72. kDecoding, // Ready to decode from any point.
  73. kAfterReset, // After Reset(), need a resume point.
  74. kError, // Error in decode, can't continue.
  75. };
  76. State state_;
  77. Vp8Parser parser_;
  78. std::unique_ptr<Vp8FrameHeader> curr_frame_hdr_;
  79. Vp8ReferenceFrameVector ref_frames_;
  80. // Current stream buffer id; to be assigned to pictures decoded from it.
  81. static constexpr int32_t kInvalidId = -1;
  82. int32_t stream_id_ = kInvalidId;
  83. int32_t last_decoded_stream_id_ = kInvalidId;
  84. size_t size_change_failure_counter_ = 0;
  85. const uint8_t* curr_frame_start_;
  86. size_t frame_size_;
  87. gfx::Size pic_size_;
  88. int horizontal_scale_;
  89. int vertical_scale_;
  90. const std::unique_ptr<VP8Accelerator> accelerator_;
  91. // Color space provided by the container.
  92. const VideoColorSpace container_color_space_;
  93. };
  94. } // namespace media
  95. #endif // MEDIA_GPU_VP8_DECODER_H_