Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / gav1_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_GAV1_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_GAV1_VIDEO_DECODER_H_
7
8 #include <memory>
9 #include <queue>
10 #include <vector>
11
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"
21
22 namespace libgav1 {
23 class Decoder;
24 }  // namespace libgav1
25
26 namespace media {
27 class MediaLog;
28
29 class MEDIA_EXPORT Gav1VideoDecoder : public OffloadableVideoDecoder {
30  public:
31   static SupportedVideoDecoderConfigs SupportedConfigs();
32
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;
38
39   // VideoDecoder implementation.
40   VideoDecoderType GetDecoderType() const override;
41   void Initialize(const VideoDecoderConfig& config,
42                   bool low_delay,
43                   CdmContext* cdm_context,
44                   InitCB init_cb,
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;
49
50   // OffloadableVideoDecoder implementation.
51   void Detach() override;
52
53   scoped_refptr<VideoFrame> CreateVideoFrame(VideoPixelFormat format,
54                                              const gfx::Size& coded_size,
55                                              const gfx::Rect& visible_rect);
56
57  private:
58   enum class DecoderState {
59     kUninitialized,
60     kDecoding,
61     kError,
62   };
63
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   // Used to report error messages to the client.
70   MediaLog* const media_log_;
71   const bool bind_callbacks_;
72
73   // Info configured in Initialize(). These are used in outputting frames.
74   VideoColorSpace color_space_;
75   VideoAspectRatio aspect_ratio_;
76
77   DecoderState state_ = DecoderState::kUninitialized;
78
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_;
83
84   OutputCB output_cb_;
85   std::unique_ptr<libgav1::Decoder> libgav1_decoder_;
86
87   SEQUENCE_CHECKER(sequence_checker_);
88 };
89
90 // Helper class for creating a Gav1VideoDecoder which will offload all AV1
91 // content from the media thread.
92 class OffloadingGav1VideoDecoder : public OffloadingVideoDecoder {
93  public:
94   explicit OffloadingGav1VideoDecoder(MediaLog* media_log)
95       : OffloadingVideoDecoder(
96             0,
97             std::vector<VideoCodec>(1, VideoCodec::kAV1),
98             std::make_unique<Gav1VideoDecoder>(
99                 media_log,
100                 OffloadableVideoDecoder::OffloadState::kOffloaded)) {}
101 };
102 }  // namespace media
103 #endif  // MEDIA_FILTERS_GAV1_VIDEO_DECODER_H_