Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: media/filters/opus_audio_decoder.cc

Issue 104873011: Add support for setting OPUS header gain. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/message_loop/message_loop_proxy.h" 12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/sys_byteorder.h" 13 #include "base/sys_byteorder.h"
14 #include "media/base/audio_buffer.h" 14 #include "media/base/audio_buffer.h"
15 #include "media/base/audio_decoder_config.h" 15 #include "media/base/audio_decoder_config.h"
16 #include "media/base/audio_timestamp_helper.h" 16 #include "media/base/audio_timestamp_helper.h"
17 #include "media/base/bind_to_loop.h" 17 #include "media/base/bind_to_loop.h"
18 #include "media/base/buffers.h" 18 #include "media/base/buffers.h"
19 #include "media/base/decoder_buffer.h" 19 #include "media/base/decoder_buffer.h"
20 #include "media/base/demuxer.h" 20 #include "media/base/demuxer.h"
21 #include "media/base/pipeline.h" 21 #include "media/base/pipeline.h"
22 #include "third_party/opus/src/include/opus.h" 22 #include "third_party/opus/src/include/opus.h"
23 #include "third_party/opus/src/include/opus_multistream.h" 23 #include "third_party/opus/src/include/opus_multistream.h"
24 24
25 namespace media { 25 namespace media {
26 26
27 static uint16 ReadLE16(const uint8* data, size_t data_size, int read_offset) { 27 template <typename T>
28 uint16 value = 0; 28 static T ReadLE16(const uint8* data, size_t data_size, int read_offset) {
29 T value = 0;
29 DCHECK_LE(read_offset + sizeof(value), data_size); 30 DCHECK_LE(read_offset + sizeof(value), data_size);
30 memcpy(&value, data + read_offset, sizeof(value)); 31 memcpy(&value, data + read_offset, sizeof(value));
31 return base::ByteSwapToLE16(value); 32 return base::ByteSwapToLE16(value);
32 } 33 }
33 34
34 static int TimeDeltaToAudioFrames(base::TimeDelta time_delta, 35 static int TimeDeltaToAudioFrames(base::TimeDelta time_delta,
35 int frame_rate) { 36 int frame_rate) {
36 return std::ceil(time_delta.InSecondsF() * frame_rate); 37 return std::ceil(time_delta.InSecondsF() * frame_rate);
37 } 38 }
38 39
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 151
151 // Size of the Opus extra data excluding optional mapping information. 152 // Size of the Opus extra data excluding optional mapping information.
152 static const int kOpusExtraDataSize = 19; 153 static const int kOpusExtraDataSize = 19;
153 154
154 // Offset to the channel count byte in the Opus extra data. 155 // Offset to the channel count byte in the Opus extra data.
155 static const int kOpusExtraDataChannelsOffset = 9; 156 static const int kOpusExtraDataChannelsOffset = 9;
156 157
157 // Offset to the pre-skip value in the Opus extra data. 158 // Offset to the pre-skip value in the Opus extra data.
158 static const int kOpusExtraDataSkipSamplesOffset = 10; 159 static const int kOpusExtraDataSkipSamplesOffset = 10;
159 160
161 // Offset to the gain value in the Opus extra data.
162 static const int kOpusExtraDataGainOffset = 16;
163
160 // Offset to the channel mapping byte in the Opus extra data. 164 // Offset to the channel mapping byte in the Opus extra data.
161 static const int kOpusExtraDataChannelMappingOffset = 18; 165 static const int kOpusExtraDataChannelMappingOffset = 18;
162 166
163 // Extra Data contains a stream map. The mapping values are in extra data beyond 167 // 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 168 // the always present |kOpusExtraDataSize| bytes of data. The mapping data
165 // contains stream count, coupling information, and per channel mapping values: 169 // contains stream count, coupling information, and per channel mapping values:
166 // - Byte 0: Number of streams. 170 // - Byte 0: Number of streams.
167 // - Byte 1: Number coupled. 171 // - Byte 1: Number coupled.
168 // - Byte 2: Starting at byte 2 are |extra_data->channels| uint8 mapping 172 // - Byte 2: Starting at byte 2 are |extra_data->channels| uint8 mapping
169 // values. 173 // values.
170 static const int kOpusExtraDataNumStreamsOffset = kOpusExtraDataSize; 174 static const int kOpusExtraDataNumStreamsOffset = kOpusExtraDataSize;
171 static const int kOpusExtraDataNumCoupledOffset = 175 static const int kOpusExtraDataNumCoupledOffset =
172 kOpusExtraDataNumStreamsOffset + 1; 176 kOpusExtraDataNumStreamsOffset + 1;
173 static const int kOpusExtraDataStreamMapOffset = 177 static const int kOpusExtraDataStreamMapOffset =
174 kOpusExtraDataNumStreamsOffset + 2; 178 kOpusExtraDataNumStreamsOffset + 2;
175 179
176 struct OpusExtraData { 180 struct OpusExtraData {
177 OpusExtraData() 181 OpusExtraData()
178 : channels(0), 182 : channels(0),
179 skip_samples(0), 183 skip_samples(0),
180 channel_mapping(0), 184 channel_mapping(0),
181 num_streams(0), 185 num_streams(0),
182 num_coupled(0) { 186 num_coupled(0),
187 gain_db(0) {
183 memcpy(stream_map, 188 memcpy(stream_map,
184 kDefaultOpusChannelLayout, 189 kDefaultOpusChannelLayout,
185 kMaxChannelsWithDefaultLayout); 190 kMaxChannelsWithDefaultLayout);
186 } 191 }
187 int channels; 192 int channels;
188 int skip_samples; 193 uint16_t skip_samples;
189 int channel_mapping; 194 int channel_mapping;
190 int num_streams; 195 int num_streams;
191 int num_coupled; 196 int num_coupled;
197 int16_t gain_db;
192 uint8 stream_map[kMaxVorbisChannels]; 198 uint8 stream_map[kMaxVorbisChannels];
193 }; 199 };
194 200
195 // Returns true when able to successfully parse and store Opus extra data in 201 // 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, 202 // |extra_data|. Based on opus header parsing code in libopusdec from FFmpeg,
197 // and opus_header from Xiph's opus-tools project. 203 // and opus_header from Xiph's opus-tools project.
198 static bool ParseOpusExtraData(const uint8* data, int data_size, 204 static bool ParseOpusExtraData(const uint8* data, int data_size,
199 const AudioDecoderConfig& config, 205 const AudioDecoderConfig& config,
200 OpusExtraData* extra_data) { 206 OpusExtraData* extra_data) {
201 if (data_size < kOpusExtraDataSize) { 207 if (data_size < kOpusExtraDataSize) {
202 DLOG(ERROR) << "Extra data size is too small:" << data_size; 208 DLOG(ERROR) << "Extra data size is too small:" << data_size;
203 return false; 209 return false;
204 } 210 }
205 211
206 extra_data->channels = *(data + kOpusExtraDataChannelsOffset); 212 extra_data->channels = *(data + kOpusExtraDataChannelsOffset);
207 213
208 if (extra_data->channels <= 0 || extra_data->channels > kMaxVorbisChannels) { 214 if (extra_data->channels <= 0 || extra_data->channels > kMaxVorbisChannels) {
209 DLOG(ERROR) << "invalid channel count in extra data: " 215 DLOG(ERROR) << "invalid channel count in extra data: "
210 << extra_data->channels; 216 << extra_data->channels;
211 return false; 217 return false;
212 } 218 }
213 219
214 extra_data->skip_samples = 220 extra_data->skip_samples =
215 ReadLE16(data, data_size, kOpusExtraDataSkipSamplesOffset); 221 ReadLE16<uint16_t>(data, data_size, kOpusExtraDataSkipSamplesOffset);
222 extra_data->gain_db =
223 ReadLE16<int16_t>(data, data_size, kOpusExtraDataGainOffset);
acolwell GONE FROM CHROMIUM 2013/12/16 21:37:08 nit: How about just putting a static_cast<int16_t>
DaleCurtis 2013/12/16 22:21:04 Nice catch, I've reverted this in place of just us
216 224
217 extra_data->channel_mapping = *(data + kOpusExtraDataChannelMappingOffset); 225 extra_data->channel_mapping = *(data + kOpusExtraDataChannelMappingOffset);
218 226
219 if (!extra_data->channel_mapping) { 227 if (!extra_data->channel_mapping) {
220 if (extra_data->channels > kMaxChannelsWithDefaultLayout) { 228 if (extra_data->channels > kMaxChannelsWithDefaultLayout) {
221 DLOG(ERROR) << "Invalid extra data, missing stream map."; 229 DLOG(ERROR) << "Invalid extra data, missing stream map.";
222 return false; 230 return false;
223 } 231 }
224 232
225 extra_data->num_streams = 1; 233 extra_data->num_streams = 1;
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 opus_extra_data.num_streams, 503 opus_extra_data.num_streams,
496 opus_extra_data.num_coupled, 504 opus_extra_data.num_coupled,
497 channel_mapping, 505 channel_mapping,
498 &status); 506 &status);
499 if (!opus_decoder_ || status != OPUS_OK) { 507 if (!opus_decoder_ || status != OPUS_OK) {
500 DLOG(ERROR) << "opus_multistream_decoder_create failed status=" 508 DLOG(ERROR) << "opus_multistream_decoder_create failed status="
501 << opus_strerror(status); 509 << opus_strerror(status);
502 return false; 510 return false;
503 } 511 }
504 512
513 status = opus_multistream_decoder_ctl(
514 opus_decoder_, OPUS_SET_GAIN(opus_extra_data.gain_db));
515 if (status != OPUS_OK) {
516 DLOG(WARNING) << "Failed to set OPUS header gain. status="
517 << opus_strerror(status);
acolwell GONE FROM CHROMIUM 2013/12/16 21:37:08 nit: Shouldn't we return false here since this is
DaleCurtis 2013/12/16 22:21:04 While the docs don't make it clear, errors will be
vignesh 2013/12/16 22:24:51 We do strict checking on other fields (like codec_
DaleCurtis 2013/12/16 22:28:02 The bounds are int16_min -> int16_max, it's enforc
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
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
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698