OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "media/filters/decoder_base.h" | 8 #include <list> |
| 9 |
| 10 #include "base/message_loop.h" |
| 11 #include "media/base/filters.h" |
9 | 12 |
10 struct AVCodecContext; | 13 struct AVCodecContext; |
11 | 14 |
12 namespace media { | 15 namespace media { |
13 | 16 |
14 // Forward declaration for scoped_ptr_malloc. | 17 class DataBuffer; |
15 class ScopedPtrAVFree; | |
16 | 18 |
17 class MEDIA_EXPORT FFmpegAudioDecoder | 19 class MEDIA_EXPORT FFmpegAudioDecoder : public AudioDecoder { |
18 : public DecoderBase<AudioDecoder, Buffer> { | |
19 public: | 20 public: |
20 explicit FFmpegAudioDecoder(MessageLoop* message_loop); | 21 explicit FFmpegAudioDecoder(MessageLoop* message_loop); |
21 virtual ~FFmpegAudioDecoder(); | 22 virtual ~FFmpegAudioDecoder(); |
22 | 23 |
| 24 // Filter implementation. |
| 25 virtual void Flush(FilterCallback* callback) OVERRIDE; |
| 26 |
23 // AudioDecoder implementation. | 27 // AudioDecoder implementation. |
24 virtual void ProduceAudioSamples(scoped_refptr<Buffer> output); | 28 virtual void Initialize(DemuxerStream* stream, FilterCallback* callback, |
25 virtual int bits_per_channel(); | 29 StatisticsCallback* stats_callback) OVERRIDE; |
26 virtual ChannelLayout channel_layout(); | 30 virtual void ProduceAudioSamples(scoped_refptr<Buffer> output) OVERRIDE; |
27 virtual int sample_rate(); | 31 virtual int bits_per_channel() OVERRIDE; |
28 | 32 virtual ChannelLayout channel_layout() OVERRIDE; |
29 protected: | 33 virtual int sample_rate() OVERRIDE; |
30 virtual void DoInitialize(DemuxerStream* demuxer_stream, bool* success, | |
31 Task* done_cb); | |
32 | |
33 virtual void DoSeek(base::TimeDelta time, Task* done_cb); | |
34 | |
35 virtual void DoDecode(Buffer* input); | |
36 | 34 |
37 private: | 35 private: |
38 // Calculates the duration of an audio buffer based on the sample rate, | 36 // Methods running on decoder thread. |
39 // channels and bits per sample given the size in bytes. | 37 void DoInitialize(const scoped_refptr<DemuxerStream>& stream, |
40 base::TimeDelta CalculateDuration(size_t size); | 38 FilterCallback* callback, |
| 39 StatisticsCallback* stats_callback); |
| 40 void DoFlush(FilterCallback* callback); |
| 41 void DoProduceAudioSamples(const scoped_refptr<Buffer>& output); |
| 42 void DoDecodeBuffer(const scoped_refptr<Buffer>& input); |
41 | 43 |
42 // A FFmpeg defined structure that holds decoder information, this variable | 44 // Reads from the demuxer stream with corresponding callback method. |
43 // is initialized in OnInitialize(). | 45 void ReadFromDemuxerStream(); |
| 46 void DecodeBuffer(Buffer* buffer); |
| 47 |
| 48 // Updates the output buffer's duration and timestamp based on the input |
| 49 // buffer. Will fall back to an estimated timestamp if the input lacks a |
| 50 // valid timestamp. |
| 51 void UpdateDurationAndTimestamp(const Buffer* input, DataBuffer* output); |
| 52 |
| 53 // Calculates duration based on size of decoded audio bytes. |
| 54 base::TimeDelta CalculateDuration(int size); |
| 55 |
| 56 MessageLoop* message_loop_; |
| 57 |
| 58 scoped_refptr<DemuxerStream> demuxer_stream_; |
| 59 scoped_ptr<StatisticsCallback> stats_callback_; |
44 AVCodecContext* codec_context_; | 60 AVCodecContext* codec_context_; |
45 | 61 |
46 // Decoded audio format. | 62 // Decoded audio format. |
47 int bits_per_channel_; | 63 int bits_per_channel_; |
48 ChannelLayout channel_layout_; | 64 ChannelLayout channel_layout_; |
49 int sample_rate_; | 65 int sample_rate_; |
50 | 66 |
51 // Estimated timestamp for next packet. Useful for packets without timestamps. | |
52 base::TimeDelta estimated_next_timestamp_; | 67 base::TimeDelta estimated_next_timestamp_; |
53 | 68 |
54 // Data buffer to carry decoded raw PCM samples. This buffer is created by | 69 // Holds decoded audio. As required by FFmpeg, input/output buffers should |
55 // av_malloc() and is used throughout the lifetime of this class. | 70 // be allocated with suitable padding and alignment. av_malloc() provides |
56 scoped_ptr_malloc<uint8, ScopedPtrAVFree> output_buffer_; | 71 // us that guarantee. |
| 72 const int decoded_audio_size_; |
| 73 uint8* decoded_audio_; // Allocated via av_malloc(). |
57 | 74 |
58 static const size_t kOutputBufferSize; | 75 // Holds downstream-provided buffers. |
| 76 std::list<scoped_refptr<Buffer> > output_buffers_; |
59 | 77 |
60 DISALLOW_COPY_AND_ASSIGN(FFmpegAudioDecoder); | 78 // Tracks reads issued for compressed data. |
| 79 int pending_reads_; |
| 80 |
| 81 DISALLOW_IMPLICIT_CONSTRUCTORS(FFmpegAudioDecoder); |
61 }; | 82 }; |
62 | 83 |
63 } // namespace media | 84 } // namespace media |
64 | 85 |
65 #endif // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ | 86 #endif // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_ |
OLD | NEW |