ffmpeg_audio_decoder.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_
  5. #define MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "media/base/audio_buffer.h"
  11. #include "media/base/audio_decoder.h"
  12. #include "media/base/demuxer_stream.h"
  13. #include "media/base/media_log.h"
  14. #include "media/base/sample_format.h"
  15. #include "media/ffmpeg/ffmpeg_deleters.h"
  16. struct AVCodecContext;
  17. struct AVFrame;
  18. namespace base {
  19. class SequencedTaskRunner;
  20. }
  21. namespace media {
  22. class AudioDiscardHelper;
  23. class DecoderBuffer;
  24. class FFmpegDecodingLoop;
  25. class MEDIA_EXPORT FFmpegAudioDecoder : public AudioDecoder {
  26. public:
  27. FFmpegAudioDecoder() = delete;
  28. FFmpegAudioDecoder(
  29. const scoped_refptr<base::SequencedTaskRunner>& task_runner,
  30. MediaLog* media_log);
  31. FFmpegAudioDecoder(const FFmpegAudioDecoder&) = delete;
  32. FFmpegAudioDecoder& operator=(const FFmpegAudioDecoder&) = delete;
  33. ~FFmpegAudioDecoder() override;
  34. // AudioDecoder implementation.
  35. AudioDecoderType GetDecoderType() const override;
  36. void Initialize(const AudioDecoderConfig& config,
  37. CdmContext* cdm_context,
  38. InitCB init_cb,
  39. const OutputCB& output_cb,
  40. const WaitingCB& waiting_cb) override;
  41. void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
  42. void Reset(base::OnceClosure closure) override;
  43. // Callback called from within FFmpeg to allocate a buffer based on the
  44. // properties of |codec_context| and |frame|. See AVCodecContext.get_buffer2
  45. // documentation inside FFmpeg.
  46. int GetAudioBuffer(struct AVCodecContext* s, AVFrame* frame, int flags);
  47. private:
  48. // There are four states the decoder can be in:
  49. //
  50. // - kUninitialized: The decoder is not initialized.
  51. // - kNormal: This is the normal state. The decoder is idle and ready to
  52. // decode input buffers, or is decoding an input buffer.
  53. // - kDecodeFinished: EOS buffer received, codec flushed and decode finished.
  54. // No further Decode() call should be made.
  55. // - kError: Unexpected error happened.
  56. //
  57. // These are the possible state transitions.
  58. //
  59. // kUninitialized -> kNormal:
  60. // The decoder is successfully initialized and is ready to decode buffers.
  61. // kNormal -> kDecodeFinished:
  62. // When buffer->end_of_stream() is true.
  63. // kNormal -> kError:
  64. // A decoding error occurs and decoding needs to stop.
  65. // (any state) -> kNormal:
  66. // Any time Reset() is called.
  67. enum class DecoderState { kUninitialized, kNormal, kDecodeFinished, kError };
  68. // Reset decoder and call |reset_cb_|.
  69. void DoReset();
  70. // Handles decoding an unencrypted encoded buffer.
  71. void DecodeBuffer(const DecoderBuffer& buffer, DecodeCB decode_cb);
  72. bool FFmpegDecode(const DecoderBuffer& buffer);
  73. bool OnNewFrame(const DecoderBuffer& buffer,
  74. bool* decoded_frame_this_loop,
  75. AVFrame* frame);
  76. // Handles (re-)initializing the decoder with a (new) config.
  77. // Returns true if initialization was successful.
  78. bool ConfigureDecoder(const AudioDecoderConfig& config);
  79. // Releases resources associated with |codec_context_| .
  80. void ReleaseFFmpegResources();
  81. void ResetTimestampState(const AudioDecoderConfig& config);
  82. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  83. SEQUENCE_CHECKER(sequence_checker_);
  84. OutputCB output_cb_;
  85. DecoderState state_;
  86. // FFmpeg structures owned by this object.
  87. std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
  88. AudioDecoderConfig config_;
  89. // AVSampleFormat initially requested; not Chrome's SampleFormat.
  90. int av_sample_format_;
  91. std::unique_ptr<AudioDiscardHelper> discard_helper_;
  92. raw_ptr<MediaLog> media_log_;
  93. scoped_refptr<AudioBufferMemoryPool> pool_;
  94. std::unique_ptr<FFmpegDecodingLoop> decoding_loop_;
  95. };
  96. } // namespace media
  97. #endif // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_