OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/base/audio_decoder_config.h" | 5 #include "media/base/audio_decoder_config.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "media/audio/sample_rates.h" | 9 #include "media/audio/sample_rates.h" |
10 #include "media/base/limits.h" | 10 #include "media/base/limits.h" |
11 | 11 |
12 namespace media { | 12 namespace media { |
13 | 13 |
| 14 static int SampleFormatToBitsPerChannel(SampleFormat sample_format) { |
| 15 switch (sample_format) { |
| 16 case kUnknownSampleFormat: |
| 17 return 0; |
| 18 case kSampleFormatU8: |
| 19 return 8; |
| 20 case kSampleFormatS16: |
| 21 return 16; |
| 22 case kSampleFormatS32: |
| 23 case kSampleFormatF32: |
| 24 case kSampleFormatPlanarF32: |
| 25 return 32; |
| 26 case kSampleFormatMax: |
| 27 break; |
| 28 } |
| 29 |
| 30 NOTREACHED() << "Invalid sample format provided: " << sample_format; |
| 31 return 0; |
| 32 } |
| 33 |
14 AudioDecoderConfig::AudioDecoderConfig() | 34 AudioDecoderConfig::AudioDecoderConfig() |
15 : codec_(kUnknownAudioCodec), | 35 : codec_(kUnknownAudioCodec), |
| 36 sample_format_(kUnknownSampleFormat), |
16 bits_per_channel_(0), | 37 bits_per_channel_(0), |
17 channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED), | 38 channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED), |
18 samples_per_second_(0), | 39 samples_per_second_(0), |
19 bytes_per_frame_(0), | 40 bytes_per_frame_(0), |
20 extra_data_size_(0), | 41 extra_data_size_(0), |
21 is_encrypted_(false) { | 42 is_encrypted_(false) { |
22 } | 43 } |
23 | 44 |
24 AudioDecoderConfig::AudioDecoderConfig(AudioCodec codec, | 45 AudioDecoderConfig::AudioDecoderConfig(AudioCodec codec, |
25 int bits_per_channel, | 46 SampleFormat sample_format, |
26 ChannelLayout channel_layout, | 47 ChannelLayout channel_layout, |
27 int samples_per_second, | 48 int samples_per_second, |
28 const uint8* extra_data, | 49 const uint8* extra_data, |
29 size_t extra_data_size, | 50 size_t extra_data_size, |
30 bool is_encrypted) { | 51 bool is_encrypted) { |
31 Initialize(codec, bits_per_channel, channel_layout, samples_per_second, | 52 Initialize(codec, sample_format, channel_layout, samples_per_second, |
32 extra_data, extra_data_size, is_encrypted, true); | 53 extra_data, extra_data_size, is_encrypted, true); |
33 } | 54 } |
34 | 55 |
35 void AudioDecoderConfig::Initialize(AudioCodec codec, | 56 void AudioDecoderConfig::Initialize(AudioCodec codec, |
36 int bits_per_channel, | 57 SampleFormat sample_format, |
37 ChannelLayout channel_layout, | 58 ChannelLayout channel_layout, |
38 int samples_per_second, | 59 int samples_per_second, |
39 const uint8* extra_data, | 60 const uint8* extra_data, |
40 size_t extra_data_size, | 61 size_t extra_data_size, |
41 bool is_encrypted, | 62 bool is_encrypted, |
42 bool record_stats) { | 63 bool record_stats) { |
43 CHECK((extra_data_size != 0) == (extra_data != NULL)); | 64 CHECK((extra_data_size != 0) == (extra_data != NULL)); |
44 | 65 |
45 if (record_stats) { | 66 if (record_stats) { |
46 UMA_HISTOGRAM_ENUMERATION("Media.AudioCodec", codec, kAudioCodecMax + 1); | 67 UMA_HISTOGRAM_ENUMERATION("Media.AudioCodec", codec, kAudioCodecMax); |
47 // Fake enum histogram to get exact integral buckets. Expect to never see | 68 UMA_HISTOGRAM_ENUMERATION("Media.AudioSampleFormat", sample_format, |
48 // any values over 32 and even that is huge. | 69 kSampleFormatMax); |
49 UMA_HISTOGRAM_ENUMERATION("Media.AudioBitsPerChannel", bits_per_channel, | |
50 40); | |
51 UMA_HISTOGRAM_ENUMERATION("Media.AudioChannelLayout", channel_layout, | 70 UMA_HISTOGRAM_ENUMERATION("Media.AudioChannelLayout", channel_layout, |
52 CHANNEL_LAYOUT_MAX); | 71 CHANNEL_LAYOUT_MAX); |
53 AudioSampleRate asr = media::AsAudioSampleRate(samples_per_second); | 72 AudioSampleRate asr = media::AsAudioSampleRate(samples_per_second); |
54 if (asr != kUnexpectedAudioSampleRate) { | 73 if (asr != kUnexpectedAudioSampleRate) { |
55 UMA_HISTOGRAM_ENUMERATION("Media.AudioSamplesPerSecond", asr, | 74 UMA_HISTOGRAM_ENUMERATION("Media.AudioSamplesPerSecond", asr, |
56 kUnexpectedAudioSampleRate); | 75 kUnexpectedAudioSampleRate); |
57 } else { | 76 } else { |
58 UMA_HISTOGRAM_COUNTS( | 77 UMA_HISTOGRAM_COUNTS( |
59 "Media.AudioSamplesPerSecondUnexpected", samples_per_second); | 78 "Media.AudioSamplesPerSecondUnexpected", samples_per_second); |
60 } | 79 } |
61 } | 80 } |
62 | 81 |
63 codec_ = codec; | 82 codec_ = codec; |
64 bits_per_channel_ = bits_per_channel; | |
65 channel_layout_ = channel_layout; | 83 channel_layout_ = channel_layout; |
66 samples_per_second_ = samples_per_second; | 84 samples_per_second_ = samples_per_second; |
67 extra_data_size_ = extra_data_size; | 85 extra_data_size_ = extra_data_size; |
| 86 sample_format_ = sample_format; |
| 87 bits_per_channel_ = SampleFormatToBitsPerChannel(sample_format); |
68 | 88 |
69 if (extra_data_size_ > 0) { | 89 if (extra_data_size_ > 0) { |
70 extra_data_.reset(new uint8[extra_data_size_]); | 90 extra_data_.reset(new uint8[extra_data_size_]); |
71 memcpy(extra_data_.get(), extra_data, extra_data_size_); | 91 memcpy(extra_data_.get(), extra_data, extra_data_size_); |
72 } else { | 92 } else { |
73 extra_data_.reset(); | 93 extra_data_.reset(); |
74 } | 94 } |
75 | 95 |
76 is_encrypted_ = is_encrypted; | 96 is_encrypted_ = is_encrypted; |
77 | 97 |
78 int channels = ChannelLayoutToChannelCount(channel_layout_); | 98 int channels = ChannelLayoutToChannelCount(channel_layout_); |
79 bytes_per_frame_ = channels * bits_per_channel_ / 8; | 99 bytes_per_frame_ = channels * bits_per_channel_ / 8; |
80 } | 100 } |
81 | 101 |
82 AudioDecoderConfig::~AudioDecoderConfig() {} | 102 AudioDecoderConfig::~AudioDecoderConfig() {} |
83 | 103 |
84 bool AudioDecoderConfig::IsValidConfig() const { | 104 bool AudioDecoderConfig::IsValidConfig() const { |
85 return codec_ != kUnknownAudioCodec && | 105 return codec_ != kUnknownAudioCodec && |
86 channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED && | 106 channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED && |
87 bits_per_channel_ > 0 && | 107 bits_per_channel_ > 0 && |
88 bits_per_channel_ <= limits::kMaxBitsPerSample && | 108 bits_per_channel_ <= limits::kMaxBitsPerSample && |
89 samples_per_second_ > 0 && | 109 samples_per_second_ > 0 && |
90 samples_per_second_ <= limits::kMaxSampleRate; | 110 samples_per_second_ <= limits::kMaxSampleRate && |
| 111 sample_format_ != kUnknownSampleFormat; |
91 } | 112 } |
92 | 113 |
93 bool AudioDecoderConfig::Matches(const AudioDecoderConfig& config) const { | 114 bool AudioDecoderConfig::Matches(const AudioDecoderConfig& config) const { |
94 return ((codec() == config.codec()) && | 115 return ((codec() == config.codec()) && |
95 (bits_per_channel() == config.bits_per_channel()) && | 116 (bits_per_channel() == config.bits_per_channel()) && |
96 (channel_layout() == config.channel_layout()) && | 117 (channel_layout() == config.channel_layout()) && |
97 (samples_per_second() == config.samples_per_second()) && | 118 (samples_per_second() == config.samples_per_second()) && |
98 (extra_data_size() == config.extra_data_size()) && | 119 (extra_data_size() == config.extra_data_size()) && |
99 (!extra_data() || !memcmp(extra_data(), config.extra_data(), | 120 (!extra_data() || !memcmp(extra_data(), config.extra_data(), |
100 extra_data_size())) && | 121 extra_data_size())) && |
101 (is_encrypted() == config.is_encrypted())); | 122 (is_encrypted() == config.is_encrypted()) && |
| 123 (sample_format() == config.sample_format())); |
102 } | 124 } |
103 | 125 |
104 void AudioDecoderConfig::CopyFrom(const AudioDecoderConfig& audio_config) { | 126 void AudioDecoderConfig::CopyFrom(const AudioDecoderConfig& audio_config) { |
105 Initialize(audio_config.codec(), | 127 Initialize(audio_config.codec(), |
106 audio_config.bits_per_channel(), | 128 audio_config.sample_format(), |
107 audio_config.channel_layout(), | 129 audio_config.channel_layout(), |
108 audio_config.samples_per_second(), | 130 audio_config.samples_per_second(), |
109 audio_config.extra_data(), | 131 audio_config.extra_data(), |
110 audio_config.extra_data_size(), | 132 audio_config.extra_data_size(), |
111 audio_config.is_encrypted(), | 133 audio_config.is_encrypted(), |
112 false); | 134 false); |
113 } | 135 } |
114 | 136 |
115 AudioCodec AudioDecoderConfig::codec() const { | |
116 return codec_; | |
117 } | |
118 | |
119 int AudioDecoderConfig::bits_per_channel() const { | |
120 return bits_per_channel_; | |
121 } | |
122 | |
123 ChannelLayout AudioDecoderConfig::channel_layout() const { | |
124 return channel_layout_; | |
125 } | |
126 | |
127 int AudioDecoderConfig::samples_per_second() const { | |
128 return samples_per_second_; | |
129 } | |
130 | |
131 int AudioDecoderConfig::bytes_per_frame() const { | |
132 return bytes_per_frame_; | |
133 } | |
134 | |
135 uint8* AudioDecoderConfig::extra_data() const { | |
136 return extra_data_.get(); | |
137 } | |
138 | |
139 size_t AudioDecoderConfig::extra_data_size() const { | |
140 return extra_data_size_; | |
141 } | |
142 | |
143 bool AudioDecoderConfig::is_encrypted() const { | |
144 return is_encrypted_; | |
145 } | |
146 | |
147 } // namespace media | 137 } // namespace media |
OLD | NEW |