Chromium Code Reviews| Index: media/filters/opus_audio_decoder.cc |
| diff --git a/media/filters/opus_audio_decoder.cc b/media/filters/opus_audio_decoder.cc |
| index 37e1abda698328542350b510558a1763cda60392..a3cfa91d337de3b2e5d07e66d15ae590b5be3ab1 100644 |
| --- a/media/filters/opus_audio_decoder.cc |
| +++ b/media/filters/opus_audio_decoder.cc |
| @@ -25,7 +25,6 @@ |
| namespace media { |
| static uint16 ReadLE16(const uint8* data, size_t data_size, int read_offset) { |
| - DCHECK(data); |
| uint16 value = 0; |
| DCHECK_LE(read_offset + sizeof(value), data_size); |
| memcpy(&value, data + read_offset, sizeof(value)); |
| @@ -46,15 +45,8 @@ static int TimeDeltaToAudioFrames(base::TimeDelta time_delta, |
| // http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html |
| static const int kMaxVorbisChannels = 8; |
| -// Opus allows for decode of S16 or float samples. OpusAudioDecoder always uses |
| -// S16 samples. |
| -static const int kBitsPerChannel = 16; |
| -static const int kBytesPerChannel = kBitsPerChannel / 8; |
| - |
| // Maximum packet size used in Xiph's opusdec and FFmpeg's libopusdec. |
| -static const int kMaxOpusOutputPacketSizeSamples = 960 * 6 * kMaxVorbisChannels; |
| -static const int kMaxOpusOutputPacketSizeBytes = |
| - kMaxOpusOutputPacketSizeSamples * kBytesPerChannel; |
| +static const int kMaxOpusOutputPacketSizeSamples = 960 * 6; |
| static void RemapOpusChannelLayout(const uint8* opus_mapping, |
| int num_channels, |
| @@ -206,13 +198,16 @@ struct OpusExtraData { |
| static bool ParseOpusExtraData(const uint8* data, int data_size, |
| const AudioDecoderConfig& config, |
| OpusExtraData* extra_data) { |
| - if (data_size < kOpusExtraDataSize) |
| + if (data_size < kOpusExtraDataSize) { |
| + DLOG(ERROR) << "Extra data size is too small:" << data_size; |
| return false; |
| + } |
| extra_data->channels = *(data + kOpusExtraDataChannelsOffset); |
| if (extra_data->channels <= 0 || extra_data->channels > kMaxVorbisChannels) { |
| - DVLOG(0) << "invalid channel count in extra data: " << extra_data->channels; |
| + DLOG(ERROR) << "invalid channel count in extra data: " |
| + << extra_data->channels; |
| return false; |
| } |
| @@ -223,7 +218,7 @@ static bool ParseOpusExtraData(const uint8* data, int data_size, |
| if (!extra_data->channel_mapping) { |
| if (extra_data->channels > kMaxChannelsWithDefaultLayout) { |
| - DVLOG(0) << "Invalid extra data, missing stream map."; |
| + DLOG(ERROR) << "Invalid extra data, missing stream map."; |
| return false; |
| } |
| @@ -234,8 +229,8 @@ static bool ParseOpusExtraData(const uint8* data, int data_size, |
| } |
| if (data_size < kOpusExtraDataStreamMapOffset + extra_data->channels) { |
| - DVLOG(0) << "Invalid stream map; insufficient data for current channel " |
| - << "count: " << extra_data->channels; |
| + DLOG(ERROR) << "Invalid stream map; insufficient data for current channel " |
| + << "count: " << extra_data->channels; |
| return false; |
| } |
| @@ -259,6 +254,7 @@ OpusAudioDecoder::OpusAudioDecoder( |
| bits_per_channel_(0), |
| channel_layout_(CHANNEL_LAYOUT_NONE), |
| samples_per_second_(0), |
| + sample_format_(kUnknownSampleFormat), |
| last_input_timestamp_(kNoTimestamp()), |
| frames_to_discard_(0), |
| frame_delay_at_start_(0) { |
| @@ -274,7 +270,7 @@ void OpusAudioDecoder::Initialize( |
| if (demuxer_stream_) { |
| // TODO(scherkus): initialization currently happens more than once in |
| // PipelineIntegrationTest.BasicPlayback. |
| - DVLOG(0) << "Initialize has already been called."; |
| + DLOG(ERROR) << "Initialize has already been called."; |
|
acolwell GONE FROM CHROMIUM
2013/12/11 23:30:02
This looks like stale code. Please remove.
DaleCurtis
2013/12/12 00:03:24
The whole block? Or just the DLOG?
acolwell GONE FROM CHROMIUM
2013/12/12 00:53:14
The whole block.
|
| CHECK(false); |
| } |
| @@ -375,7 +371,7 @@ void OpusAudioDecoder::BufferReady( |
| // occurs with some damaged files. |
| if (input->timestamp() == kNoTimestamp() && |
| output_timestamp_helper_->base_timestamp() == kNoTimestamp()) { |
| - DVLOG(1) << "Received a buffer without timestamps!"; |
| + DLOG(ERROR) << "Received a buffer without timestamps!"; |
| base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); |
| return; |
| } |
| @@ -384,9 +380,9 @@ void OpusAudioDecoder::BufferReady( |
| input->timestamp() != kNoTimestamp() && |
| input->timestamp() < last_input_timestamp_) { |
| base::TimeDelta diff = input->timestamp() - last_input_timestamp_; |
| - DVLOG(1) << "Input timestamps are not monotonically increasing! " |
| - << " ts " << input->timestamp().InMicroseconds() << " us" |
| - << " diff " << diff.InMicroseconds() << " us"; |
| + DLOG(ERROR) << "Input timestamps are not monotonically increasing! " |
| + << " ts " << input->timestamp().InMicroseconds() << " us" |
| + << " diff " << diff.InMicroseconds() << " us"; |
| base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); |
| return; |
| } |
| @@ -414,29 +410,24 @@ bool OpusAudioDecoder::ConfigureDecoder() { |
| const AudioDecoderConfig& config = demuxer_stream_->audio_decoder_config(); |
| if (config.codec() != kCodecOpus) { |
| - DVLOG(0) << "codec must be kCodecOpus."; |
| + DLOG(ERROR) << "codec must be kCodecOpus."; |
| return false; |
| } |
| const int channel_count = |
| ChannelLayoutToChannelCount(config.channel_layout()); |
| if (!config.IsValidConfig() || channel_count > kMaxVorbisChannels) { |
| - DVLOG(0) << "Invalid or unsupported audio stream -" |
| - << " codec: " << config.codec() |
| - << " channel count: " << channel_count |
| - << " channel layout: " << config.channel_layout() |
| - << " bits per channel: " << config.bits_per_channel() |
| - << " samples per second: " << config.samples_per_second(); |
| - return false; |
| - } |
| - |
| - if (config.bits_per_channel() != kBitsPerChannel) { |
| - DVLOG(0) << "16 bit samples required."; |
| + DLOG(ERROR) << "Invalid or unsupported audio stream -" |
| + << " codec: " << config.codec() |
| + << " channel count: " << channel_count |
| + << " channel layout: " << config.channel_layout() |
| + << " bits per channel: " << config.bits_per_channel() |
| + << " samples per second: " << config.samples_per_second(); |
| return false; |
| } |
| if (config.is_encrypted()) { |
| - DVLOG(0) << "Encrypted audio stream not supported."; |
| + DLOG(ERROR) << "Encrypted audio stream not supported."; |
| return false; |
| } |
| @@ -444,23 +435,19 @@ bool OpusAudioDecoder::ConfigureDecoder() { |
| (bits_per_channel_ != config.bits_per_channel() || |
| channel_layout_ != config.channel_layout() || |
| samples_per_second_ != config.samples_per_second())) { |
| - DVLOG(1) << "Unsupported config change :"; |
| - DVLOG(1) << "\tbits_per_channel : " << bits_per_channel_ |
| - << " -> " << config.bits_per_channel(); |
| - DVLOG(1) << "\tchannel_layout : " << channel_layout_ |
| - << " -> " << config.channel_layout(); |
| - DVLOG(1) << "\tsample_rate : " << samples_per_second_ |
| - << " -> " << config.samples_per_second(); |
| + DLOG(ERROR) << "Unsupported config change -" |
| + << " bits_per_channel: " << bits_per_channel_ |
| + << " -> " << config.bits_per_channel() |
| + << ", channel_layout: " << channel_layout_ |
| + << " -> " << config.channel_layout() |
| + << ", sample_rate: " << samples_per_second_ |
| + << " -> " << config.samples_per_second(); |
| return false; |
| } |
| // Clean up existing decoder if necessary. |
| CloseDecoder(); |
| - // Allocate the output buffer if necessary. |
| - if (!output_buffer_) |
| - output_buffer_.reset(new int16[kMaxOpusOutputPacketSizeSamples]); |
| - |
| // Parse the Opus Extra Data. |
| OpusExtraData opus_extra_data; |
| if (!ParseOpusExtraData(config.extra_data(), config.extra_data_size(), |
| @@ -468,24 +455,23 @@ bool OpusAudioDecoder::ConfigureDecoder() { |
| &opus_extra_data)) |
| return false; |
| - if (!config.codec_delay().InMicroseconds()) |
| - return false; |
| - |
| // Convert from seconds to samples. |
| timestamp_offset_ = config.codec_delay(); |
| frame_delay_at_start_ = TimeDeltaToAudioFrames(config.codec_delay(), |
| config.samples_per_second()); |
| - if (frame_delay_at_start_ < 0) { |
| - DVLOG(1) << "Invalid file. Incorrect value for codec delay."; |
| + if (timestamp_offset_ <= base::TimeDelta() || frame_delay_at_start_ < 0) { |
| + DLOG(ERROR) << "Invalid file. Incorrect value for codec delay: " |
| + << config.codec_delay().InMicroseconds(); |
| return false; |
| } |
| + |
| if (frame_delay_at_start_ != opus_extra_data.skip_samples) { |
| - DVLOG(1) << "Invalid file. Codec Delay in container does not match the " |
| - << "value in Opus Extra Data."; |
| + DLOG(ERROR) << "Invalid file. Codec Delay in container does not match the " |
| + << "value in Opus Extra Data."; |
| return false; |
| } |
| - uint8 channel_mapping[kMaxVorbisChannels]; |
| + uint8 channel_mapping[kMaxVorbisChannels] = {0}; |
|
acolwell GONE FROM CHROMIUM
2013/12/11 23:30:02
nit: Why is this change needed? Doesn't it immedia
DaleCurtis
2013/12/12 00:03:24
Not necessary, but good for sanity. The memcpy bel
|
| memcpy(&channel_mapping, |
| kDefaultOpusChannelLayout, |
| kMaxChannelsWithDefaultLayout); |
| @@ -505,12 +491,19 @@ bool OpusAudioDecoder::ConfigureDecoder() { |
| channel_mapping, |
| &status); |
| if (!opus_decoder_ || status != OPUS_OK) { |
| - DVLOG(0) << "opus_multistream_decoder_create failed status=" |
| + DLOG(ERROR) << "opus_multistream_decoder_create failed status=" |
| << opus_strerror(status); |
| return false; |
| } |
| - bits_per_channel_ = config.bits_per_channel(); |
| + // Android uses a fixed point build of the opus decoder. |
| +#if defined(OS_ANDROID) |
|
acolwell GONE FROM CHROMIUM
2013/12/11 23:30:02
nit: Any reason not to move these changes to the c
DaleCurtis
2013/12/12 00:03:24
Done.
|
| + sample_format_ = kSampleFormatS16; |
| +#else |
| + sample_format_ = kSampleFormatF32; |
| +#endif |
| + |
| + bits_per_channel_ = SampleFormatToBytesPerChannel(sample_format_) * 8; |
| channel_layout_ = config.channel_layout(); |
| samples_per_second_ = config.samples_per_second(); |
| output_timestamp_helper_.reset( |
| @@ -535,31 +528,53 @@ void OpusAudioDecoder::ResetTimestampState() { |
| bool OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& input, |
| scoped_refptr<AudioBuffer>* output_buffer) { |
| - int frames_decoded = opus_multistream_decode(opus_decoder_, |
| - input->data(), |
| - input->data_size(), |
| - &output_buffer_[0], |
| - kMaxOpusOutputPacketSizeSamples, |
| - 0); |
| + // Allocate a buffer for the output samples. |
| + *output_buffer = AudioBuffer::CreateBuffer( |
| + sample_format_, |
| + ChannelLayoutToChannelCount(channel_layout_), |
| + kMaxOpusOutputPacketSizeSamples); |
| + const int buffer_size = |
| + output_buffer->get()->channel_count() * |
| + output_buffer->get()->frame_count() * |
| + SampleFormatToBytesPerChannel(sample_format_); |
| + |
| + // Android uses a fixed point build of the opus decoder. |
| +#if defined(OS_ANDROID) |
|
acolwell GONE FROM CHROMIUM
2013/12/11 23:30:02
I don't believe we actually even use this code on
DaleCurtis
2013/12/12 00:03:24
Hmm, vignesh said it works on Android? Vignesh?
|
| + int16* int16_output_buffer = reinterpret_cast<int16*>( |
| + output_buffer->get()->channel_data()[0]); |
| + int frames_decoded = |
| + opus_multistream_decode(opus_decoder_, |
| + input->data(), |
| + input->data_size(), |
| + int16_output_buffer, |
| + buffer_size, |
| + 0); |
| +#else |
| + float* float_output_buffer = reinterpret_cast<float*>( |
| + output_buffer->get()->channel_data()[0]); |
| + int frames_decoded = |
| + opus_multistream_decode_float(opus_decoder_, |
| + input->data(), |
| + input->data_size(), |
| + float_output_buffer, |
| + buffer_size, |
| + 0); |
| +#endif |
| + |
| if (frames_decoded < 0) { |
| - DVLOG(0) << "opus_multistream_decode failed for" |
| - << " timestamp: " << input->timestamp().InMicroseconds() |
| - << " us, duration: " << input->duration().InMicroseconds() |
| - << " us, packet size: " << input->data_size() << " bytes with" |
| - << " status: " << opus_strerror(frames_decoded); |
| + DLOG(ERROR) << "opus_multistream_decode failed for" |
| + << " timestamp: " << input->timestamp().InMicroseconds() |
| + << " us, duration: " << input->duration().InMicroseconds() |
| + << " us, packet size: " << input->data_size() << " bytes with" |
| + << " status: " << opus_strerror(frames_decoded); |
| return false; |
| } |
| - uint8* decoded_audio_data = reinterpret_cast<uint8*>(&output_buffer_[0]); |
| - int bytes_decoded = frames_decoded * |
| - demuxer_stream_->audio_decoder_config().bytes_per_frame(); |
| - DCHECK_LE(bytes_decoded, kMaxOpusOutputPacketSizeBytes); |
| - |
| - if (output_timestamp_helper_->base_timestamp() == kNoTimestamp() && |
| - !input->end_of_stream()) { |
| - DCHECK(input->timestamp() != kNoTimestamp()); |
| - output_timestamp_helper_->SetBaseTimestamp(input->timestamp()); |
| - } |
| + // Trim off any extraneous allocation. |
| + DCHECK_LE(frames_decoded, output_buffer->get()->frame_count()); |
| + const int trim_frames = output_buffer->get()->frame_count() - frames_decoded; |
| + if (trim_frames > 0) |
| + output_buffer->get()->TrimEnd(trim_frames); |
| // Skip samples should be equal to codec delay when the file starts and when |
| // there is a seek to zero. |
| @@ -568,17 +583,8 @@ bool OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& input, |
| if (input->timestamp() == base::TimeDelta()) |
| frames_to_discard_ = frame_delay_at_start_; |
| - if (bytes_decoded > 0 && frames_decoded > frames_to_discard_) { |
| - // Copy the audio samples into an output buffer. |
| - uint8* data[] = { decoded_audio_data }; |
| - *output_buffer = AudioBuffer::CopyFrom( |
| - kSampleFormatS16, |
| - ChannelLayoutToChannelCount(channel_layout_), |
| - frames_decoded, |
| - data, |
| - output_timestamp_helper_->GetTimestamp() - timestamp_offset_, |
| - output_timestamp_helper_->GetFrameDuration(frames_decoded)); |
| - output_timestamp_helper_->AddFrames(frames_decoded); |
| + // Handle frame discard and trimming. |
| + if (frames_decoded > 0) { |
| if (frames_to_discard_ > 0) { |
|
acolwell GONE FROM CHROMIUM
2013/12/11 23:30:02
This doesn't look right to me. It assumes that fra
DaleCurtis
2013/12/12 00:03:24
Good eye, during testing I realized it was broken.
|
| output_buffer->get()->TrimStart(frames_to_discard_); |
| frames_decoded -= frames_to_discard_; |
| @@ -594,9 +600,6 @@ bool OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& input, |
| output_buffer->get()->TrimEnd(discard_padding); |
| frames_decoded -= discard_padding; |
| } |
| - } else if (bytes_decoded > 0) { |
| - frames_to_discard_ -= frames_decoded; |
| - frames_decoded = 0; |
| } |
| // Decoding finished successfully, update statistics. |
| @@ -605,8 +608,22 @@ bool OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& input, |
| statistics_cb_.Run(statistics); |
| // Discard the buffer to indicate we need more data. |
| - if (!frames_decoded) |
| + if (!frames_decoded) { |
| *output_buffer = NULL; |
| + return true; |
| + } |
| + |
| + if (output_timestamp_helper_->base_timestamp() == kNoTimestamp() && |
|
acolwell GONE FROM CHROMIUM
2013/12/11 23:30:02
This also does not look right to me. This assumes
DaleCurtis
2013/12/12 00:03:24
Done. Note: I still have some hackish detection f
|
| + !input->end_of_stream()) { |
| + DCHECK(input->timestamp() != kNoTimestamp()); |
| + output_timestamp_helper_->SetBaseTimestamp(input->timestamp()); |
| + } |
| + |
| + output_buffer->get()->set_timestamp( |
| + output_timestamp_helper_->GetTimestamp() - timestamp_offset_); |
| + output_buffer->get()->set_duration( |
| + output_timestamp_helper_->GetFrameDuration(frames_decoded)); |
| + output_timestamp_helper_->AddFrames(frames_decoded); |
| return true; |
| } |