1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_FILTERS_GAV1_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_GAV1_VIDEO_DECODER_H_
12 #include "base/containers/queue.h"
13 #include "base/memory/scoped_refptr.h"
14 #include "base/sequence_checker.h"
15 #include "media/base/media_export.h"
16 #include "media/base/supported_video_decoder_config.h"
17 #include "media/base/video_aspect_ratio.h"
18 #include "media/base/video_decoder_config.h"
19 #include "media/base/video_frame_pool.h"
20 #include "media/filters/offloading_video_decoder.h"
24 } // namespace libgav1
29 class MEDIA_EXPORT Gav1VideoDecoder : public OffloadableVideoDecoder {
31 static SupportedVideoDecoderConfigs SupportedConfigs();
33 explicit Gav1VideoDecoder(MediaLog* media_log,
34 OffloadState offload_state = OffloadState::kNormal);
35 ~Gav1VideoDecoder() override;
36 Gav1VideoDecoder(const Gav1VideoDecoder&) = delete;
37 Gav1VideoDecoder& operator=(const Gav1VideoDecoder&) = delete;
39 // VideoDecoder implementation.
40 VideoDecoderType GetDecoderType() const override;
41 void Initialize(const VideoDecoderConfig& config,
43 CdmContext* cdm_context,
45 const OutputCB& output_cb,
46 const WaitingCB& waiting_cb) override;
47 void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
48 void Reset(base::OnceClosure reset_cb) override;
50 // OffloadableVideoDecoder implementation.
51 void Detach() override;
53 scoped_refptr<VideoFrame> CreateVideoFrame(VideoPixelFormat format,
54 const gfx::Size& coded_size,
55 const gfx::Rect& visible_rect);
58 enum class DecoderState {
66 // Invokes the decoder and calls |output_cb_| for any returned frames.
67 bool DecodeBuffer(scoped_refptr<DecoderBuffer> buffer);
69 // Used to report error messages to the client.
70 MediaLog* const media_log_;
71 const bool bind_callbacks_;
73 // Info configured in Initialize(). These are used in outputting frames.
74 VideoColorSpace color_space_;
75 VideoAspectRatio aspect_ratio_;
77 DecoderState state_ = DecoderState::kUninitialized;
79 // A decoded buffer used in libgav1 is allocated and managed by
80 // |frame_pool_|. The buffer can be reused only if libgav1's decoder doesn't
81 // use the buffer and rendering the frame is complete.
82 VideoFramePool frame_pool_;
85 std::unique_ptr<libgav1::Decoder> libgav1_decoder_;
87 SEQUENCE_CHECKER(sequence_checker_);
90 // Helper class for creating a Gav1VideoDecoder which will offload all AV1
91 // content from the media thread.
92 class OffloadingGav1VideoDecoder : public OffloadingVideoDecoder {
94 explicit OffloadingGav1VideoDecoder(MediaLog* media_log)
95 : OffloadingVideoDecoder(
97 std::vector<VideoCodec>(1, VideoCodec::kAV1),
98 std::make_unique<Gav1VideoDecoder>(
100 OffloadableVideoDecoder::OffloadState::kOffloaded)) {}
103 #endif // MEDIA_FILTERS_GAV1_VIDEO_DECODER_H_