| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/renderer/media/audio_decoder.h" | 5 #include "content/renderer/media/audio_decoder.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 namespace content { | 25 namespace content { |
| 26 | 26 |
| 27 // Decode in-memory audio file data. | 27 // Decode in-memory audio file data. |
| 28 bool DecodeAudioFileData( | 28 bool DecodeAudioFileData( |
| 29 blink::WebAudioBus* destination_bus, | 29 blink::WebAudioBus* destination_bus, |
| 30 const char* data, size_t data_size) { | 30 const char* data, size_t data_size) { |
| 31 DCHECK(destination_bus); | 31 DCHECK(destination_bus); |
| 32 if (!destination_bus) | 32 if (!destination_bus) |
| 33 return false; | 33 return false; |
| 34 | 34 |
| 35 #if !defined(MEDIA_DISABLE_FFMPEG) | 35 #if defined(MEDIA_DISABLE_FFMPEG) |
| 36 return false |
| 37 #else |
| 36 // Uses the FFmpeg library for audio file reading. | 38 // Uses the FFmpeg library for audio file reading. |
| 37 InMemoryUrlProtocol url_protocol(reinterpret_cast<const uint8_t*>(data), | 39 InMemoryUrlProtocol url_protocol(reinterpret_cast<const uint8_t*>(data), |
| 38 data_size, false); | 40 data_size, false); |
| 39 AudioFileReader reader(&url_protocol); | 41 AudioFileReader reader(&url_protocol); |
| 40 | 42 |
| 41 if (!reader.Open()) | 43 if (!reader.Open()) |
| 42 return false; | 44 return false; |
| 43 | 45 |
| 44 size_t number_of_channels = reader.channels(); | 46 size_t number_of_channels = reader.channels(); |
| 45 double file_sample_rate = reader.sample_rate(); | 47 double file_sample_rate = reader.sample_rate(); |
| 46 size_t number_of_frames = static_cast<size_t>(reader.GetNumberOfFrames()); | |
| 47 | 48 |
| 48 // Apply sanity checks to make sure crazy values aren't coming out of | 49 // Apply sanity checks to make sure crazy values aren't coming out of |
| 49 // FFmpeg. | 50 // FFmpeg. |
| 50 if (!number_of_channels || | 51 if (!number_of_channels || |
| 51 number_of_channels > static_cast<size_t>(media::limits::kMaxChannels) || | 52 number_of_channels > static_cast<size_t>(media::limits::kMaxChannels) || |
| 52 file_sample_rate < media::limits::kMinSampleRate || | 53 file_sample_rate < media::limits::kMinSampleRate || |
| 53 file_sample_rate > media::limits::kMaxSampleRate) | 54 file_sample_rate > media::limits::kMaxSampleRate) |
| 54 return false; | 55 return false; |
| 55 | 56 |
| 56 // Allocate and configure the output audio channel data. | 57 std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets; |
| 57 destination_bus->initialize(number_of_channels, | 58 int number_of_frames = reader.Read(&decoded_audio_packets); |
| 58 number_of_frames, | 59 |
| 60 if (number_of_frames <= 0) |
| 61 return false; |
| 62 |
| 63 // Allocate and configure the output audio channel data and then |
| 64 // copy the decoded data to the destination. |
| 65 destination_bus->initialize(number_of_channels, number_of_frames, |
| 59 file_sample_rate); | 66 file_sample_rate); |
| 60 | 67 |
| 61 // Wrap the channel pointers which will receive the decoded PCM audio. | 68 int dest_frame_offset = 0; |
| 62 vector<float*> audio_data; | 69 for (size_t k = 0; k < decoded_audio_packets.size(); ++k) { |
| 63 audio_data.reserve(number_of_channels); | 70 AudioBus* packet = decoded_audio_packets[k].get(); |
| 64 for (size_t i = 0; i < number_of_channels; ++i) { | 71 int packet_length = packet->frames(); |
| 65 audio_data.push_back(destination_bus->channelData(i)); | 72 for (size_t ch = 0; ch < number_of_channels; ++ch) { |
| 73 float* dst = destination_bus->channelData(ch); |
| 74 float* src = packet->channel(ch); |
| 75 DCHECK_LE(dest_frame_offset + packet_length, number_of_frames); |
| 76 memcpy(dst + dest_frame_offset, src, packet_length * sizeof(*dst)); |
| 77 } |
| 78 dest_frame_offset += packet_length; |
| 66 } | 79 } |
| 67 | 80 |
| 68 std::unique_ptr<AudioBus> audio_bus = | 81 DVLOG(1) << "Decoded file data (unknown duration)-" |
| 69 AudioBus::WrapVector(number_of_frames, audio_data); | 82 << " data: " << data << " data size: " << data_size |
| 83 << ", decoded duration: " << (number_of_frames / file_sample_rate) |
| 84 << ", number of frames: " << number_of_frames |
| 85 << ", estimated frames (if available): " |
| 86 << (reader.HasKnownDuration() ? reader.GetNumberOfFrames() : 0) |
| 87 << ", sample rate: " << file_sample_rate |
| 88 << ", number of channels: " << number_of_channels; |
| 70 | 89 |
| 71 // Decode the audio file data. | 90 return number_of_frames > 0; |
| 72 // TODO(crogers): If our estimate was low, then we still may fail to read | 91 #endif // defined(MEDIA_DISABLE_FFMPEG) |
| 73 // all of the data from the file. | |
| 74 size_t actual_frames = reader.Read(audio_bus.get()); | |
| 75 | |
| 76 // Adjust WebKit's bus to account for the actual file length | |
| 77 // and valid data read. | |
| 78 if (actual_frames != number_of_frames) { | |
| 79 DCHECK_LE(actual_frames, number_of_frames); | |
| 80 destination_bus->resizeSmaller(actual_frames); | |
| 81 } | |
| 82 | |
| 83 double duration = actual_frames / file_sample_rate; | |
| 84 | |
| 85 DVLOG(1) << "Decoded file data -" | |
| 86 << " data: " << data | |
| 87 << " data size: " << data_size | |
| 88 << " duration: " << duration | |
| 89 << " number of frames: " << actual_frames | |
| 90 << " sample rate: " << file_sample_rate | |
| 91 << " number of channels: " << number_of_channels; | |
| 92 | |
| 93 return actual_frames > 0; | |
| 94 #else | |
| 95 return false; | |
| 96 #endif // !defined(MEDIA_DISABLE_FFMPEG) | |
| 97 } | 92 } |
| 98 | 93 |
| 99 } // namespace content | 94 } // namespace content |
| OLD | NEW |