Revert "[M120 Migration]Fix for crash during chrome exit"
[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/functional/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/scoped_refptr.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/ffmpeg/ffmpeg_deleters.h"
18 #include "media/filters/frame_buffer_pool.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
33   explicit FFmpegVideoDecoder(MediaLog* media_log);
34
35   FFmpegVideoDecoder(const FFmpegVideoDecoder&) = delete;
36   FFmpegVideoDecoder& operator=(const FFmpegVideoDecoder&) = delete;
37
38   ~FFmpegVideoDecoder() override;
39
40   // Allow decoding of individual NALU. Entire frames are required by default.
41   // Disables low-latency mode. Must be called before Initialize().
42   void set_decode_nalus(bool decode_nalus) { decode_nalus_ = decode_nalus; }
43
44   // VideoDecoder implementation.
45   VideoDecoderType GetDecoderType() const override;
46   void Initialize(const VideoDecoderConfig& config,
47                   bool low_delay,
48                   CdmContext* cdm_context,
49                   InitCB init_cb,
50                   const OutputCB& output_cb,
51                   const WaitingCB& waiting_cb) override;
52   void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
53   void Reset(base::OnceClosure closure) override;
54
55   // Callback called from within FFmpeg to allocate a buffer based on
56   // the dimensions of |codec_context|. See AVCodecContext.get_buffer2
57   // documentation inside FFmpeg.
58   int GetVideoBuffer(struct AVCodecContext* codec_context,
59                      AVFrame* frame,
60                      int flags);
61
62   void force_allocation_error_for_testing() { force_allocation_error_ = true; }
63
64  private:
65   enum class DecoderState { kUninitialized, kNormal, kDecodeFinished, kError };
66
67   // Handles decoding of an unencrypted encoded buffer. A return value of false
68   // indicates that an error has occurred.
69   bool FFmpegDecode(const DecoderBuffer& buffer);
70   bool OnNewFrame(AVFrame* frame);
71
72   // Handles (re-)initializing the decoder with a (new) config.
73   // Returns true if initialization was successful.
74   bool ConfigureDecoder(const VideoDecoderConfig& config, bool low_delay);
75
76   // Releases resources associated with |codec_context_|.
77   void ReleaseFFmpegResources();
78
79   SEQUENCE_CHECKER(sequence_checker_);
80
81   const raw_ptr<MediaLog, DanglingUntriaged> media_log_;
82
83   DecoderState state_ = DecoderState::kUninitialized;
84
85   OutputCB output_cb_;
86
87   // FFmpeg structures owned by this object.
88   std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
89
90   VideoDecoderConfig config_;
91
92   scoped_refptr<FrameBufferPool> frame_pool_;
93
94   bool decode_nalus_ = false;
95
96   bool force_allocation_error_ = false;
97
98   std::unique_ptr<FFmpegDecodingLoop> decoding_loop_;
99 };
100
101 }  // namespace media
102
103 #endif  // MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_