Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: media/filters/audio_file_reader.cc

Issue 5880002: (Committing on behalf of Chris Rogers -- original CL... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/filters/audio_file_reader.h ('k') | media/media.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 #include "media/filters/audio_file_reader.h"
6
7 #include <string>
8 #include "base/basictypes.h"
9 #include "base/string_util.h"
10 #include "base/time.h"
11 #include "media/audio/audio_util.h"
12 #include "media/base/filters.h"
13 #include "media/ffmpeg/ffmpeg_common.h"
14 #include "media/ffmpeg/ffmpeg_util.h"
15 #include "media/filters/ffmpeg_glue.h"
16
17 namespace media {
18
19 AudioFileReader::AudioFileReader(FFmpegURLProtocol* protocol)
20 : protocol_(protocol),
21 format_context_(NULL),
22 codec_context_(NULL),
23 codec_(NULL) {
24 }
25
26 AudioFileReader::~AudioFileReader() {
27 Close();
28 }
29
30 int AudioFileReader::channels() const {
31 return codec_context_->channels;
32 }
33
34 int AudioFileReader::sample_rate() const {
35 return codec_context_->sample_rate;
36 }
37
38 base::TimeDelta AudioFileReader::duration() const {
39 const AVRational av_time_base = {1, AV_TIME_BASE};
40 return ConvertTimestamp(av_time_base, format_context_->duration);
41 }
42
43 int64 AudioFileReader::number_of_frames() const {
44 return static_cast<int64>(duration().InSecondsF() * sample_rate());
45 }
46
47 bool AudioFileReader::Open() {
48 // Add our data reader to the protocol list and get our unique key.
49 std::string key = FFmpegGlue::GetInstance()->AddProtocol(protocol_);
50
51 // Open FFmpeg AVFormatContext.
52 DCHECK(!format_context_);
53 AVFormatContext* context = NULL;
54
55 int result = av_open_input_file(&context, key.c_str(), NULL, 0, NULL);
56
57 // Remove our data reader from protocol list since av_open_input_file() setup
58 // the AVFormatContext with the data reader.
59 FFmpegGlue::GetInstance()->RemoveProtocol(protocol_);
60
61 if (result) {
62 DLOG(WARNING)
63 << "AudioFileReader::Open() : error in av_open_input_file() -"
64 << " result: " << result;
65 return false;
66 }
67
68 DCHECK(context);
69 format_context_ = context;
70
71 // Get the codec context.
72 codec_context_ = NULL;
73 for (size_t i = 0; i < format_context_->nb_streams; ++i) {
74 AVCodecContext* c = format_context_->streams[i]->codec;
75 if (c->codec_type == CODEC_TYPE_AUDIO) {
76 codec_context_ = c;
77 break;
78 }
79 }
80
81 // Get the codec.
82 if (!codec_context_)
83 return false;
84
85 av_find_stream_info(format_context_);
86 codec_ = avcodec_find_decoder(codec_context_->codec_id);
87 if (codec_) {
88 if ((result = avcodec_open(codec_context_, codec_)) < 0) {
89 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -"
90 << " result: " << result;
91 return false;
92 }
93
94 result = av_seek_frame(format_context_, 0, 0, 0);
95 }
96
97 return true;
98 }
99
100 void AudioFileReader::Close() {
101 if (codec_context_ && codec_)
102 avcodec_close(codec_context_);
103
104 codec_context_ = NULL;
105 codec_ = NULL;
106
107 if (format_context_) {
108 av_close_input_file(format_context_);
109 format_context_ = NULL;
110 }
111 }
112
113 bool AudioFileReader::Read(const std::vector<float*>& audio_data,
114 size_t number_of_frames) {
115 size_t channels = this->channels();
116 DCHECK_EQ(audio_data.size(), channels);
117 if (audio_data.size() != channels)
118 return false;
119
120 DCHECK(format_context_ && codec_context_);
121 if (!format_context_ || !codec_context_) {
122 DLOG(WARNING) << "AudioFileReader::Read() : reader is not opened!";
123 return false;
124 }
125
126 scoped_ptr_malloc<int16, ScopedPtrAVFree> output_buffer(
127 static_cast<int16*>(av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE)));
128
129 // Read until we hit EOF or we've read the requested number of frames.
130 AVPacket avpkt;
131 av_init_packet(&avpkt);
132
133 int result = 0;
134 size_t current_frame = 0;
135
136 while (current_frame < number_of_frames &&
137 (result = av_read_frame(format_context_, &avpkt)) >= 0) {
138 int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
139 result = avcodec_decode_audio3(codec_context_,
140 output_buffer.get(),
141 &out_size,
142 &avpkt);
143
144 if (result < 0) {
145 DLOG(WARNING)
146 << "AudioFileReader::Read() : error in avcodec_decode_audio3() -"
147 << result;
148 return false;
149 }
150
151 // Determine the number of sample-frames we just decoded.
152 size_t bytes_per_sample =
153 av_get_bits_per_sample_fmt(codec_context_->sample_fmt) >> 3;
154 size_t frames_read = out_size / (channels * bytes_per_sample);
155
156 // Truncate, if necessary, if the destination isn't big enough.
157 if (current_frame + frames_read > number_of_frames)
158 frames_read = number_of_frames - current_frame;
159
160 // Deinterleave each channel and convert to 32bit floating-point
161 // with nominal range -1.0 -> +1.0.
162 for (size_t channel_index = 0; channel_index < channels;
163 ++channel_index) {
164 if (!DeinterleaveAudioChannel(output_buffer.get(),
165 audio_data[channel_index] + current_frame,
166 channels,
167 channel_index,
168 bytes_per_sample,
169 frames_read)) {
170 DLOG(WARNING)
171 << "AudioFileReader::Read() : Unsupported sample format : "
172 << codec_context_->sample_fmt
173 << " codec_->id : " << codec_->id;
174 return false;
175 }
176 }
177
178 current_frame += frames_read;
179 }
180
181 return true;
182 }
183
184 InMemoryDataReader::InMemoryDataReader(const char* data, int64 size)
185 : data_(data),
186 size_(size),
187 position_(0) {
188 }
189
190 int InMemoryDataReader::Read(int size, uint8* data) {
191 if (size < 0)
192 return -1;
193
194 int available_bytes = static_cast<int>(size_ - position_);
195 if (size > available_bytes)
196 size = available_bytes;
197
198 memcpy(data, data_ + position_, size);
199 position_ += size;
200 return size;
201 }
202
203 bool InMemoryDataReader::GetPosition(int64* position_out) {
204 if (position_out)
205 *position_out = position_;
206 return true;
207 }
208
209 bool InMemoryDataReader::SetPosition(int64 position) {
210 if (position >= size_)
211 return false;
212 position_ = position;
213 return true;
214 }
215
216 bool InMemoryDataReader::GetSize(int64* size_out) {
217 if (size_out)
218 *size_out = size_;
219 return true;
220 }
221
222 bool InMemoryDataReader::IsStreaming() {
223 return false;
224 }
225
226 } // namespace media
OLDNEW
« no previous file with comments | « media/filters/audio_file_reader.h ('k') | media/media.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698