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 #include "media/filters/audio_file_reader.h" | 5 #include "media/filters/audio_file_reader.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "media/base/audio_bus.h" | 9 #include "media/base/audio_bus.h" |
10 #include "media/ffmpeg/ffmpeg_common.h" | 10 #include "media/ffmpeg/ffmpeg_common.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 | 72 |
73 int result = avformat_find_stream_info(format_context, NULL); | 73 int result = avformat_find_stream_info(format_context, NULL); |
74 if (result < 0) { | 74 if (result < 0) { |
75 DLOG(WARNING) | 75 DLOG(WARNING) |
76 << "AudioFileReader::Open() : error in avformat_find_stream_info()"; | 76 << "AudioFileReader::Open() : error in avformat_find_stream_info()"; |
77 return false; | 77 return false; |
78 } | 78 } |
79 | 79 |
80 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 80 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
81 if (codec) { | 81 if (codec) { |
82 // MP3 decodes to S16P which we don't support, tell it to use S16 instead. | |
scherkus (not reviewing)
2012/12/13 22:40:11
should we also be checking the codec id?
DaleCurtis
2012/12/13 22:51:28
We could but, but this is more generic and the che
| |
83 bool request_s16_format = codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P; | |
scherkus (not reviewing)
2012/12/13 22:40:11
how about instead of a bool (which I found a bit t
DaleCurtis
2012/12/13 22:51:28
Sounds good. Will do so in next patch set.
DaleCurtis
2012/12/14 20:48:32
Done.
| |
84 if (request_s16_format) | |
85 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; | |
86 | |
82 if ((result = avcodec_open2(codec_context_, codec, NULL)) < 0) { | 87 if ((result = avcodec_open2(codec_context_, codec, NULL)) < 0) { |
83 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" | 88 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" |
84 << " result: " << result; | 89 << " result: " << result; |
90 return false; | |
91 } | |
92 | |
93 // Ensure avcodec_open2() respected our format request. | |
94 if (request_s16_format && codec_context_->sample_fmt != AV_SAMPLE_FMT_S16) { | |
95 DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a" | |
96 << " supported sample format - " | |
97 << codec_context_->sample_fmt; | |
85 return false; | 98 return false; |
86 } | 99 } |
87 } else { | 100 } else { |
88 DLOG(WARNING) << "AudioFileReader::Open() : could not find codec -" | 101 DLOG(WARNING) << "AudioFileReader::Open() : could not find codec -" |
89 << " result: " << result; | 102 << " result: " << result; |
90 return false; | 103 return false; |
91 } | 104 } |
92 | 105 |
93 return true; | 106 return true; |
94 } | 107 } |
95 | 108 |
96 void AudioFileReader::Close() { | 109 void AudioFileReader::Close() { |
97 if (codec_context_) { | 110 if (codec_context_) { |
98 avcodec_close(codec_context_); | 111 avcodec_close(codec_context_); |
99 codec_context_ = NULL; | 112 codec_context_ = NULL; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 int frames_read = av_frame->nb_samples; | 169 int frames_read = av_frame->nb_samples; |
157 if (frames_read < 0) { | 170 if (frames_read < 0) { |
158 continue_decoding = false; | 171 continue_decoding = false; |
159 break; | 172 break; |
160 } | 173 } |
161 | 174 |
162 // Truncate, if necessary, if the destination isn't big enough. | 175 // Truncate, if necessary, if the destination isn't big enough. |
163 if (current_frame + frames_read > audio_bus->frames()) | 176 if (current_frame + frames_read > audio_bus->frames()) |
164 frames_read = audio_bus->frames() - current_frame; | 177 frames_read = audio_bus->frames() - current_frame; |
165 | 178 |
166 // Deinterleave each channel and convert to 32bit floating-point | 179 // Deinterleave each channel and convert to 32bit floating-point with |
167 // with nominal range -1.0 -> +1.0. | 180 // nominal range -1.0 -> +1.0. If the output is already in float planar |
168 audio_bus->FromInterleavedPartial( | 181 // format, just copy it into the AudioBus. |
169 av_frame->data[0], current_frame, frames_read, bytes_per_sample); | 182 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { |
183 float* decoded_audio_data = reinterpret_cast<float*>(av_frame->data[0]); | |
184 int channels = audio_bus->channels(); | |
185 for (int ch = 0; ch < channels; ++ch) { | |
186 float* bus_data = audio_bus->channel(ch) + current_frame; | |
187 for (int i = 0, offset = ch; i < frames_read; | |
188 ++i, offset += channels) { | |
189 bus_data[i] = decoded_audio_data[offset]; | |
190 } | |
191 } | |
192 } else if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP) { | |
193 for (int ch = 0; ch < audio_bus->channels(); ++ch) { | |
194 memcpy(audio_bus->channel(ch) + current_frame, | |
195 av_frame->extended_data[ch], sizeof(float) * frames_read); | |
196 } | |
197 } else { | |
198 audio_bus->FromInterleavedPartial( | |
199 av_frame->data[0], current_frame, frames_read, bytes_per_sample); | |
200 } | |
170 | 201 |
171 current_frame += frames_read; | 202 current_frame += frames_read; |
172 } while (packet_temp.size > 0); | 203 } while (packet_temp.size > 0); |
173 av_free_packet(&packet); | 204 av_free_packet(&packet); |
174 } | 205 } |
175 | 206 |
176 // Zero any remaining frames. | 207 // Zero any remaining frames. |
177 audio_bus->ZeroFramesPartial( | 208 audio_bus->ZeroFramesPartial( |
178 current_frame, audio_bus->frames() - current_frame); | 209 current_frame, audio_bus->frames() - current_frame); |
179 | 210 |
180 // Returns the actual number of sample-frames decoded. | 211 // Returns the actual number of sample-frames decoded. |
181 // Ideally this represents the "true" exact length of the file. | 212 // Ideally this represents the "true" exact length of the file. |
182 return current_frame; | 213 return current_frame; |
183 } | 214 } |
184 | 215 |
185 } // namespace media | 216 } // namespace media |
OLD | NEW |