Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / ffmpeg_video_decoder.h
1 // Copyright 2012 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_FFMPEG_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
7
8 #include <memory>
9
10 #include "base/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/sequence_checker.h"
14 #include "media/base/supported_video_decoder_config.h"
15 #include "media/base/video_decoder.h"
16 #include "media/base/video_decoder_config.h"
17 #include "media/base/video_frame_pool.h"
18 #include "media/ffmpeg/ffmpeg_deleters.h"
19
20 struct AVCodecContext;
21 struct AVFrame;
22
23 namespace media {
24
25 class DecoderBuffer;
26 class FFmpegDecodingLoop;
27 class MediaLog;
28
29 class MEDIA_EXPORT FFmpegVideoDecoder : public VideoDecoder {
30  public:
31   static bool IsCodecSupported(VideoCodec codec);
32   static SupportedVideoDecoderConfigs SupportedConfigsForWebRTC();
33
34   explicit FFmpegVideoDecoder(MediaLog* media_log);
35
36   FFmpegVideoDecoder(const FFmpegVideoDecoder&) = delete;
37   FFmpegVideoDecoder& operator=(const FFmpegVideoDecoder&) = delete;
38
39   ~FFmpegVideoDecoder() override;
40
41   // Allow decoding of individual NALU. Entire frames are required by default.
42   // Disables low-latency mode. Must be called before Initialize().
43   void set_decode_nalus(bool decode_nalus) { decode_nalus_ = decode_nalus; }
44
45   // VideoDecoder implementation.
46   VideoDecoderType GetDecoderType() const override;
47   void Initialize(const VideoDecoderConfig& config,
48                   bool low_delay,
49                   CdmContext* cdm_context,
50                   InitCB init_cb,
51                   const OutputCB& output_cb,
52                   const WaitingCB& waiting_cb) override;
53   void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
54   void Reset(base::OnceClosure closure) override;
55
56   // Callback called from within FFmpeg to allocate a buffer based on
57   // the dimensions of |codec_context|. See AVCodecContext.get_buffer2
58   // documentation inside FFmpeg.
59   int GetVideoBuffer(struct AVCodecContext* codec_context,
60                      AVFrame* frame,
61                      int flags);
62
63   void force_allocation_error_for_testing() { force_allocation_error_ = true; }
64
65  private:
66   enum class DecoderState { kUninitialized, kNormal, kDecodeFinished, kError };
67
68   // Handles decoding of an unencrypted encoded buffer. A return value of false
69   // indicates that an error has occurred.
70   bool FFmpegDecode(const DecoderBuffer& buffer);
71   bool OnNewFrame(AVFrame* frame);
72
73   // Handles (re-)initializing the decoder with a (new) config.
74   // Returns true if initialization was successful.
75   bool ConfigureDecoder(const VideoDecoderConfig& config, bool low_delay);
76
77   // Releases resources associated with |codec_context_|.
78   void ReleaseFFmpegResources();
79
80   SEQUENCE_CHECKER(sequence_checker_);
81
82   const raw_ptr<MediaLog> media_log_;
83
84   DecoderState state_ = DecoderState::kUninitialized;
85
86   OutputCB output_cb_;
87
88   // FFmpeg structures owned by this object.
89   std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
90
91   VideoDecoderConfig config_;
92
93   VideoFramePool frame_pool_;
94
95   bool decode_nalus_ = false;
96
97   bool force_allocation_error_ = false;
98
99   std::unique_ptr<FFmpegDecodingLoop> decoding_loop_;
100 };
101
102 }  // namespace media
103
104 #endif  // MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_