| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ | 5 #ifndef MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ |
| 6 #define MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ | 6 #define MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "media/base/audio_decoder.h" | 14 #include "media/base/audio_decoder.h" |
| 15 #include "media/base/demuxer_stream.h" | 15 #include "media/base/demuxer_stream.h" |
| 16 #include "media/base/sample_format.h" | 16 #include "media/base/sample_format.h" |
| 17 | 17 |
| 18 struct AVCodecContext; | 18 struct AVCodecContext; |
| 19 struct AVFrame; | 19 struct AVFrame; |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 class SingleThreadTaskRunner; | 22 class SingleThreadTaskRunner; |
| 23 } | 23 } |
| 24 | 24 |
| 25 namespace media { | 25 namespace media { |
| 26 | 26 |
| 27 class AudioTimestampHelper; | 27 class AudioTimestampHelper; |
| 28 class DecoderBuffer; | 28 class DecoderBuffer; |
| 29 struct QueuedAudioBuffer; | |
| 30 class ScopedPtrAVFreeContext; | 29 class ScopedPtrAVFreeContext; |
| 31 class ScopedPtrAVFreeFrame; | 30 class ScopedPtrAVFreeFrame; |
| 32 | 31 |
| 33 class MEDIA_EXPORT FFmpegAudioDecoder : public AudioDecoder { | 32 class MEDIA_EXPORT FFmpegAudioDecoder : public AudioDecoder { |
| 34 public: | 33 public: |
| 35 explicit FFmpegAudioDecoder( | 34 explicit FFmpegAudioDecoder( |
| 36 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | 35 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
| 37 virtual ~FFmpegAudioDecoder(); | 36 virtual ~FFmpegAudioDecoder(); |
| 38 | 37 |
| 39 // AudioDecoder implementation. | 38 // AudioDecoder implementation. |
| 40 virtual void Initialize(DemuxerStream* stream, | 39 virtual void Initialize(const AudioDecoderConfig& config, |
| 41 const PipelineStatusCB& status_cb, | 40 const PipelineStatusCB& status_cb) OVERRIDE; |
| 42 const StatisticsCB& statistics_cb) OVERRIDE; | 41 virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer, |
| 43 virtual void Read(const ReadCB& read_cb) OVERRIDE; | 42 const DecodeCB& decode_cb) OVERRIDE; |
| 43 virtual scoped_refptr<AudioBuffer> GetDecodeOutput() OVERRIDE; |
| 44 virtual int bits_per_channel() OVERRIDE; | 44 virtual int bits_per_channel() OVERRIDE; |
| 45 virtual ChannelLayout channel_layout() OVERRIDE; | 45 virtual ChannelLayout channel_layout() OVERRIDE; |
| 46 virtual int samples_per_second() OVERRIDE; | 46 virtual int samples_per_second() OVERRIDE; |
| 47 virtual void Reset(const base::Closure& closure) OVERRIDE; | 47 virtual void Reset(const base::Closure& closure) OVERRIDE; |
| 48 virtual void Stop(const base::Closure& closure) OVERRIDE; | 48 virtual void Stop(const base::Closure& closure) OVERRIDE; |
| 49 | 49 |
| 50 // Callback called from within FFmpeg to allocate a buffer based on | 50 private: |
| 51 // the dimensions of |codec_context|. See AVCodecContext.get_buffer2 | 51 enum DecoderState { |
| 52 // documentation inside FFmpeg. | 52 kUninitialized, |
| 53 int GetAudioBuffer(AVCodecContext* codec, AVFrame* frame, int flags); | 53 kNormal, |
| 54 kFlushCodec, |
| 55 kDecodeFinished, |
| 56 kError |
| 57 }; |
| 54 | 58 |
| 55 private: | 59 // Reset decoder and call |reset_cb_|. |
| 56 void DoStop(); | |
| 57 void DoReset(); | 60 void DoReset(); |
| 58 | 61 |
| 59 // Reads from the demuxer stream with corresponding callback method. | 62 // Handles decoding an unencrypted encoded buffer. |
| 60 void ReadFromDemuxerStream(); | 63 void DecodeBuffer(const scoped_refptr<DecoderBuffer>& buffer, |
| 61 void BufferReady(DemuxerStream::Status status, | 64 const DecodeCB& decode_cb); |
| 62 const scoped_refptr<DecoderBuffer>& input); | 65 bool FFmpegDecode(const scoped_refptr<DecoderBuffer>& buffer); |
| 63 | 66 |
| 67 // Handles (re-)initializing the decoder with a (new) config. |
| 68 // Returns true if initialization was successful. |
| 64 bool ConfigureDecoder(); | 69 bool ConfigureDecoder(); |
| 70 |
| 71 // Releases resources associated with |codec_context_| and |av_frame_| |
| 72 // and resets them to NULL. |
| 65 void ReleaseFFmpegResources(); | 73 void ReleaseFFmpegResources(); |
| 66 void ResetTimestampState(); | 74 void ResetTimestampState(); |
| 67 void RunDecodeLoop(const scoped_refptr<DecoderBuffer>& input, | |
| 68 bool skip_eos_append); | |
| 69 | 75 |
| 70 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 76 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 71 base::WeakPtrFactory<FFmpegAudioDecoder> weak_factory_; | 77 base::WeakPtrFactory<FFmpegAudioDecoder> weak_factory_; |
| 72 base::WeakPtr<FFmpegAudioDecoder> weak_this_; | 78 base::WeakPtr<FFmpegAudioDecoder> weak_this_; |
| 73 | 79 |
| 74 DemuxerStream* demuxer_stream_; | 80 DecoderState state_; |
| 75 StatisticsCB statistics_cb_; | 81 |
| 82 // FFmpeg structures owned by this object. |
| 76 scoped_ptr_malloc<AVCodecContext, ScopedPtrAVFreeContext> codec_context_; | 83 scoped_ptr_malloc<AVCodecContext, ScopedPtrAVFreeContext> codec_context_; |
| 84 scoped_ptr_malloc<AVFrame, ScopedPtrAVFreeFrame> av_frame_; |
| 77 | 85 |
| 78 // Decoded audio format. | 86 // Decoded audio format. |
| 79 int bytes_per_channel_; | 87 int bytes_per_channel_; |
| 80 ChannelLayout channel_layout_; | 88 ChannelLayout channel_layout_; |
| 81 int channels_; | 89 int channels_; |
| 82 int samples_per_second_; | 90 int samples_per_second_; |
| 83 | 91 |
| 84 // AVSampleFormat initially requested; not Chrome's SampleFormat. | 92 // AVSampleFormat initially requested; not Chrome's SampleFormat. |
| 85 int av_sample_format_; | 93 int av_sample_format_; |
| 86 SampleFormat sample_format_; | 94 SampleFormat sample_format_; |
| 87 | 95 |
| 96 AudioDecoderConfig config_; |
| 97 |
| 88 // Used for computing output timestamps. | 98 // Used for computing output timestamps. |
| 89 scoped_ptr<AudioTimestampHelper> output_timestamp_helper_; | 99 scoped_ptr<AudioTimestampHelper> output_timestamp_helper_; |
| 90 base::TimeDelta last_input_timestamp_; | 100 base::TimeDelta last_input_timestamp_; |
| 91 | 101 |
| 92 // Number of frames to drop before generating output buffers. | 102 // Number of frames to drop before generating output buffers. |
| 93 int output_frames_to_drop_; | 103 int output_frames_to_drop_; |
| 94 | 104 |
| 95 // Holds decoded audio. | |
| 96 scoped_ptr_malloc<AVFrame, ScopedPtrAVFreeFrame> av_frame_; | |
| 97 | |
| 98 ReadCB read_cb_; | |
| 99 base::Closure stop_cb_; | |
| 100 base::Closure reset_cb_; | |
| 101 | |
| 102 // Since multiple frames may be decoded from the same packet we need to queue | 105 // Since multiple frames may be decoded from the same packet we need to queue |
| 103 // them up and hand them out as we receive Read() calls. | 106 // them up. |
| 104 std::list<QueuedAudioBuffer> queued_audio_; | 107 std::list<scoped_refptr<AudioBuffer> > queued_audio_; |
| 105 | 108 |
| 106 DISALLOW_IMPLICIT_CONSTRUCTORS(FFmpegAudioDecoder); | 109 DISALLOW_IMPLICIT_CONSTRUCTORS(FFmpegAudioDecoder); |
| 107 }; | 110 }; |
| 108 | 111 |
| 109 } // namespace media | 112 } // namespace media |
| 110 | 113 |
| 111 #endif // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ | 114 #endif // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ |
| OLD | NEW |