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_DAV1D_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_DAV1D_VIDEO_DECODER_H_
8 #include "base/callback_forward.h"
9 #include "base/memory/raw_ptr.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/sequence_checker.h"
12 #include "media/base/supported_video_decoder_config.h"
13 #include "media/base/video_decoder.h"
14 #include "media/base/video_decoder_config.h"
15 #include "media/base/video_frame.h"
16 #include "media/filters/offloading_video_decoder.h"
24 class MEDIA_EXPORT Dav1dVideoDecoder : public OffloadableVideoDecoder {
26 static SupportedVideoDecoderConfigs SupportedConfigs();
28 Dav1dVideoDecoder(MediaLog* media_log,
29 OffloadState offload_state = OffloadState::kNormal);
31 Dav1dVideoDecoder(const Dav1dVideoDecoder&) = delete;
32 Dav1dVideoDecoder& operator=(const Dav1dVideoDecoder&) = delete;
34 ~Dav1dVideoDecoder() override;
36 // VideoDecoder implementation.
37 VideoDecoderType GetDecoderType() const override;
38 void Initialize(const VideoDecoderConfig& config,
40 CdmContext* cdm_context,
42 const OutputCB& output_cb,
43 const WaitingCB& waiting_cb) override;
44 void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
45 void Reset(base::OnceClosure reset_cb) override;
47 // OffloadableVideoDecoder implementation.
48 void Detach() override;
51 enum class DecoderState {
59 // Releases any configured decoder and clears |dav1d_decoder_|.
62 // Invokes the decoder and calls |output_cb_| for any returned frames.
63 bool DecodeBuffer(scoped_refptr<DecoderBuffer> buffer);
65 scoped_refptr<VideoFrame> BindImageToVideoFrame(const Dav1dPicture* img);
67 // Used to report error messages to the client.
68 const raw_ptr<MediaLog> media_log_ = nullptr;
70 // Indicates if the decoder is being wrapped by OffloadVideoDecoder; controls
71 // whether callbacks are bound to the current loop on calls.
72 const bool bind_callbacks_;
74 SEQUENCE_CHECKER(sequence_checker_);
76 // "Zero" filled UV data for monochrome images to use since Chromium doesn't
77 // have support for I400P(8|10|12) images.
78 scoped_refptr<base::RefCountedBytes> fake_uv_data_;
80 // Current decoder state. Used to ensure methods are called as expected.
81 DecoderState state_ = DecoderState::kUninitialized;
83 // Callback given during Initialize() used for delivering decoded frames.
86 // The configuration passed to Initialize(), saved since some fields are
87 // needed to annotate video frames after decoding.
88 VideoDecoderConfig config_;
90 // The allocated decoder; null before Initialize() and anytime after
92 Dav1dContext* dav1d_decoder_ = nullptr;
95 // Helper class for creating a Dav1dVideoDecoder which will offload all AV1
96 // content from the media thread.
97 class OffloadingDav1dVideoDecoder : public OffloadingVideoDecoder {
99 explicit OffloadingDav1dVideoDecoder(MediaLog* media_log)
100 : OffloadingVideoDecoder(
102 std::vector<VideoCodec>(1, VideoCodec::kAV1),
103 std::make_unique<Dav1dVideoDecoder>(
105 OffloadableVideoDecoder::OffloadState::kOffloaded)) {}
110 #endif // MEDIA_FILTERS_DAV1D_VIDEO_DECODER_H_