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_AUDIO_DECODER_H_
6 #define MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_
10 #include "base/functional/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/sequence_checker.h"
13 #include "media/base/audio_buffer.h"
14 #include "media/base/audio_decoder.h"
15 #include "media/base/demuxer_stream.h"
16 #include "media/base/media_log.h"
17 #include "media/base/sample_format.h"
18 #include "media/ffmpeg/ffmpeg_deleters.h"
20 struct AVCodecContext;
24 class SequencedTaskRunner;
29 class AudioDiscardHelper;
31 class FFmpegDecodingLoop;
33 class MEDIA_EXPORT FFmpegAudioDecoder : public AudioDecoder {
35 FFmpegAudioDecoder() = delete;
38 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
41 FFmpegAudioDecoder(const FFmpegAudioDecoder&) = delete;
42 FFmpegAudioDecoder& operator=(const FFmpegAudioDecoder&) = delete;
44 ~FFmpegAudioDecoder() override;
46 // AudioDecoder implementation.
47 AudioDecoderType GetDecoderType() const override;
48 void Initialize(const AudioDecoderConfig& config,
49 CdmContext* cdm_context,
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;
56 // Callback called from within FFmpeg to allocate a buffer based on the
57 // properties of |codec_context| and |frame|. See AVCodecContext.get_buffer2
58 // documentation inside FFmpeg.
59 int GetAudioBuffer(struct AVCodecContext* s, AVFrame* frame, int flags);
62 // There are four states the decoder can be in:
64 // - kUninitialized: The decoder is not initialized.
65 // - kNormal: This is the normal state. The decoder is idle and ready to
66 // decode input buffers, or is decoding an input buffer.
67 // - kDecodeFinished: EOS buffer received, codec flushed and decode finished.
68 // No further Decode() call should be made.
69 // - kError: Unexpected error happened.
71 // These are the possible state transitions.
73 // kUninitialized -> kNormal:
74 // The decoder is successfully initialized and is ready to decode buffers.
75 // kNormal -> kDecodeFinished:
76 // When buffer->end_of_stream() is true.
78 // A decoding error occurs and decoding needs to stop.
79 // (any state) -> kNormal:
80 // Any time Reset() is called.
81 enum class DecoderState { kUninitialized, kNormal, kDecodeFinished, kError };
83 // Reset decoder and call |reset_cb_|.
86 // Handles decoding an unencrypted encoded buffer.
87 void DecodeBuffer(const DecoderBuffer& buffer, DecodeCB decode_cb);
88 bool FFmpegDecode(const DecoderBuffer& buffer);
89 bool OnNewFrame(const DecoderBuffer& buffer,
90 bool* decoded_frame_this_loop,
93 // Handles (re-)initializing the decoder with a (new) config.
94 // Returns true if initialization was successful.
95 bool ConfigureDecoder(const AudioDecoderConfig& config);
97 // Releases resources associated with |codec_context_| .
98 void ReleaseFFmpegResources();
100 void ResetTimestampState(const AudioDecoderConfig& config);
102 scoped_refptr<base::SequencedTaskRunner> task_runner_;
103 SEQUENCE_CHECKER(sequence_checker_);
109 // FFmpeg structures owned by this object.
110 std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
112 AudioDecoderConfig config_;
114 // AVSampleFormat initially requested; not Chrome's SampleFormat.
115 int av_sample_format_;
117 std::unique_ptr<AudioDiscardHelper> discard_helper_;
119 raw_ptr<MediaLog, DanglingUntriaged> media_log_;
121 scoped_refptr<AudioBufferMemoryPool> pool_;
123 std::unique_ptr<FFmpegDecodingLoop> decoding_loop_;
128 #endif // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_