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/filters/opus_audio_decoder.h" | 5 #include "media/filters/opus_audio_decoder.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 150 |
151 // Size of the Opus extra data excluding optional mapping information. | 151 // Size of the Opus extra data excluding optional mapping information. |
152 static const int kOpusExtraDataSize = 19; | 152 static const int kOpusExtraDataSize = 19; |
153 | 153 |
154 // Offset to the channel count byte in the Opus extra data. | 154 // Offset to the channel count byte in the Opus extra data. |
155 static const int kOpusExtraDataChannelsOffset = 9; | 155 static const int kOpusExtraDataChannelsOffset = 9; |
156 | 156 |
157 // Offset to the pre-skip value in the Opus extra data. | 157 // Offset to the pre-skip value in the Opus extra data. |
158 static const int kOpusExtraDataSkipSamplesOffset = 10; | 158 static const int kOpusExtraDataSkipSamplesOffset = 10; |
159 | 159 |
| 160 // Offset to the gain value in the Opus extra data. |
| 161 static const int kOpusExtraDataGainOffset = 16; |
| 162 |
160 // Offset to the channel mapping byte in the Opus extra data. | 163 // Offset to the channel mapping byte in the Opus extra data. |
161 static const int kOpusExtraDataChannelMappingOffset = 18; | 164 static const int kOpusExtraDataChannelMappingOffset = 18; |
162 | 165 |
163 // Extra Data contains a stream map. The mapping values are in extra data beyond | 166 // Extra Data contains a stream map. The mapping values are in extra data beyond |
164 // the always present |kOpusExtraDataSize| bytes of data. The mapping data | 167 // the always present |kOpusExtraDataSize| bytes of data. The mapping data |
165 // contains stream count, coupling information, and per channel mapping values: | 168 // contains stream count, coupling information, and per channel mapping values: |
166 // - Byte 0: Number of streams. | 169 // - Byte 0: Number of streams. |
167 // - Byte 1: Number coupled. | 170 // - Byte 1: Number coupled. |
168 // - Byte 2: Starting at byte 2 are |extra_data->channels| uint8 mapping | 171 // - Byte 2: Starting at byte 2 are |extra_data->channels| uint8 mapping |
169 // values. | 172 // values. |
170 static const int kOpusExtraDataNumStreamsOffset = kOpusExtraDataSize; | 173 static const int kOpusExtraDataNumStreamsOffset = kOpusExtraDataSize; |
171 static const int kOpusExtraDataNumCoupledOffset = | 174 static const int kOpusExtraDataNumCoupledOffset = |
172 kOpusExtraDataNumStreamsOffset + 1; | 175 kOpusExtraDataNumStreamsOffset + 1; |
173 static const int kOpusExtraDataStreamMapOffset = | 176 static const int kOpusExtraDataStreamMapOffset = |
174 kOpusExtraDataNumStreamsOffset + 2; | 177 kOpusExtraDataNumStreamsOffset + 2; |
175 | 178 |
176 struct OpusExtraData { | 179 struct OpusExtraData { |
177 OpusExtraData() | 180 OpusExtraData() |
178 : channels(0), | 181 : channels(0), |
179 skip_samples(0), | 182 skip_samples(0), |
180 channel_mapping(0), | 183 channel_mapping(0), |
181 num_streams(0), | 184 num_streams(0), |
182 num_coupled(0) { | 185 num_coupled(0), |
| 186 gain_db(0) { |
183 memcpy(stream_map, | 187 memcpy(stream_map, |
184 kDefaultOpusChannelLayout, | 188 kDefaultOpusChannelLayout, |
185 kMaxChannelsWithDefaultLayout); | 189 kMaxChannelsWithDefaultLayout); |
186 } | 190 } |
187 int channels; | 191 int channels; |
188 int skip_samples; | 192 uint16 skip_samples; |
189 int channel_mapping; | 193 int channel_mapping; |
190 int num_streams; | 194 int num_streams; |
191 int num_coupled; | 195 int num_coupled; |
| 196 int16 gain_db; |
192 uint8 stream_map[kMaxVorbisChannels]; | 197 uint8 stream_map[kMaxVorbisChannels]; |
193 }; | 198 }; |
194 | 199 |
195 // Returns true when able to successfully parse and store Opus extra data in | 200 // Returns true when able to successfully parse and store Opus extra data in |
196 // |extra_data|. Based on opus header parsing code in libopusdec from FFmpeg, | 201 // |extra_data|. Based on opus header parsing code in libopusdec from FFmpeg, |
197 // and opus_header from Xiph's opus-tools project. | 202 // and opus_header from Xiph's opus-tools project. |
198 static bool ParseOpusExtraData(const uint8* data, int data_size, | 203 static bool ParseOpusExtraData(const uint8* data, int data_size, |
199 const AudioDecoderConfig& config, | 204 const AudioDecoderConfig& config, |
200 OpusExtraData* extra_data) { | 205 OpusExtraData* extra_data) { |
201 if (data_size < kOpusExtraDataSize) { | 206 if (data_size < kOpusExtraDataSize) { |
202 DLOG(ERROR) << "Extra data size is too small:" << data_size; | 207 DLOG(ERROR) << "Extra data size is too small:" << data_size; |
203 return false; | 208 return false; |
204 } | 209 } |
205 | 210 |
206 extra_data->channels = *(data + kOpusExtraDataChannelsOffset); | 211 extra_data->channels = *(data + kOpusExtraDataChannelsOffset); |
207 | 212 |
208 if (extra_data->channels <= 0 || extra_data->channels > kMaxVorbisChannels) { | 213 if (extra_data->channels <= 0 || extra_data->channels > kMaxVorbisChannels) { |
209 DLOG(ERROR) << "invalid channel count in extra data: " | 214 DLOG(ERROR) << "invalid channel count in extra data: " |
210 << extra_data->channels; | 215 << extra_data->channels; |
211 return false; | 216 return false; |
212 } | 217 } |
213 | 218 |
214 extra_data->skip_samples = | 219 extra_data->skip_samples = |
215 ReadLE16(data, data_size, kOpusExtraDataSkipSamplesOffset); | 220 ReadLE16(data, data_size, kOpusExtraDataSkipSamplesOffset); |
| 221 extra_data->gain_db = static_cast<int16>( |
| 222 ReadLE16(data, data_size, kOpusExtraDataGainOffset)); |
216 | 223 |
217 extra_data->channel_mapping = *(data + kOpusExtraDataChannelMappingOffset); | 224 extra_data->channel_mapping = *(data + kOpusExtraDataChannelMappingOffset); |
218 | 225 |
219 if (!extra_data->channel_mapping) { | 226 if (!extra_data->channel_mapping) { |
220 if (extra_data->channels > kMaxChannelsWithDefaultLayout) { | 227 if (extra_data->channels > kMaxChannelsWithDefaultLayout) { |
221 DLOG(ERROR) << "Invalid extra data, missing stream map."; | 228 DLOG(ERROR) << "Invalid extra data, missing stream map."; |
222 return false; | 229 return false; |
223 } | 230 } |
224 | 231 |
225 extra_data->num_streams = 1; | 232 extra_data->num_streams = 1; |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 opus_extra_data.num_streams, | 502 opus_extra_data.num_streams, |
496 opus_extra_data.num_coupled, | 503 opus_extra_data.num_coupled, |
497 channel_mapping, | 504 channel_mapping, |
498 &status); | 505 &status); |
499 if (!opus_decoder_ || status != OPUS_OK) { | 506 if (!opus_decoder_ || status != OPUS_OK) { |
500 DLOG(ERROR) << "opus_multistream_decoder_create failed status=" | 507 DLOG(ERROR) << "opus_multistream_decoder_create failed status=" |
501 << opus_strerror(status); | 508 << opus_strerror(status); |
502 return false; | 509 return false; |
503 } | 510 } |
504 | 511 |
| 512 status = opus_multistream_decoder_ctl( |
| 513 opus_decoder_, OPUS_SET_GAIN(opus_extra_data.gain_db)); |
| 514 if (status != OPUS_OK) { |
| 515 DLOG(ERROR) << "Failed to set OPUS header gain; status=" |
| 516 << opus_strerror(status); |
| 517 return false; |
| 518 } |
| 519 |
505 channel_layout_ = config.channel_layout(); | 520 channel_layout_ = config.channel_layout(); |
506 samples_per_second_ = config.samples_per_second(); | 521 samples_per_second_ = config.samples_per_second(); |
507 output_timestamp_helper_.reset( | 522 output_timestamp_helper_.reset( |
508 new AudioTimestampHelper(config.samples_per_second())); | 523 new AudioTimestampHelper(config.samples_per_second())); |
509 start_input_timestamp_ = kNoTimestamp(); | 524 start_input_timestamp_ = kNoTimestamp(); |
510 return true; | 525 return true; |
511 } | 526 } |
512 | 527 |
513 void OpusAudioDecoder::CloseDecoder() { | 528 void OpusAudioDecoder::CloseDecoder() { |
514 if (opus_decoder_) { | 529 if (opus_decoder_) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 output_timestamp_helper_->AddFrames(frames_decoded); | 619 output_timestamp_helper_->AddFrames(frames_decoded); |
605 | 620 |
606 // Discard the buffer to indicate we need more data. | 621 // Discard the buffer to indicate we need more data. |
607 if (!frames_to_output) | 622 if (!frames_to_output) |
608 *output_buffer = NULL; | 623 *output_buffer = NULL; |
609 | 624 |
610 return true; | 625 return true; |
611 } | 626 } |
612 | 627 |
613 } // namespace media | 628 } // namespace media |
OLD | NEW |