| 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 <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 | 119 |
| 120 // Holds decoded audio. | 120 // Holds decoded audio. |
| 121 scoped_ptr<AVFrame, ScopedPtrAVFreeFrame> av_frame(av_frame_alloc()); | 121 scoped_ptr<AVFrame, ScopedPtrAVFreeFrame> av_frame(av_frame_alloc()); |
| 122 | 122 |
| 123 // Read until we hit EOF or we've read the requested number of frames. | 123 // Read until we hit EOF or we've read the requested number of frames. |
| 124 AVPacket packet; | 124 AVPacket packet; |
| 125 int current_frame = 0; | 125 int current_frame = 0; |
| 126 bool continue_decoding = true; | 126 bool continue_decoding = true; |
| 127 | 127 |
| 128 while (current_frame < audio_bus->frames() && continue_decoding && | 128 while (current_frame < audio_bus->frames() && continue_decoding && |
| 129 av_read_frame(glue_->format_context(), &packet) >= 0 && | 129 ReadPacket(&packet)) { |
| 130 av_dup_packet(&packet) >= 0) { | |
| 131 // Skip packets from other streams. | |
| 132 if (packet.stream_index != stream_index_) { | |
| 133 av_free_packet(&packet); | |
| 134 continue; | |
| 135 } | |
| 136 | |
| 137 // Make a shallow copy of packet so we can slide packet.data as frames are | 130 // Make a shallow copy of packet so we can slide packet.data as frames are |
| 138 // decoded from the packet; otherwise av_free_packet() will corrupt memory. | 131 // decoded from the packet; otherwise av_free_packet() will corrupt memory. |
| 139 AVPacket packet_temp = packet; | 132 AVPacket packet_temp = packet; |
| 140 do { | 133 do { |
| 141 avcodec_get_frame_defaults(av_frame.get()); | 134 avcodec_get_frame_defaults(av_frame.get()); |
| 142 int frame_decoded = 0; | 135 int frame_decoded = 0; |
| 143 int result = avcodec_decode_audio4( | 136 int result = avcodec_decode_audio4( |
| 144 codec_context_, av_frame.get(), &frame_decoded, &packet_temp); | 137 codec_context_, av_frame.get(), &frame_decoded, &packet_temp); |
| 145 | 138 |
| 146 if (result < 0) { | 139 if (result < 0) { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 // One microsecond is much less than the time of a single sample-frame | 231 // One microsecond is much less than the time of a single sample-frame |
| 239 // at any real-world sample-rate. | 232 // at any real-world sample-rate. |
| 240 return ConvertFromTimeBase(av_time_base, | 233 return ConvertFromTimeBase(av_time_base, |
| 241 glue_->format_context()->duration + 1); | 234 glue_->format_context()->duration + 1); |
| 242 } | 235 } |
| 243 | 236 |
| 244 int AudioFileReader::GetNumberOfFrames() const { | 237 int AudioFileReader::GetNumberOfFrames() const { |
| 245 return static_cast<int>(ceil(GetDuration().InSecondsF() * sample_rate())); | 238 return static_cast<int>(ceil(GetDuration().InSecondsF() * sample_rate())); |
| 246 } | 239 } |
| 247 | 240 |
| 241 bool AudioFileReader::ReadPacketForTesting(AVPacket* output_packet) { |
| 242 return ReadPacket(output_packet); |
| 243 } |
| 244 |
| 245 bool AudioFileReader::ReadPacket(AVPacket* output_packet) { |
| 246 while (av_read_frame(glue_->format_context(), output_packet) >= 0 && |
| 247 av_dup_packet(output_packet) >= 0) { |
| 248 // Skip packets from other streams. |
| 249 if (output_packet->stream_index != stream_index_) { |
| 250 av_free_packet(output_packet); |
| 251 continue; |
| 252 } |
| 253 return true; |
| 254 } |
| 255 return false; |
| 256 } |
| 257 |
| 248 } // namespace media | 258 } // namespace media |
| OLD | NEW |