Chromium Code Reviews| Index: webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.cc |
| diff --git a/webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.cc b/webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d33861145dbede40157e9761508e355d7fddff89 |
| --- /dev/null |
| +++ b/webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.cc |
| @@ -0,0 +1,354 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.h" |
| + |
|
xhwang
2012/10/24 08:18:15
add #include <algorithm> for std::min
Tom Finegan
2012/10/24 21:16:44
Done.
|
| +#include "base/logging.h" |
| +#include "media/base/buffers.h" |
| +#include "media/base/limits.h" |
| + |
| +// Include FFmpeg header files. |
| +extern "C" { |
| +// Temporarily disable possible loss of data warning. |
| +MSVC_PUSH_DISABLE_WARNING(4244); |
| +#include <libavcodec/avcodec.h> |
| +MSVC_POP_WARNING(); |
| +} // extern "C" |
| + |
| +namespace webkit_media { |
| + |
| +// Maximum number of channels with defined order in the Vorbis specification. |
| +// http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html |
| +static const int kMaxVorbisChannels = 8; |
| + |
| +static CodecID CdmAudioCodecToCodecID( |
| + cdm::AudioDecoderConfig::AudioCodec audio_codec) { |
| + switch (audio_codec) { |
| + case cdm::AudioDecoderConfig::kCodecVorbis: |
| + return CODEC_ID_VORBIS; |
| + default: |
| + NOTREACHED() << "Unsupported cdm::AudioCodec: " << audio_codec; |
| + } |
| + |
| + return CODEC_ID_NONE; |
| +} |
| + |
| +static void CdmAudioDecoderConfigToAVCodecContext( |
| + const cdm::AudioDecoderConfig& config, |
| + AVCodecContext* codec_context) { |
| + codec_context->codec_type = AVMEDIA_TYPE_AUDIO; |
| + codec_context->codec_id = CdmAudioCodecToCodecID(config.codec); |
| + |
| + switch (config.bits_per_channel) { |
| + case 8: |
| + codec_context->sample_fmt = AV_SAMPLE_FMT_U8; |
| + break; |
| + case 16: |
| + codec_context->sample_fmt = AV_SAMPLE_FMT_S16; |
| + break; |
| + case 32: |
| + codec_context->sample_fmt = AV_SAMPLE_FMT_S32; |
| + break; |
| + default: |
| + DVLOG(1) << "CdmAudioDecoderConfigToAVCodecContext() Unsupported bits " |
| + "per channel: " << config.bits_per_channel; |
| + codec_context->sample_fmt = AV_SAMPLE_FMT_NONE; |
| + } |
| + |
| + codec_context->channels = config.channel_count; |
| + codec_context->sample_rate = config.samples_per_second; |
| + |
| + if (config.extra_data) { |
| + codec_context->extradata_size = config.extra_data_size; |
| + codec_context->extradata = reinterpret_cast<uint8_t*>( |
| + av_malloc(config.extra_data_size + FF_INPUT_BUFFER_PADDING_SIZE)); |
| + memcpy(codec_context->extradata, config.extra_data, |
| + config.extra_data_size); |
| + memset(codec_context->extradata + config.extra_data_size, '\0', |
| + FF_INPUT_BUFFER_PADDING_SIZE); |
| + } else { |
| + codec_context->extradata = NULL; |
| + codec_context->extradata_size = 0; |
| + } |
| +} |
| + |
| +// Returns true when the decode result was end of stream. |
| +static inline bool IsEndOfOutputStream(int result, |
| + int decoded_size, |
| + bool is_end_of_input_stream) { |
| + // Three conditions to meet to declare end of stream for this decoder: |
| + // 1. FFmpeg didn't read anything. |
| + // 2. FFmpeg didn't output anything. |
| + // 3. An end of stream buffer is received. |
| + return result == 0 && decoded_size == 0 && is_end_of_input_stream; |
| +} |
| + |
| +FFmpegCdmAudioDecoder::FFmpegCdmAudioDecoder(cdm::Allocator* allocator) |
| + : allocator_(allocator), |
|
xhwang
2012/10/24 08:18:15
initialization order should match declaration orde
Tom Finegan
2012/10/24 21:16:44
Done.
|
| + codec_context_(NULL), |
| + av_frame_(NULL), |
| + is_initialized_(false), |
| + bits_per_channel_(0), |
| + samples_per_second_(0), |
| + bytes_per_frame_(0), |
| + output_timestamp_base_(media::kNoTimestamp()), |
| + total_frames_decoded_(0), |
|
xhwang
2012/10/24 08:18:15
If we really need to use double for total_frames_d
Tom Finegan
2012/10/24 21:16:44
It's int64_t now.
|
| + last_input_timestamp_(media::kNoTimestamp()), |
| + output_bytes_to_drop_(0) { |
| +} |
| + |
| +FFmpegCdmAudioDecoder::~FFmpegCdmAudioDecoder() { |
| + ReleaseFFmpegResources(); |
| +} |
| + |
| +bool FFmpegCdmAudioDecoder::Initialize(const cdm::AudioDecoderConfig& config) { |
| + DVLOG(1) << "Initialize()"; |
| + |
| + if (!IsValidConfig(config)) { |
| + LOG(ERROR) << "Initialize(): invalid audio decoder configuration."; |
| + return false; |
| + } |
| + |
| + if (is_initialized_) { |
| + LOG(ERROR) << "Initialize(): Already initialized."; |
| + return false; |
| + } |
| + |
| + // Release existing resources if necessary. |
| + ReleaseFFmpegResources(); |
|
xhwang
2012/10/24 08:18:15
In which case do we need this? Are we worrying tha
Tom Finegan
2012/10/24 21:16:44
Done, here and in the video decoder. Copied from m
|
| + |
| + // Initialize AVCodecContext structure. |
| + codec_context_ = avcodec_alloc_context3(NULL); |
| + CdmAudioDecoderConfigToAVCodecContext(config, codec_context_); |
| + DCHECK_EQ(CODEC_ID_VORBIS, codec_context_->codec_id); |
| + |
| + AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
| + if (!codec) { |
| + LOG(ERROR) << "Initialize(): avcodec_find_decoder failed."; |
| + return false; |
| + } |
| + |
| + int status; |
| + if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) { |
| + LOG(ERROR) << "Initialize(): avcodec_open2 failed: " << status; |
| + return false; |
| + } |
| + |
| + av_frame_ = avcodec_alloc_frame(); |
| + is_initialized_ = true; |
|
xhwang
2012/10/24 08:18:15
Move this down to line 144.
Tom Finegan
2012/10/24 21:16:44
Done.
|
| + bits_per_channel_ = config.bits_per_channel; |
| + samples_per_second_ = config.samples_per_second; |
| + bytes_per_frame_ = codec_context_->channels * bits_per_channel_ / 8; |
| + serialized_audio_frames_.reserve(bytes_per_frame_ * samples_per_second_); |
| + |
| + return true; |
| +} |
| + |
| +void FFmpegCdmAudioDecoder::Deinitialize() { |
| + DVLOG(1) << "Deinitialize()"; |
| + ReleaseFFmpegResources(); |
| + is_initialized_ = false; |
| + ResetOutputTime(); |
| +} |
| + |
| +void FFmpegCdmAudioDecoder::Reset() { |
| + DVLOG(1) << "Reset()"; |
| + avcodec_flush_buffers(codec_context_); |
| + ResetOutputTime(); |
| +} |
| + |
| +// static |
| +bool FFmpegCdmAudioDecoder::IsValidConfig( |
| + const cdm::AudioDecoderConfig& config) { |
| + return config.codec == cdm::AudioDecoderConfig::kCodecVorbis && |
| + config.channel_count > 0 && config.channel_count <= kMaxVorbisChannels && |
|
xhwang
2012/10/24 08:18:15
split this to two lines to be consistent with the
Tom Finegan
2012/10/24 21:16:44
Done.
|
| + config.bits_per_channel > 0 && |
| + config.bits_per_channel <= media::limits::kMaxBitsPerSample && |
| + config.samples_per_second > 0 && |
| + config.samples_per_second <= media::limits::kMaxSampleRate; |
| +} |
| + |
| +cdm::Status FFmpegCdmAudioDecoder::DecodeBuffer( |
| + const uint8_t* compressed_buffer, |
| + int32_t compressed_buffer_size, |
| + int64_t input_timestamp, |
| + cdm::AudioFrames* decoded_frames) { |
| + const bool is_end_of_stream = compressed_buffer_size == 0; |
| + base::TimeDelta timestamp = |
| + base::TimeDelta::FromMicroseconds(input_timestamp); |
| + if (!is_end_of_stream) { |
| + if (last_input_timestamp_ == media::kNoTimestamp()) { |
| + if (codec_context_->codec_id == CODEC_ID_VORBIS && |
| + timestamp < base::TimeDelta()) { |
| + // Dropping frames for negative timestamps as outlined in section A.2 |
| + // in the Vorbis spec. http://xiph.org/vorbis/doc/Vorbis_I_spec.html |
| + int frames_to_drop = floor( |
| + 0.5 + -timestamp.InSecondsF() * samples_per_second_); |
| + output_bytes_to_drop_ = bytes_per_frame_ * frames_to_drop; |
| + } else { |
| + last_input_timestamp_ = timestamp; |
| + } |
| + } else if (timestamp != media::kNoTimestamp()) { |
| + if (timestamp < last_input_timestamp_) { |
| + base::TimeDelta diff = timestamp - last_input_timestamp_; |
| + DVLOG(1) << "Input timestamps are not monotonically increasing! " |
| + << " ts " << timestamp.InMicroseconds() << " us" |
| + << " diff " << diff.InMicroseconds() << " us"; |
| + return cdm::kDecodeError; |
| + } |
| + |
| + last_input_timestamp_ = timestamp; |
| + } |
| + } |
| + |
| + AVPacket packet; |
| + av_init_packet(&packet); |
| + packet.data = const_cast<uint8_t*>(compressed_buffer); |
| + packet.size = compressed_buffer_size; |
| + |
| + // Each audio packet may contain several frames, so we must call the decoder |
| + // until we've exhausted the packet. Regardless of the packet size we always |
| + // want to hand it to the decoder at least once, otherwise we would end up |
| + // skipping end of stream packets since they have a size of zero. |
| + do { |
| + // Reset frame to default values. |
| + avcodec_get_frame_defaults(av_frame_); |
| + |
| + int frame_decoded = 0; |
| + int result = avcodec_decode_audio4( |
| + codec_context_, av_frame_, &frame_decoded, &packet); |
| + |
| + if (result < 0) { |
| + DCHECK(!is_end_of_stream) |
| + << "End of stream buffer produced an error! " |
| + << "This is quite possibly a bug in the audio decoder not handling " |
| + << "end of stream AVPackets correctly."; |
| + |
| + DLOG(ERROR) |
| + << "Error decoding an audio frame with timestamp: " |
| + << timestamp.InMicroseconds() << " us, duration: " |
| + << timestamp.InMicroseconds() << " us, packet size: " |
| + << compressed_buffer_size << " bytes"; |
| + |
| + // TODO(tomfinegan): Return cdm::kDecodeError here when |
| + // http://crbug.com/145276 is fixed. |
| + break; |
|
xhwang
2012/10/24 08:18:15
The break here will hit line 315. In that case we
Tom Finegan
2012/10/24 21:16:44
Done.
|
| + } |
| + |
| + // Update packet size and data pointer in case we need to call the decoder |
| + // with the remaining bytes from this packet. |
| + packet.size -= result; |
| + packet.data += result; |
| + |
| + if (output_timestamp_base_ == media::kNoTimestamp() && !is_end_of_stream) { |
| + DCHECK(timestamp != media::kNoTimestamp()); |
| + if (output_bytes_to_drop_ > 0) { |
| + // If we have to drop samples it always means the timeline starts at 0. |
| + output_timestamp_base_ = base::TimeDelta(); |
| + } else { |
| + output_timestamp_base_ = timestamp; |
| + } |
| + } |
| + |
| + const uint8* decoded_audio_data = NULL; |
|
xhwang
2012/10/24 08:18:15
we are usng uint8_t in other places in this file.
Tom Finegan
2012/10/24 21:16:44
_t added; missed this one after the copy from medi
|
| + int decoded_audio_size = 0; |
| + if (frame_decoded) { |
| + int output_sample_rate = av_frame_->sample_rate; |
| + if (output_sample_rate != samples_per_second_) { |
| + DLOG(ERROR) << "Output sample rate (" << output_sample_rate |
| + << ") doesn't match expected rate " << samples_per_second_; |
| + return cdm::kDecodeError; |
| + } |
| + |
| + decoded_audio_data = av_frame_->data[0]; |
| + decoded_audio_size = av_samples_get_buffer_size( |
| + NULL, codec_context_->channels, av_frame_->nb_samples, |
| + codec_context_->sample_fmt, 1); |
| + } |
| + |
| + if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { |
| + int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); |
| + decoded_audio_data += dropped_size; |
| + decoded_audio_size -= dropped_size; |
| + output_bytes_to_drop_ -= dropped_size; |
| + } |
| + |
| + if (decoded_audio_size > 0) { |
| + DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) |
| + << "Decoder didn't output full frames"; |
| + |
| + base::TimeDelta output_timestamp = GetNextOutputTimestamp(); |
|
xhwang
2012/10/24 08:18:15
hmm, ISTM that this timestamp is the time of the l
xhwang
2012/10/24 16:33:21
Okay, now I understand how it works ;) Could you p
Tom Finegan
2012/10/24 21:16:44
Done.
|
| + |
| + // Serialize the audio samples into |serialized_audio_frames_|. |
| + SerializeInt64(output_timestamp.InMicroseconds()); |
| + SerializeInt64(decoded_audio_size); |
| + serialized_audio_frames_.insert( |
| + serialized_audio_frames_.end(), |
| + decoded_audio_data, |
| + decoded_audio_data + decoded_audio_size); |
| + |
| + total_frames_decoded_ += decoded_audio_size / bytes_per_frame_; |
| + } else if (IsEndOfOutputStream(result, |
| + decoded_audio_size, |
| + is_end_of_stream)) { |
|
xhwang
2012/10/24 08:18:15
I am overwhelmed by the logic in this function (I
Tom Finegan
2012/10/24 21:16:44
Done (returning kNeedMoreData). Changed nothing el
|
| + DCHECK_EQ(packet.size, 0); |
| + // Serialize an end of stream buffer. |
| + SerializeInt64(0); |
| + SerializeInt64(0); |
|
xhwang
2012/10/24 08:18:15
The new contract is: suppose when the input is EOS
Tom Finegan
2012/10/24 21:16:44
Done.
|
| + } |
| + } while (packet.size > 0); |
| + |
| + if (serialized_audio_frames_.size() > 0) { |
| + decoded_frames->set_buffer( |
| + allocator_->Allocate(serialized_audio_frames_.size())); |
| + if (!decoded_frames->buffer()) { |
| + LOG(ERROR) << "DecodeBuffer() cdm::Allocator::Allocate failed."; |
| + return cdm::kDecodeError; |
| + } |
| + memcpy(decoded_frames->buffer()->data(), |
| + &serialized_audio_frames_[0], |
| + serialized_audio_frames_.size()); |
| + serialized_audio_frames_.clear(); |
| + } |
| + |
| + return cdm::kSuccess; |
| +} |
| + |
| +void FFmpegCdmAudioDecoder::ResetOutputTime() { |
|
xhwang
2012/10/24 08:18:15
This function name is a little misleading as we ar
Tom Finegan
2012/10/24 21:16:44
Renamed to ResetAudioTimingData().
|
| + output_timestamp_base_ = media::kNoTimestamp(); |
| + total_frames_decoded_ = 0; |
| + last_input_timestamp_ = media::kNoTimestamp(); |
| + output_bytes_to_drop_ = 0; |
| +} |
| + |
| +void FFmpegCdmAudioDecoder::ReleaseFFmpegResources() { |
| + DVLOG(1) << "ReleaseFFmpegResources()"; |
| + |
| + if (codec_context_) { |
| + av_free(codec_context_->extradata); |
| + avcodec_close(codec_context_); |
| + av_free(codec_context_); |
| + codec_context_ = NULL; |
| + } |
| + if (av_frame_) { |
| + av_free(av_frame_); |
| + av_frame_ = NULL; |
| + } |
| +} |
| + |
| +base::TimeDelta FFmpegCdmAudioDecoder::GetNextOutputTimestamp() const { |
| + DCHECK(output_timestamp_base_ != media::kNoTimestamp()); |
| + double decoded_us = (total_frames_decoded_ / samples_per_second_) * |
| + base::Time::kMicrosecondsPerSecond; |
| + return output_timestamp_base_ + |
| + base::TimeDelta::FromMicroseconds(decoded_us); |
| +} |
| + |
| +void FFmpegCdmAudioDecoder::SerializeInt64(int64 value) { |
| + const uint8_t* ptr = reinterpret_cast<uint8_t*>(&value); |
| + serialized_audio_frames_.insert(serialized_audio_frames_.end(), |
| + ptr, ptr + sizeof(value)); |
| +} |
| + |
| +} // namespace webkit_media |