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 5 #ifndef MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ 6 #define MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ 7 8 #include <list> 9 10 #include "base/callback.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/time/time.h" 14 #include "media/base/audio_decoder.h" 15 #include "media/base/demuxer_stream.h" 16 #include "media/base/sample_format.h" 17 18 struct AVCodecContext; 19 struct AVFrame; 20 21 namespace base { 22 class MessageLoopProxy; 23 } 24 25 namespace media { 26 27 class AudioTimestampHelper; 28 class DecoderBuffer; 29 struct QueuedAudioBuffer; 30 class ScopedPtrAVFreeContext; 31 class ScopedPtrAVFreeFrame; 32 33 class MEDIA_EXPORT FFmpegAudioDecoder : public AudioDecoder { 34 public: 35 explicit FFmpegAudioDecoder( 36 const scoped_refptr<base::MessageLoopProxy>& message_loop); 37 virtual ~FFmpegAudioDecoder(); 38 39 // AudioDecoder implementation. 40 virtual void Initialize(DemuxerStream* stream, 41 const PipelineStatusCB& status_cb, 42 const StatisticsCB& statistics_cb) OVERRIDE; 43 virtual void Read(const ReadCB& read_cb) OVERRIDE; 44 virtual int bits_per_channel() OVERRIDE; 45 virtual ChannelLayout channel_layout() OVERRIDE; 46 virtual int samples_per_second() OVERRIDE; 47 virtual void Reset(const base::Closure& closure) OVERRIDE; 48 49 // Callback called from within FFmpeg to allocate a buffer based on 50 // the dimensions of |codec_context|. See AVCodecContext.get_buffer2 51 // documentation inside FFmpeg. 52 int GetAudioBuffer(AVCodecContext* codec, AVFrame* frame, int flags); 53 54 private: 55 // Reads from the demuxer stream with corresponding callback method. 56 void ReadFromDemuxerStream(); 57 void BufferReady(DemuxerStream::Status status, 58 const scoped_refptr<DecoderBuffer>& input); 59 60 bool ConfigureDecoder(); 61 void ReleaseFFmpegResources(); 62 void ResetTimestampState(); 63 void RunDecodeLoop(const scoped_refptr<DecoderBuffer>& input, 64 bool skip_eos_append); 65 66 scoped_refptr<base::MessageLoopProxy> message_loop_; 67 base::WeakPtrFactory<FFmpegAudioDecoder> weak_factory_; 68 base::WeakPtr<FFmpegAudioDecoder> weak_this_; 69 70 DemuxerStream* demuxer_stream_; 71 StatisticsCB statistics_cb_; 72 scoped_ptr_malloc<AVCodecContext, ScopedPtrAVFreeContext> codec_context_; 73 74 // Decoded audio format. 75 int bytes_per_channel_; 76 ChannelLayout channel_layout_; 77 int channels_; 78 int samples_per_second_; 79 80 // AVSampleFormat initially requested; not Chrome's SampleFormat. 81 int av_sample_format_; 82 SampleFormat sample_format_; 83 84 // Used for computing output timestamps. 85 scoped_ptr<AudioTimestampHelper> output_timestamp_helper_; 86 base::TimeDelta last_input_timestamp_; 87 88 // Number of frames to drop before generating output buffers. 89 int output_frames_to_drop_; 90 91 // Holds decoded audio. 92 scoped_ptr_malloc<AVFrame, ScopedPtrAVFreeFrame> av_frame_; 93 94 ReadCB read_cb_; 95 96 // Since multiple frames may be decoded from the same packet we need to queue 97 // them up and hand them out as we receive Read() calls. 98 std::list<QueuedAudioBuffer> queued_audio_; 99 100 DISALLOW_IMPLICIT_CONSTRUCTORS(FFmpegAudioDecoder); 101 }; 102 103 } // namespace media 104 105 #endif // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ 106