audio_file_reader.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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
  3. // found in the LICENSE file.
  4. #include "media/filters/audio_file_reader.h"
  5. #include <stddef.h>
  6. #include <cmath>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/bind.h"
  10. #include "base/callback.h"
  11. #include "base/logging.h"
  12. #include "base/numerics/safe_math.h"
  13. #include "base/time/time.h"
  14. #include "media/base/audio_bus.h"
  15. #include "media/base/audio_sample_types.h"
  16. #include "media/ffmpeg/ffmpeg_common.h"
  17. #include "media/ffmpeg/ffmpeg_decoding_loop.h"
  18. namespace media {
  19. // AAC(M4A) decoding specific constants.
  20. static const int kAACPrimingFrameCount = 2112;
  21. static const int kAACRemainderFrameCount = 519;
  22. AudioFileReader::AudioFileReader(FFmpegURLProtocol* protocol)
  23. : stream_index_(0),
  24. protocol_(protocol),
  25. audio_codec_(AudioCodec::kUnknown),
  26. channels_(0),
  27. sample_rate_(0),
  28. av_sample_format_(0) {}
  29. AudioFileReader::~AudioFileReader() {
  30. Close();
  31. }
  32. bool AudioFileReader::Open() {
  33. return OpenDemuxer() && OpenDecoder();
  34. }
  35. bool AudioFileReader::OpenDemuxer() {
  36. glue_ = std::make_unique<FFmpegGlue>(protocol_);
  37. AVFormatContext* format_context = glue_->format_context();
  38. // Open FFmpeg AVFormatContext.
  39. if (!glue_->OpenContext()) {
  40. DLOG(WARNING) << "AudioFileReader::Open() : error in avformat_open_input()";
  41. return false;
  42. }
  43. const int result = avformat_find_stream_info(format_context, NULL);
  44. if (result < 0) {
  45. DLOG(WARNING)
  46. << "AudioFileReader::Open() : error in avformat_find_stream_info()";
  47. return false;
  48. }
  49. // Calling avformat_find_stream_info can uncover new streams. We wait till now
  50. // to find the first audio stream, if any.
  51. codec_context_.reset();
  52. bool found_stream = false;
  53. for (size_t i = 0; i < format_context->nb_streams; ++i) {
  54. if (format_context->streams[i]->codecpar->codec_type ==
  55. AVMEDIA_TYPE_AUDIO) {
  56. stream_index_ = i;
  57. found_stream = true;
  58. break;
  59. }
  60. }
  61. if (!found_stream)
  62. return false;
  63. // Get the codec context.
  64. codec_context_ =
  65. AVStreamToAVCodecContext(format_context->streams[stream_index_]);
  66. if (!codec_context_)
  67. return false;
  68. DCHECK_EQ(codec_context_->codec_type, AVMEDIA_TYPE_AUDIO);
  69. return true;
  70. }
  71. bool AudioFileReader::OpenDecoder() {
  72. const AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
  73. if (codec) {
  74. // MP3 decodes to S16P which we don't support, tell it to use S16 instead.
  75. if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P)
  76. codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16;
  77. const int result = avcodec_open2(codec_context_.get(), codec, nullptr);
  78. if (result < 0) {
  79. DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -"
  80. << " result: " << result;
  81. return false;
  82. }
  83. // Ensure avcodec_open2() respected our format request.
  84. if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) {
  85. DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a"
  86. << " supported sample format - "
  87. << codec_context_->sample_fmt;
  88. return false;
  89. }
  90. } else {
  91. DLOG(WARNING) << "AudioFileReader::Open() : could not find codec.";
  92. return false;
  93. }
  94. // Verify the channel layout is supported by Chrome. Acts as a sanity check
  95. // against invalid files. See http://crbug.com/171962
  96. if (ChannelLayoutToChromeChannelLayout(
  97. codec_context_->ch_layout.u.mask,
  98. codec_context_->ch_layout.nb_channels) ==
  99. CHANNEL_LAYOUT_UNSUPPORTED) {
  100. return false;
  101. }
  102. // Store initial values to guard against midstream configuration changes.
  103. channels_ = codec_context_->ch_layout.nb_channels;
  104. audio_codec_ = CodecIDToAudioCodec(codec_context_->codec_id);
  105. sample_rate_ = codec_context_->sample_rate;
  106. av_sample_format_ = codec_context_->sample_fmt;
  107. return true;
  108. }
  109. bool AudioFileReader::HasKnownDuration() const {
  110. return glue_->format_context()->duration != AV_NOPTS_VALUE;
  111. }
  112. void AudioFileReader::Close() {
  113. codec_context_.reset();
  114. glue_.reset();
  115. }
  116. int AudioFileReader::Read(
  117. std::vector<std::unique_ptr<AudioBus>>* decoded_audio_packets,
  118. int packets_to_read) {
  119. DCHECK(glue_ && codec_context_)
  120. << "AudioFileReader::Read() : reader is not opened!";
  121. FFmpegDecodingLoop decode_loop(codec_context_.get());
  122. int total_frames = 0;
  123. auto frame_ready_cb =
  124. base::BindRepeating(&AudioFileReader::OnNewFrame, base::Unretained(this),
  125. &total_frames, decoded_audio_packets);
  126. AVPacket packet;
  127. int packets_read = 0;
  128. while (packets_read++ < packets_to_read && ReadPacket(&packet)) {
  129. const auto status = decode_loop.DecodePacket(&packet, frame_ready_cb);
  130. av_packet_unref(&packet);
  131. if (status != FFmpegDecodingLoop::DecodeStatus::kOkay)
  132. break;
  133. }
  134. return total_frames;
  135. }
  136. base::TimeDelta AudioFileReader::GetDuration() const {
  137. const AVRational av_time_base = {1, AV_TIME_BASE};
  138. DCHECK_NE(glue_->format_context()->duration, AV_NOPTS_VALUE);
  139. base::CheckedNumeric<int64_t> estimated_duration_us =
  140. glue_->format_context()->duration;
  141. if (audio_codec_ == AudioCodec::kAAC) {
  142. // For certain AAC-encoded files, FFMPEG's estimated frame count might not
  143. // be sufficient to capture the entire audio content that we want. This is
  144. // especially noticeable for short files (< 10ms) resulting in silence
  145. // throughout the decoded buffer. Thus we add the priming frames and the
  146. // remainder frames to the estimation.
  147. // (See: crbug.com/513178)
  148. estimated_duration_us += ceil(
  149. 1000000.0 *
  150. static_cast<double>(kAACPrimingFrameCount + kAACRemainderFrameCount) /
  151. sample_rate());
  152. } else {
  153. // Add one microsecond to avoid rounding-down errors which can occur when
  154. // |duration| has been calculated from an exact number of sample-frames.
  155. // One microsecond is much less than the time of a single sample-frame
  156. // at any real-world sample-rate.
  157. estimated_duration_us += 1;
  158. }
  159. return ConvertFromTimeBase(av_time_base, estimated_duration_us.ValueOrDie());
  160. }
  161. int AudioFileReader::GetNumberOfFrames() const {
  162. return base::ClampCeil(GetDuration().InSecondsF() * sample_rate());
  163. }
  164. bool AudioFileReader::OpenDemuxerForTesting() {
  165. return OpenDemuxer();
  166. }
  167. bool AudioFileReader::ReadPacketForTesting(AVPacket* output_packet) {
  168. return ReadPacket(output_packet);
  169. }
  170. bool AudioFileReader::ReadPacket(AVPacket* output_packet) {
  171. while (av_read_frame(glue_->format_context(), output_packet) >= 0) {
  172. // Skip packets from other streams.
  173. if (output_packet->stream_index != stream_index_) {
  174. av_packet_unref(output_packet);
  175. continue;
  176. }
  177. return true;
  178. }
  179. return false;
  180. }
  181. bool AudioFileReader::OnNewFrame(
  182. int* total_frames,
  183. std::vector<std::unique_ptr<AudioBus>>* decoded_audio_packets,
  184. AVFrame* frame) {
  185. int frames_read = frame->nb_samples;
  186. if (frames_read < 0)
  187. return false;
  188. const int channels = frame->ch_layout.nb_channels;
  189. if (frame->sample_rate != sample_rate_ || channels != channels_ ||
  190. frame->format != av_sample_format_) {
  191. DLOG(ERROR) << "Unsupported midstream configuration change!"
  192. << " Sample Rate: " << frame->sample_rate << " vs "
  193. << sample_rate_ << ", Channels: " << channels << " vs "
  194. << channels_ << ", Sample Format: " << frame->format << " vs "
  195. << av_sample_format_;
  196. // This is an unrecoverable error, so bail out. We'll return
  197. // whatever we've decoded up to this point.
  198. return false;
  199. }
  200. // AAC decoding doesn't properly trim the last packet in a stream, so if we
  201. // have duration information, use it to set the correct length to avoid extra
  202. // silence from being output. In the case where we are also discarding some
  203. // portion of the packet (as indicated by a negative pts), we further want to
  204. // adjust the duration downward by however much exists before zero.
  205. if (audio_codec_ == AudioCodec::kAAC && frame->duration) {
  206. const base::TimeDelta pkt_duration = ConvertFromTimeBase(
  207. glue_->format_context()->streams[stream_index_]->time_base,
  208. frame->duration + std::min(static_cast<int64_t>(0), frame->pts));
  209. const base::TimeDelta frame_duration =
  210. base::Seconds(frames_read / static_cast<double>(sample_rate_));
  211. if (pkt_duration < frame_duration && pkt_duration.is_positive()) {
  212. const int new_frames_read =
  213. base::ClampFloor(frames_read * (pkt_duration / frame_duration));
  214. DVLOG(2) << "Shrinking AAC frame from " << frames_read << " to "
  215. << new_frames_read << " based on packet duration.";
  216. frames_read = new_frames_read;
  217. // The above process may delete the entire packet.
  218. if (!frames_read)
  219. return true;
  220. }
  221. }
  222. // Deinterleave each channel and convert to 32bit floating-point with
  223. // nominal range -1.0 -> +1.0. If the output is already in float planar
  224. // format, just copy it into the AudioBus.
  225. decoded_audio_packets->emplace_back(AudioBus::Create(channels, frames_read));
  226. AudioBus* audio_bus = decoded_audio_packets->back().get();
  227. if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) {
  228. audio_bus->FromInterleaved<Float32SampleTypeTraits>(
  229. reinterpret_cast<float*>(frame->data[0]), frames_read);
  230. } else if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP) {
  231. for (int ch = 0; ch < audio_bus->channels(); ++ch) {
  232. memcpy(audio_bus->channel(ch), frame->extended_data[ch],
  233. sizeof(float) * frames_read);
  234. }
  235. } else {
  236. int bytes_per_sample = av_get_bytes_per_sample(codec_context_->sample_fmt);
  237. switch (bytes_per_sample) {
  238. case 1:
  239. audio_bus->FromInterleaved<UnsignedInt8SampleTypeTraits>(
  240. reinterpret_cast<const uint8_t*>(frame->data[0]), frames_read);
  241. break;
  242. case 2:
  243. audio_bus->FromInterleaved<SignedInt16SampleTypeTraits>(
  244. reinterpret_cast<const int16_t*>(frame->data[0]), frames_read);
  245. break;
  246. case 4:
  247. audio_bus->FromInterleaved<SignedInt32SampleTypeTraits>(
  248. reinterpret_cast<const int32_t*>(frame->data[0]), frames_read);
  249. break;
  250. default:
  251. NOTREACHED() << "Unsupported bytes per sample encountered: "
  252. << bytes_per_sample;
  253. audio_bus->ZeroFrames(frames_read);
  254. }
  255. }
  256. (*total_frames) += frames_read;
  257. return true;
  258. }
  259. bool AudioFileReader::SeekForTesting(base::TimeDelta seek_time) {
  260. // Use the AVStream's time_base, since |codec_context_| does not have
  261. // time_base populated until after OpenDecoder().
  262. return av_seek_frame(
  263. glue_->format_context(), stream_index_,
  264. ConvertToTimeBase(GetAVStreamForTesting()->time_base, seek_time),
  265. AVSEEK_FLAG_BACKWARD) >= 0;
  266. }
  267. const AVStream* AudioFileReader::GetAVStreamForTesting() const {
  268. return glue_->format_context()->streams[stream_index_];
  269. }
  270. } // namespace media