gav1_video_decoder.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2019 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_FILTERS_GAV1_VIDEO_DECODER_H_
  5. #define MEDIA_FILTERS_GAV1_VIDEO_DECODER_H_
  6. #include <memory>
  7. #include <queue>
  8. #include <vector>
  9. #include "base/containers/queue.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "media/base/media_export.h"
  13. #include "media/base/supported_video_decoder_config.h"
  14. #include "media/base/video_aspect_ratio.h"
  15. #include "media/base/video_decoder_config.h"
  16. #include "media/base/video_frame_pool.h"
  17. #include "media/filters/offloading_video_decoder.h"
  18. namespace libgav1 {
  19. class Decoder;
  20. } // namespace libgav1
  21. namespace media {
  22. class MediaLog;
  23. class MEDIA_EXPORT Gav1VideoDecoder : public OffloadableVideoDecoder {
  24. public:
  25. static SupportedVideoDecoderConfigs SupportedConfigs();
  26. explicit Gav1VideoDecoder(MediaLog* media_log,
  27. OffloadState offload_state = OffloadState::kNormal);
  28. ~Gav1VideoDecoder() override;
  29. Gav1VideoDecoder(const Gav1VideoDecoder&) = delete;
  30. Gav1VideoDecoder& operator=(const Gav1VideoDecoder&) = delete;
  31. // VideoDecoder implementation.
  32. VideoDecoderType GetDecoderType() const override;
  33. void Initialize(const VideoDecoderConfig& config,
  34. bool low_delay,
  35. CdmContext* cdm_context,
  36. InitCB init_cb,
  37. const OutputCB& output_cb,
  38. const WaitingCB& waiting_cb) override;
  39. void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
  40. void Reset(base::OnceClosure reset_cb) override;
  41. // OffloadableVideoDecoder implementation.
  42. void Detach() override;
  43. scoped_refptr<VideoFrame> CreateVideoFrame(VideoPixelFormat format,
  44. const gfx::Size& coded_size,
  45. const gfx::Rect& visible_rect);
  46. private:
  47. enum class DecoderState {
  48. kUninitialized,
  49. kDecoding,
  50. kError,
  51. };
  52. void CloseDecoder();
  53. // Invokes the decoder and calls |output_cb_| for any returned frames.
  54. bool DecodeBuffer(scoped_refptr<DecoderBuffer> buffer);
  55. // Used to report error messages to the client.
  56. MediaLog* const media_log_;
  57. const bool bind_callbacks_;
  58. // Info configured in Initialize(). These are used in outputting frames.
  59. VideoColorSpace color_space_;
  60. VideoAspectRatio aspect_ratio_;
  61. DecoderState state_ = DecoderState::kUninitialized;
  62. // A decoded buffer used in libgav1 is allocated and managed by
  63. // |frame_pool_|. The buffer can be reused only if libgav1's decoder doesn't
  64. // use the buffer and rendering the frame is complete.
  65. VideoFramePool frame_pool_;
  66. OutputCB output_cb_;
  67. std::unique_ptr<libgav1::Decoder> libgav1_decoder_;
  68. SEQUENCE_CHECKER(sequence_checker_);
  69. };
  70. // Helper class for creating a Gav1VideoDecoder which will offload all AV1
  71. // content from the media thread.
  72. class OffloadingGav1VideoDecoder : public OffloadingVideoDecoder {
  73. public:
  74. explicit OffloadingGav1VideoDecoder(MediaLog* media_log)
  75. : OffloadingVideoDecoder(
  76. 0,
  77. std::vector<VideoCodec>(1, VideoCodec::kAV1),
  78. std::make_unique<Gav1VideoDecoder>(
  79. media_log,
  80. OffloadableVideoDecoder::OffloadState::kOffloaded)) {}
  81. };
  82. } // namespace media
  83. #endif // MEDIA_FILTERS_GAV1_VIDEO_DECODER_H_