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.
5 #ifndef MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
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"
20 struct AVCodecContext;
26 class FFmpegDecodingLoop;
29 class MEDIA_EXPORT FFmpegVideoDecoder : public VideoDecoder {
31 static bool IsCodecSupported(VideoCodec codec);
33 explicit FFmpegVideoDecoder(MediaLog* media_log);
35 FFmpegVideoDecoder(const FFmpegVideoDecoder&) = delete;
36 FFmpegVideoDecoder& operator=(const FFmpegVideoDecoder&) = delete;
38 ~FFmpegVideoDecoder() override;
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; }
44 // VideoDecoder implementation.
45 VideoDecoderType GetDecoderType() const override;
46 void Initialize(const VideoDecoderConfig& config,
48 CdmContext* cdm_context,
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;
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,
62 void force_allocation_error_for_testing() { force_allocation_error_ = true; }
65 enum class DecoderState { kUninitialized, kNormal, kDecodeFinished, kError };
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);
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);
76 // Releases resources associated with |codec_context_|.
77 void ReleaseFFmpegResources();
79 SEQUENCE_CHECKER(sequence_checker_);
81 const raw_ptr<MediaLog, DanglingUntriaged> media_log_;
83 DecoderState state_ = DecoderState::kUninitialized;
87 // FFmpeg structures owned by this object.
88 std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
90 VideoDecoderConfig config_;
92 scoped_refptr<FrameBufferPool> frame_pool_;
94 bool decode_nalus_ = false;
96 bool force_allocation_error_ = false;
98 std::unique_ptr<FFmpegDecodingLoop> decoding_loop_;
103 #endif // MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_