[M120 Migration][hbbtv] Audio tracks count notification
[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 <memory>
9
10 #include "base/functional/callback_forward.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/raw_ptr_exclusion.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "base/sequence_checker.h"
15 #include "media/base/media_log.h"
16 #include "media/base/supported_video_decoder_config.h"
17 #include "media/base/video_decoder.h"
18 #include "media/base/video_decoder_config.h"
19 #include "media/base/video_frame.h"
20 #include "media/filters/offloading_video_decoder.h"
21
22 struct Dav1dContext;
23 struct Dav1dPicture;
24
25 namespace media {
26
27 class MEDIA_EXPORT Dav1dVideoDecoder : public OffloadableVideoDecoder {
28  public:
29   static SupportedVideoDecoderConfigs SupportedConfigs();
30
31   explicit Dav1dVideoDecoder(
32       std::unique_ptr<MediaLog> media_log,
33       OffloadState offload_state = OffloadState::kNormal);
34
35   Dav1dVideoDecoder(const Dav1dVideoDecoder&) = delete;
36   Dav1dVideoDecoder& operator=(const Dav1dVideoDecoder&) = delete;
37
38   ~Dav1dVideoDecoder() override;
39
40   // VideoDecoder implementation.
41   VideoDecoderType GetDecoderType() const override;
42   void Initialize(const VideoDecoderConfig& config,
43                   bool low_delay,
44                   CdmContext* cdm_context,
45                   InitCB init_cb,
46                   const OutputCB& output_cb,
47                   const WaitingCB& waiting_cb) override;
48   void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
49   void Reset(base::OnceClosure reset_cb) override;
50
51   // OffloadableVideoDecoder implementation.
52   void Detach() override;
53
54  private:
55   enum class DecoderState {
56     kUninitialized,
57     kNormal,
58     kFlushCodec,
59     kDecodeFinished,
60     kError
61   };
62
63   // Releases any configured decoder and clears |dav1d_decoder_|.
64   void CloseDecoder();
65
66   // Invokes the decoder and calls |output_cb_| for any returned frames.
67   bool DecodeBuffer(scoped_refptr<DecoderBuffer> buffer);
68
69   scoped_refptr<VideoFrame> BindImageToVideoFrame(const Dav1dPicture* img);
70
71   // Used to report error messages to the client.
72   std::unique_ptr<MediaLog> media_log_;
73
74   // Indicates if the decoder is being wrapped by OffloadVideoDecoder; controls
75   // whether callbacks are bound to the current loop on calls.
76   const bool bind_callbacks_;
77
78   SEQUENCE_CHECKER(sequence_checker_);
79
80   // "Zero" filled UV data for monochrome images to use since Chromium doesn't
81   // have support for I400P(8|10|12) images.
82   scoped_refptr<base::RefCountedBytes> fake_uv_data_;
83
84   // Current decoder state. Used to ensure methods are called as expected.
85   DecoderState state_ = DecoderState::kUninitialized;
86
87   // Callback given during Initialize() used for delivering decoded frames.
88   OutputCB output_cb_;
89
90   // The configuration passed to Initialize(), saved since some fields are
91   // needed to annotate video frames after decoding.
92   VideoDecoderConfig config_;
93
94   // The allocated decoder; null before Initialize() and anytime after
95   // CloseDecoder().
96   // This field is not a raw_ptr<> because it was filtered by the rewriter for:
97   // #addr-of
98   RAW_PTR_EXCLUSION Dav1dContext* dav1d_decoder_ = nullptr;
99 };
100
101 // Helper class for creating a Dav1dVideoDecoder which will offload all AV1
102 // content from the media thread.
103 class OffloadingDav1dVideoDecoder : public OffloadingVideoDecoder {
104  public:
105   explicit OffloadingDav1dVideoDecoder(std::unique_ptr<MediaLog> media_log)
106       : OffloadingVideoDecoder(
107             0,
108             std::vector<VideoCodec>(1, VideoCodec::kAV1),
109             std::make_unique<Dav1dVideoDecoder>(
110                 std::move(media_log),
111                 OffloadableVideoDecoder::OffloadState::kOffloaded)) {}
112 };
113
114 }  // namespace media
115
116 #endif  // MEDIA_FILTERS_DAV1D_VIDEO_DECODER_H_