Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / dav1d_video_decoder.h
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.
4
5 #ifndef MEDIA_FILTERS_DAV1D_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_DAV1D_VIDEO_DECODER_H_
7
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"
17
18 struct Dav1dContext;
19 struct Dav1dPicture;
20
21 namespace media {
22 class MediaLog;
23
24 class MEDIA_EXPORT Dav1dVideoDecoder : public OffloadableVideoDecoder {
25  public:
26   static SupportedVideoDecoderConfigs SupportedConfigs();
27
28   Dav1dVideoDecoder(MediaLog* media_log,
29                     OffloadState offload_state = OffloadState::kNormal);
30
31   Dav1dVideoDecoder(const Dav1dVideoDecoder&) = delete;
32   Dav1dVideoDecoder& operator=(const Dav1dVideoDecoder&) = delete;
33
34   ~Dav1dVideoDecoder() override;
35
36   // VideoDecoder implementation.
37   VideoDecoderType GetDecoderType() const override;
38   void Initialize(const VideoDecoderConfig& config,
39                   bool low_delay,
40                   CdmContext* cdm_context,
41                   InitCB init_cb,
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;
46
47   // OffloadableVideoDecoder implementation.
48   void Detach() override;
49
50  private:
51   enum class DecoderState {
52     kUninitialized,
53     kNormal,
54     kFlushCodec,
55     kDecodeFinished,
56     kError
57   };
58
59   // Releases any configured decoder and clears |dav1d_decoder_|.
60   void CloseDecoder();
61
62   // Invokes the decoder and calls |output_cb_| for any returned frames.
63   bool DecodeBuffer(scoped_refptr<DecoderBuffer> buffer);
64
65   scoped_refptr<VideoFrame> BindImageToVideoFrame(const Dav1dPicture* img);
66
67   // Used to report error messages to the client.
68   const raw_ptr<MediaLog> media_log_ = nullptr;
69
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_;
73
74   SEQUENCE_CHECKER(sequence_checker_);
75
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_;
79
80   // Current decoder state. Used to ensure methods are called as expected.
81   DecoderState state_ = DecoderState::kUninitialized;
82
83   // Callback given during Initialize() used for delivering decoded frames.
84   OutputCB output_cb_;
85
86   // The configuration passed to Initialize(), saved since some fields are
87   // needed to annotate video frames after decoding.
88   VideoDecoderConfig config_;
89
90   // The allocated decoder; null before Initialize() and anytime after
91   // CloseDecoder().
92   Dav1dContext* dav1d_decoder_ = nullptr;
93 };
94
95 // Helper class for creating a Dav1dVideoDecoder which will offload all AV1
96 // content from the media thread.
97 class OffloadingDav1dVideoDecoder : public OffloadingVideoDecoder {
98  public:
99   explicit OffloadingDav1dVideoDecoder(MediaLog* media_log)
100       : OffloadingVideoDecoder(
101             0,
102             std::vector<VideoCodec>(1, VideoCodec::kAV1),
103             std::make_unique<Dav1dVideoDecoder>(
104                 media_log,
105                 OffloadableVideoDecoder::OffloadState::kOffloaded)) {}
106 };
107
108 }  // namespace media
109
110 #endif  // MEDIA_FILTERS_DAV1D_VIDEO_DECODER_H_