michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "mp4_demuxer/audio_decoder_config.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: namespace mp4_demuxer { michael@0: michael@0: static int SampleFormatToBitsPerChannel(SampleFormat sample_format) { michael@0: switch (sample_format) { michael@0: case kUnknownSampleFormat: michael@0: return 0; michael@0: case kSampleFormatU8: michael@0: return 8; michael@0: case kSampleFormatS16: michael@0: case kSampleFormatPlanarS16: michael@0: return 16; michael@0: case kSampleFormatS32: michael@0: case kSampleFormatF32: michael@0: case kSampleFormatPlanarF32: michael@0: return 32; michael@0: case kSampleFormatMax: michael@0: break; michael@0: } michael@0: michael@0: //NOTREACHED() << "Invalid sample format provided: " << sample_format; michael@0: return 0; michael@0: } michael@0: michael@0: AudioDecoderConfig::AudioDecoderConfig() michael@0: : codec_(kUnknownAudioCodec), michael@0: sample_format_(kUnknownSampleFormat), michael@0: bits_per_channel_(0), michael@0: channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED), michael@0: samples_per_second_(0), michael@0: bytes_per_frame_(0), michael@0: is_encrypted_(false) { michael@0: } michael@0: michael@0: AudioDecoderConfig::AudioDecoderConfig(AudioCodec codec, michael@0: SampleFormat sample_format, michael@0: ChannelLayout channel_layout, michael@0: int samples_per_second, michael@0: const uint8_t* extra_data, michael@0: size_t extra_data_size, michael@0: bool is_encrypted) { michael@0: Initialize(codec, sample_format, channel_layout, samples_per_second, michael@0: extra_data, extra_data_size, is_encrypted); michael@0: } michael@0: michael@0: void AudioDecoderConfig::Initialize(AudioCodec codec, michael@0: SampleFormat sample_format, michael@0: ChannelLayout channel_layout, michael@0: int samples_per_second, michael@0: const uint8_t* extra_data, michael@0: size_t extra_data_size, michael@0: bool is_encrypted) { michael@0: CHECK((extra_data_size != 0) == (extra_data != NULL)); michael@0: michael@0: codec_ = codec; michael@0: channel_layout_ = channel_layout; michael@0: samples_per_second_ = samples_per_second; michael@0: sample_format_ = sample_format; michael@0: bits_per_channel_ = SampleFormatToBitsPerChannel(sample_format); michael@0: extra_data_.assign(extra_data, extra_data + extra_data_size); michael@0: is_encrypted_ = is_encrypted; michael@0: michael@0: int channels = ChannelLayoutToChannelCount(channel_layout_); michael@0: bytes_per_frame_ = channels * bits_per_channel_ / 8; michael@0: } michael@0: michael@0: AudioDecoderConfig::~AudioDecoderConfig() {} michael@0: michael@0: bool AudioDecoderConfig::IsValidConfig() const { michael@0: return codec_ != kUnknownAudioCodec && michael@0: channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED && michael@0: bits_per_channel_ > 0 && michael@0: bits_per_channel_ <= kMaxBitsPerSample && michael@0: samples_per_second_ > 0 && michael@0: samples_per_second_ <= kMaxSampleRate && michael@0: sample_format_ != kUnknownSampleFormat; michael@0: } michael@0: michael@0: bool AudioDecoderConfig::Matches(const AudioDecoderConfig& config) const { michael@0: return ((codec() == config.codec()) && michael@0: (bits_per_channel() == config.bits_per_channel()) && michael@0: (channel_layout() == config.channel_layout()) && michael@0: (samples_per_second() == config.samples_per_second()) && michael@0: (extra_data_size() == config.extra_data_size()) && michael@0: (!extra_data() || !memcmp(extra_data(), config.extra_data(), michael@0: extra_data_size())) && michael@0: (is_encrypted() == config.is_encrypted()) && michael@0: (sample_format() == config.sample_format())); michael@0: } michael@0: michael@0: std::string AudioDecoderConfig::AsHumanReadableString() const { michael@0: std::ostringstream s; michael@0: s << "codec: " << codec() michael@0: << " bits/channel: " << bits_per_channel() michael@0: << " samples/s: " << samples_per_second() michael@0: << " has extra data? " << (extra_data() ? "true" : "false") michael@0: << " encrypted? " << (is_encrypted() ? "true" : "false"); michael@0: return s.str(); michael@0: } michael@0: michael@0: } // namespace mp4_demuxer