OLD | NEW |
1 // Copyright (c) 2011 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 <string> | 7 #include <string> |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "media/audio/audio_util.h" | 11 #include "media/audio/audio_util.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 } | 44 } |
45 | 45 |
46 bool AudioFileReader::Open() { | 46 bool AudioFileReader::Open() { |
47 // Add our data reader to the protocol list and get our unique key. | 47 // Add our data reader to the protocol list and get our unique key. |
48 std::string key = FFmpegGlue::GetInstance()->AddProtocol(protocol_); | 48 std::string key = FFmpegGlue::GetInstance()->AddProtocol(protocol_); |
49 | 49 |
50 // Open FFmpeg AVFormatContext. | 50 // Open FFmpeg AVFormatContext. |
51 DCHECK(!format_context_); | 51 DCHECK(!format_context_); |
52 AVFormatContext* context = NULL; | 52 AVFormatContext* context = NULL; |
53 | 53 |
54 int result = av_open_input_file(&context, key.c_str(), NULL, 0, NULL); | 54 int result = avformat_open_input(&context, key.c_str(), NULL, NULL); |
55 | 55 |
56 // Remove our data reader from protocol list since av_open_input_file() setup | 56 // Remove our data reader from protocol list since avformat_open_input() setup |
57 // the AVFormatContext with the data reader. | 57 // the AVFormatContext with the data reader. |
58 FFmpegGlue::GetInstance()->RemoveProtocol(protocol_); | 58 FFmpegGlue::GetInstance()->RemoveProtocol(protocol_); |
59 | 59 |
60 if (result) { | 60 if (result) { |
61 DLOG(WARNING) | 61 DLOG(WARNING) |
62 << "AudioFileReader::Open() : error in av_open_input_file() -" | 62 << "AudioFileReader::Open() : error in avformat_open_input() -" |
63 << " result: " << result; | 63 << " result: " << result; |
64 return false; | 64 return false; |
65 } | 65 } |
66 | 66 |
67 DCHECK(context); | 67 DCHECK(context); |
68 format_context_ = context; | 68 format_context_ = context; |
69 | 69 |
70 // Get the codec context. | 70 // Get the codec context. |
71 codec_context_ = NULL; | 71 codec_context_ = NULL; |
72 for (size_t i = 0; i < format_context_->nb_streams; ++i) { | 72 for (size_t i = 0; i < format_context_->nb_streams; ++i) { |
73 AVCodecContext* c = format_context_->streams[i]->codec; | 73 AVCodecContext* c = format_context_->streams[i]->codec; |
74 if (c->codec_type == AVMEDIA_TYPE_AUDIO) { | 74 if (c->codec_type == AVMEDIA_TYPE_AUDIO) { |
75 codec_context_ = c; | 75 codec_context_ = c; |
76 break; | 76 break; |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 // Get the codec. | 80 // Get the codec. |
81 if (!codec_context_) | 81 if (!codec_context_) |
82 return false; | 82 return false; |
83 | 83 |
84 av_find_stream_info(format_context_); | 84 avformat_find_stream_info(format_context_, NULL); |
85 codec_ = avcodec_find_decoder(codec_context_->codec_id); | 85 codec_ = avcodec_find_decoder(codec_context_->codec_id); |
86 if (codec_) { | 86 if (codec_) { |
87 if ((result = avcodec_open(codec_context_, codec_)) < 0) { | 87 if ((result = avcodec_open2(codec_context_, codec_, NULL)) < 0) { |
88 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" | 88 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" |
89 << " result: " << result; | 89 << " result: " << result; |
90 return false; | 90 return false; |
91 } | 91 } |
92 | 92 |
93 if ((result = av_seek_frame(format_context_, 0, 0, 0)) < 0) { | 93 if ((result = av_seek_frame(format_context_, 0, 0, 0)) < 0) { |
94 DLOG(WARNING) << "AudioFileReader::Open() : could not seek frame -" | 94 DLOG(WARNING) << "AudioFileReader::Open() : could not seek frame -" |
95 << " result: " << result; | 95 << " result: " << result; |
96 return false; | 96 return false; |
97 } | 97 } |
98 } else { | 98 } else { |
99 DLOG(WARNING) << "AudioFileReader::Open() : could not find codec -" | 99 DLOG(WARNING) << "AudioFileReader::Open() : could not find codec -" |
100 << " result: " << result; | 100 << " result: " << result; |
101 return false; | 101 return false; |
102 } | 102 } |
103 | 103 |
104 return true; | 104 return true; |
105 } | 105 } |
106 | 106 |
107 void AudioFileReader::Close() { | 107 void AudioFileReader::Close() { |
108 if (codec_context_ && codec_) | 108 if (codec_context_ && codec_) |
109 avcodec_close(codec_context_); | 109 avcodec_close(codec_context_); |
110 | 110 |
111 codec_context_ = NULL; | 111 codec_context_ = NULL; |
112 codec_ = NULL; | 112 codec_ = NULL; |
113 | 113 |
114 if (format_context_) { | 114 if (format_context_) { |
115 av_close_input_file(format_context_); | 115 avformat_close_input(&format_context_); |
116 format_context_ = NULL; | 116 format_context_ = NULL; |
117 } | 117 } |
118 } | 118 } |
119 | 119 |
120 bool AudioFileReader::Read(const std::vector<float*>& audio_data, | 120 bool AudioFileReader::Read(const std::vector<float*>& audio_data, |
121 size_t number_of_frames) { | 121 size_t number_of_frames) { |
122 size_t channels = this->channels(); | 122 size_t channels = this->channels(); |
123 DCHECK_EQ(audio_data.size(), channels); | 123 DCHECK_EQ(audio_data.size(), channels); |
124 if (audio_data.size() != channels) | 124 if (audio_data.size() != channels) |
125 return false; | 125 return false; |
(...skipping 24 matching lines...) Expand all Loading... |
150 | 150 |
151 if (result < 0) { | 151 if (result < 0) { |
152 DLOG(WARNING) | 152 DLOG(WARNING) |
153 << "AudioFileReader::Read() : error in avcodec_decode_audio3() -" | 153 << "AudioFileReader::Read() : error in avcodec_decode_audio3() -" |
154 << result; | 154 << result; |
155 return false; | 155 return false; |
156 } | 156 } |
157 | 157 |
158 // Determine the number of sample-frames we just decoded. | 158 // Determine the number of sample-frames we just decoded. |
159 size_t bytes_per_sample = | 159 size_t bytes_per_sample = |
160 av_get_bits_per_sample_fmt(codec_context_->sample_fmt) >> 3; | 160 av_get_bytes_per_sample(codec_context_->sample_fmt); |
161 size_t frames_read = out_size / (channels * bytes_per_sample); | 161 size_t frames_read = out_size / (channels * bytes_per_sample); |
162 | 162 |
163 // Truncate, if necessary, if the destination isn't big enough. | 163 // Truncate, if necessary, if the destination isn't big enough. |
164 if (current_frame + frames_read > number_of_frames) | 164 if (current_frame + frames_read > number_of_frames) |
165 frames_read = number_of_frames - current_frame; | 165 frames_read = number_of_frames - current_frame; |
166 | 166 |
167 // Deinterleave each channel and convert to 32bit floating-point | 167 // Deinterleave each channel and convert to 32bit floating-point |
168 // with nominal range -1.0 -> +1.0. | 168 // with nominal range -1.0 -> +1.0. |
169 for (size_t channel_index = 0; channel_index < channels; | 169 for (size_t channel_index = 0; channel_index < channels; |
170 ++channel_index) { | 170 ++channel_index) { |
(...skipping 11 matching lines...) Expand all Loading... |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 current_frame += frames_read; | 185 current_frame += frames_read; |
186 } | 186 } |
187 | 187 |
188 return true; | 188 return true; |
189 } | 189 } |
190 | 190 |
191 } // namespace media | 191 } // namespace media |
OLD | NEW |