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