| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/ffmpeg_audio_decoder.h" | 5 #include "media/filters/ffmpeg_audio_decoder.h" |
| 6 | 6 |
| 7 #include "media/base/callback.h" | 7 #include "media/base/callback.h" |
| 8 #include "media/base/data_buffer.h" | 8 #include "media/base/data_buffer.h" |
| 9 #include "media/base/limits.h" | 9 #include "media/base/limits.h" |
| 10 #include "media/ffmpeg/ffmpeg_common.h" | 10 #include "media/ffmpeg/ffmpeg_common.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 // Get the AVStream by querying for the provider interface. | 44 // Get the AVStream by querying for the provider interface. |
| 45 AVStreamProvider* av_stream_provider; | 45 AVStreamProvider* av_stream_provider; |
| 46 if (!demuxer_stream->QueryInterface(&av_stream_provider)) { | 46 if (!demuxer_stream->QueryInterface(&av_stream_provider)) { |
| 47 return; | 47 return; |
| 48 } | 48 } |
| 49 AVStream* av_stream = av_stream_provider->GetAVStream(); | 49 AVStream* av_stream = av_stream_provider->GetAVStream(); |
| 50 | 50 |
| 51 // Grab the AVStream's codec context and make sure we have sensible values. | 51 // Grab the AVStream's codec context and make sure we have sensible values. |
| 52 codec_context_ = av_stream->codec; | 52 codec_context_ = av_stream->codec; |
| 53 int bps = av_get_bits_per_sample_format(codec_context_->sample_fmt); | 53 int bps = av_get_bits_per_sample_fmt(codec_context_->sample_fmt); |
| 54 if (codec_context_->channels <= 0 || | 54 if (codec_context_->channels <= 0 || |
| 55 codec_context_->channels > Limits::kMaxChannels || | 55 codec_context_->channels > Limits::kMaxChannels || |
| 56 bps <= 0 || bps > Limits::kMaxBitsPerSample || | 56 bps <= 0 || bps > Limits::kMaxBitsPerSample || |
| 57 codec_context_->sample_rate <= 0 || | 57 codec_context_->sample_rate <= 0 || |
| 58 codec_context_->sample_rate > Limits::kMaxSampleRate) { | 58 codec_context_->sample_rate > Limits::kMaxSampleRate) { |
| 59 DLOG(WARNING) << "Invalid audio stream -" | 59 DLOG(WARNING) << "Invalid audio stream -" |
| 60 << " channels: " << codec_context_->channels | 60 << " channels: " << codec_context_->channels |
| 61 << " bps: " << bps | 61 << " bps: " << bps |
| 62 << " sample rate: " << codec_context_->sample_rate; | 62 << " sample rate: " << codec_context_->sample_rate; |
| 63 return; | 63 return; |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Serialize calls to avcodec_open(). | 66 // Serialize calls to avcodec_open(). |
| 67 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 67 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
| 68 if (!codec || avcodec_open(codec_context_, codec) < 0) { | 68 if (!codec || avcodec_open(codec_context_, codec) < 0) { |
| 69 return; | 69 return; |
| 70 } | 70 } |
| 71 | 71 |
| 72 // When calling avcodec_find_decoder(), |codec_context_| might be altered by | 72 // When calling avcodec_find_decoder(), |codec_context_| might be altered by |
| 73 // the decoder to give more accurate values of the output format of the | 73 // the decoder to give more accurate values of the output format of the |
| 74 // decoder. So set the media format after a decoder is allocated. | 74 // decoder. So set the media format after a decoder is allocated. |
| 75 // TODO(hclam): Reuse the information provided by the demuxer for now, we may | 75 // TODO(hclam): Reuse the information provided by the demuxer for now, we may |
| 76 // need to wait until the first buffer is decoded to know the correct | 76 // need to wait until the first buffer is decoded to know the correct |
| 77 // information. | 77 // information. |
| 78 media_format_.SetAsInteger(MediaFormat::kChannels, codec_context_->channels); | 78 media_format_.SetAsInteger(MediaFormat::kChannels, codec_context_->channels); |
| 79 media_format_.SetAsInteger(MediaFormat::kSampleBits, | 79 media_format_.SetAsInteger(MediaFormat::kSampleBits, |
| 80 av_get_bits_per_sample_format(codec_context_->sample_fmt)); | 80 av_get_bits_per_sample_fmt(codec_context_->sample_fmt)); |
| 81 media_format_.SetAsInteger(MediaFormat::kSampleRate, | 81 media_format_.SetAsInteger(MediaFormat::kSampleRate, |
| 82 codec_context_->sample_rate); | 82 codec_context_->sample_rate); |
| 83 media_format_.SetAsString(MediaFormat::kMimeType, | 83 media_format_.SetAsString(MediaFormat::kMimeType, |
| 84 mime_type::kUncompressedAudio); | 84 mime_type::kUncompressedAudio); |
| 85 | 85 |
| 86 // Prepare the output buffer. | 86 // Prepare the output buffer. |
| 87 output_buffer_.reset(static_cast<uint8*>(av_malloc(kOutputBufferSize))); | 87 output_buffer_.reset(static_cast<uint8*>(av_malloc(kOutputBufferSize))); |
| 88 if (!output_buffer_.get()) { | 88 if (!output_buffer_.get()) { |
| 89 host()->SetError(PIPELINE_ERROR_OUT_OF_MEMORY); | 89 host()->SetError(PIPELINE_ERROR_OUT_OF_MEMORY); |
| 90 return; | 90 return; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 DataBuffer* result_buffer = new DataBuffer(0); | 238 DataBuffer* result_buffer = new DataBuffer(0); |
| 239 result_buffer->SetTimestamp(input->GetTimestamp()); | 239 result_buffer->SetTimestamp(input->GetTimestamp()); |
| 240 result_buffer->SetDuration(input->GetDuration()); | 240 result_buffer->SetDuration(input->GetDuration()); |
| 241 EnqueueResult(result_buffer); | 241 EnqueueResult(result_buffer); |
| 242 } | 242 } |
| 243 DecoderBase<AudioDecoder, Buffer>::OnDecodeComplete(); | 243 DecoderBase<AudioDecoder, Buffer>::OnDecodeComplete(); |
| 244 } | 244 } |
| 245 | 245 |
| 246 base::TimeDelta FFmpegAudioDecoder::CalculateDuration(size_t size) { | 246 base::TimeDelta FFmpegAudioDecoder::CalculateDuration(size_t size) { |
| 247 int64 denominator = codec_context_->channels * | 247 int64 denominator = codec_context_->channels * |
| 248 av_get_bits_per_sample_format(codec_context_->sample_fmt) / 8 * | 248 av_get_bits_per_sample_fmt(codec_context_->sample_fmt) / 8 * |
| 249 codec_context_->sample_rate; | 249 codec_context_->sample_rate; |
| 250 double microseconds = size / | 250 double microseconds = size / |
| 251 (denominator / static_cast<double>(base::Time::kMicrosecondsPerSecond)); | 251 (denominator / static_cast<double>(base::Time::kMicrosecondsPerSecond)); |
| 252 return base::TimeDelta::FromMicroseconds(static_cast<int64>(microseconds)); | 252 return base::TimeDelta::FromMicroseconds(static_cast<int64>(microseconds)); |
| 253 } | 253 } |
| 254 | 254 |
| 255 } // namespace | 255 } // namespace |
| OLD | NEW |