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
3 // found in the LICENSE file.
4
5 #include "media/filters/opus_audio_decoder.h"
6
7 #include <cmath>
8
9 #include "base/single_thread_task_runner.h"
10 #include "base/sys_byteorder.h"
11 #include "media/base/audio_buffer.h"
12 #include "media/base/audio_decoder_config.h"
13 #include "media/base/audio_discard_helper.h"
14 #include "media/base/bind_to_current_loop.h"
15 #include "media/base/buffers.h"
16 #include "media/base/decoder_buffer.h"
17 #include "third_party/opus/src/include/opus.h"
18 #include "third_party/opus/src/include/opus_multistream.h"
19
20 namespace media {
21
ReadLE16(const uint8 * data,size_t data_size,int read_offset)22 static uint16 ReadLE16(const uint8* data, size_t data_size, int read_offset) {
23 uint16 value = 0;
24 DCHECK_LE(read_offset + sizeof(value), data_size);
25 memcpy(&value, data + read_offset, sizeof(value));
26 return base::ByteSwapToLE16(value);
27 }
28
29 // The Opus specification is part of IETF RFC 6716:
30 // http://tools.ietf.org/html/rfc6716
31
32 // Opus uses Vorbis channel mapping, and Vorbis channel mapping specifies
33 // mappings for up to 8 channels. This information is part of the Vorbis I
34 // Specification:
35 // http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html
36 static const int kMaxVorbisChannels = 8;
37
38 // Maximum packet size used in Xiph's opusdec and FFmpeg's libopusdec.
39 static const int kMaxOpusOutputPacketSizeSamples = 960 * 6;
40
RemapOpusChannelLayout(const uint8 * opus_mapping,int num_channels,uint8 * channel_layout)41 static void RemapOpusChannelLayout(const uint8* opus_mapping,
42 int num_channels,
43 uint8* channel_layout) {
44 DCHECK_LE(num_channels, kMaxVorbisChannels);
45
46 // Opus uses Vorbis channel layout.
47 const int32 num_layouts = kMaxVorbisChannels;
48 const int32 num_layout_values = kMaxVorbisChannels;
49
50 // Vorbis channel ordering for streams with >= 2 channels:
51 // 2 Channels
52 // L, R
53 // 3 Channels
54 // L, Center, R
55 // 4 Channels
56 // Front L, Front R, Back L, Back R
57 // 5 Channels
58 // Front L, Center, Front R, Back L, Back R
59 // 6 Channels (5.1)
60 // Front L, Center, Front R, Back L, Back R, LFE
61 // 7 channels (6.1)
62 // Front L, Front Center, Front R, Side L, Side R, Back Center, LFE
63 // 8 Channels (7.1)
64 // Front L, Center, Front R, Side L, Side R, Back L, Back R, LFE
65 //
66 // Channel ordering information is taken from section 4.3.9 of the Vorbis I
67 // Specification:
68 // http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
69
70 // These are the FFmpeg channel layouts expressed using the position of each
71 // channel in the output stream from libopus.
72 const uint8 kFFmpegChannelLayouts[num_layouts][num_layout_values] = {
73 { 0 },
74
75 // Stereo: No reorder.
76 { 0, 1 },
77
78 // 3 Channels, from Vorbis order to:
79 // L, R, Center
80 { 0, 2, 1 },
81
82 // 4 Channels: No reorder.
83 { 0, 1, 2, 3 },
84
85 // 5 Channels, from Vorbis order to:
86 // Front L, Front R, Center, Back L, Back R
87 { 0, 2, 1, 3, 4 },
88
89 // 6 Channels (5.1), from Vorbis order to:
90 // Front L, Front R, Center, LFE, Back L, Back R
91 { 0, 2, 1, 5, 3, 4 },
92
93 // 7 Channels (6.1), from Vorbis order to:
94 // Front L, Front R, Front Center, LFE, Side L, Side R, Back Center
95 { 0, 2, 1, 6, 3, 4, 5 },
96
97 // 8 Channels (7.1), from Vorbis order to:
98 // Front L, Front R, Center, LFE, Back L, Back R, Side L, Side R
99 { 0, 2, 1, 7, 5, 6, 3, 4 },
100 };
101
102 // Reorder the channels to produce the same ordering as FFmpeg, which is
103 // what the pipeline expects.
104 const uint8* vorbis_layout_offset = kFFmpegChannelLayouts[num_channels - 1];
105 for (int channel = 0; channel < num_channels; ++channel)
106 channel_layout[channel] = opus_mapping[vorbis_layout_offset[channel]];
107 }
108
109 // Opus Extra Data contents:
110 // - "OpusHead" (64 bits)
111 // - version number (8 bits)
112 // - Channels C (8 bits)
113 // - Pre-skip (16 bits)
114 // - Sampling rate (32 bits)
115 // - Gain in dB (16 bits, S7.8)
116 // - Mapping (8 bits, 0=single stream (mono/stereo) 1=Vorbis mapping,
117 // 2..254: reserved, 255: multistream with no mapping)
118 //
119 // - if (mapping != 0)
120 // - N = totel number of streams (8 bits)
121 // - M = number of paired streams (8 bits)
122 // - C times channel origin
123 // - if (C<2*M)
124 // - stream = byte/2
125 // - if (byte&0x1 == 0)
126 // - left
127 // else
128 // - right
129 // - else
130 // - stream = byte-M
131
132 // Default audio output channel layout. Used to initialize |stream_map| in
133 // OpusExtraData, and passed to opus_multistream_decoder_create() when the
134 // extra data does not contain mapping information. The values are valid only
135 // for mono and stereo output: Opus streams with more than 2 channels require a
136 // stream map.
137 static const int kMaxChannelsWithDefaultLayout = 2;
138 static const uint8 kDefaultOpusChannelLayout[kMaxChannelsWithDefaultLayout] = {
139 0, 1 };
140
141 // Size of the Opus extra data excluding optional mapping information.
142 static const int kOpusExtraDataSize = 19;
143
144 // Offset to the channel count byte in the Opus extra data.
145 static const int kOpusExtraDataChannelsOffset = 9;
146
147 // Offset to the pre-skip value in the Opus extra data.
148 static const int kOpusExtraDataSkipSamplesOffset = 10;
149
150 // Offset to the gain value in the Opus extra data.
151 static const int kOpusExtraDataGainOffset = 16;
152
153 // Offset to the channel mapping byte in the Opus extra data.
154 static const int kOpusExtraDataChannelMappingOffset = 18;
155
156 // Extra Data contains a stream map. The mapping values are in extra data beyond
157 // the always present |kOpusExtraDataSize| bytes of data. The mapping data
158 // contains stream count, coupling information, and per channel mapping values:
159 // - Byte 0: Number of streams.
160 // - Byte 1: Number coupled.
161 // - Byte 2: Starting at byte 2 are |extra_data->channels| uint8 mapping
162 // values.
163 static const int kOpusExtraDataNumStreamsOffset = kOpusExtraDataSize;
164 static const int kOpusExtraDataNumCoupledOffset =
165 kOpusExtraDataNumStreamsOffset + 1;
166 static const int kOpusExtraDataStreamMapOffset =
167 kOpusExtraDataNumStreamsOffset + 2;
168
169 struct OpusExtraData {
OpusExtraDatamedia::OpusExtraData170 OpusExtraData()
171 : channels(0),
172 skip_samples(0),
173 channel_mapping(0),
174 num_streams(0),
175 num_coupled(0),
176 gain_db(0),
177 stream_map() {
178 memcpy(stream_map,
179 kDefaultOpusChannelLayout,
180 kMaxChannelsWithDefaultLayout);
181 }
182 int channels;
183 uint16 skip_samples;
184 int channel_mapping;
185 int num_streams;
186 int num_coupled;
187 int16 gain_db;
188 uint8 stream_map[kMaxVorbisChannels];
189 };
190
191 // Returns true when able to successfully parse and store Opus extra data in
192 // |extra_data|. Based on opus header parsing code in libopusdec from FFmpeg,
193 // and opus_header from Xiph's opus-tools project.
ParseOpusExtraData(const uint8 * data,int data_size,const AudioDecoderConfig & config,OpusExtraData * extra_data)194 static bool ParseOpusExtraData(const uint8* data, int data_size,
195 const AudioDecoderConfig& config,
196 OpusExtraData* extra_data) {
197 if (data_size < kOpusExtraDataSize) {
198 DLOG(ERROR) << "Extra data size is too small:" << data_size;
199 return false;
200 }
201
202 extra_data->channels = *(data + kOpusExtraDataChannelsOffset);
203
204 if (extra_data->channels <= 0 || extra_data->channels > kMaxVorbisChannels) {
205 DLOG(ERROR) << "invalid channel count in extra data: "
206 << extra_data->channels;
207 return false;
208 }
209
210 extra_data->skip_samples =
211 ReadLE16(data, data_size, kOpusExtraDataSkipSamplesOffset);
212 extra_data->gain_db = static_cast<int16>(
213 ReadLE16(data, data_size, kOpusExtraDataGainOffset));
214
215 extra_data->channel_mapping = *(data + kOpusExtraDataChannelMappingOffset);
216
217 if (!extra_data->channel_mapping) {
218 if (extra_data->channels > kMaxChannelsWithDefaultLayout) {
219 DLOG(ERROR) << "Invalid extra data, missing stream map.";
220 return false;
221 }
222
223 extra_data->num_streams = 1;
224 extra_data->num_coupled =
225 (ChannelLayoutToChannelCount(config.channel_layout()) > 1) ? 1 : 0;
226 return true;
227 }
228
229 if (data_size < kOpusExtraDataStreamMapOffset + extra_data->channels) {
230 DLOG(ERROR) << "Invalid stream map; insufficient data for current channel "
231 << "count: " << extra_data->channels;
232 return false;
233 }
234
235 extra_data->num_streams = *(data + kOpusExtraDataNumStreamsOffset);
236 extra_data->num_coupled = *(data + kOpusExtraDataNumCoupledOffset);
237
238 if (extra_data->num_streams + extra_data->num_coupled != extra_data->channels)
239 DVLOG(1) << "Inconsistent channel mapping.";
240
241 for (int i = 0; i < extra_data->channels; ++i)
242 extra_data->stream_map[i] = *(data + kOpusExtraDataStreamMapOffset + i);
243 return true;
244 }
245
OpusAudioDecoder(const scoped_refptr<base::SingleThreadTaskRunner> & task_runner)246 OpusAudioDecoder::OpusAudioDecoder(
247 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
248 : task_runner_(task_runner),
249 opus_decoder_(NULL),
250 start_input_timestamp_(kNoTimestamp()) {}
251
Initialize(const AudioDecoderConfig & config,const PipelineStatusCB & status_cb,const OutputCB & output_cb)252 void OpusAudioDecoder::Initialize(const AudioDecoderConfig& config,
253 const PipelineStatusCB& status_cb,
254 const OutputCB& output_cb) {
255 DCHECK(task_runner_->BelongsToCurrentThread());
256 PipelineStatusCB initialize_cb = BindToCurrentLoop(status_cb);
257
258 config_ = config;
259 output_cb_ = BindToCurrentLoop(output_cb);
260
261 if (!ConfigureDecoder()) {
262 initialize_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
263 return;
264 }
265
266 initialize_cb.Run(PIPELINE_OK);
267 }
268
Decode(const scoped_refptr<DecoderBuffer> & buffer,const DecodeCB & decode_cb)269 void OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
270 const DecodeCB& decode_cb) {
271 DCHECK(task_runner_->BelongsToCurrentThread());
272 DCHECK(!decode_cb.is_null());
273
274 DecodeBuffer(buffer, BindToCurrentLoop(decode_cb));
275 }
276
Reset(const base::Closure & closure)277 void OpusAudioDecoder::Reset(const base::Closure& closure) {
278 DCHECK(task_runner_->BelongsToCurrentThread());
279
280 opus_multistream_decoder_ctl(opus_decoder_, OPUS_RESET_STATE);
281 ResetTimestampState();
282 task_runner_->PostTask(FROM_HERE, closure);
283 }
284
Stop()285 void OpusAudioDecoder::Stop() {
286 DCHECK(task_runner_->BelongsToCurrentThread());
287
288 if (!opus_decoder_)
289 return;
290
291 opus_multistream_decoder_ctl(opus_decoder_, OPUS_RESET_STATE);
292 ResetTimestampState();
293 CloseDecoder();
294 }
295
~OpusAudioDecoder()296 OpusAudioDecoder::~OpusAudioDecoder() {}
297
DecodeBuffer(const scoped_refptr<DecoderBuffer> & input,const DecodeCB & decode_cb)298 void OpusAudioDecoder::DecodeBuffer(
299 const scoped_refptr<DecoderBuffer>& input,
300 const DecodeCB& decode_cb) {
301 DCHECK(task_runner_->BelongsToCurrentThread());
302 DCHECK(!decode_cb.is_null());
303
304 DCHECK(input.get());
305
306 // Libopus does not buffer output. Decoding is complete when an end of stream
307 // input buffer is received.
308 if (input->end_of_stream()) {
309 decode_cb.Run(kOk);
310 return;
311 }
312
313 // Make sure we are notified if http://crbug.com/49709 returns. Issue also
314 // occurs with some damaged files.
315 if (input->timestamp() == kNoTimestamp()) {
316 DLOG(ERROR) << "Received a buffer without timestamps!";
317 decode_cb.Run(kDecodeError);
318 return;
319 }
320
321 // Apply the necessary codec delay.
322 if (start_input_timestamp_ == kNoTimestamp())
323 start_input_timestamp_ = input->timestamp();
324 if (!discard_helper_->initialized() &&
325 input->timestamp() == start_input_timestamp_) {
326 discard_helper_->Reset(config_.codec_delay());
327 }
328
329 scoped_refptr<AudioBuffer> output_buffer;
330
331 if (!Decode(input, &output_buffer)) {
332 decode_cb.Run(kDecodeError);
333 return;
334 }
335
336 if (output_buffer) {
337 output_cb_.Run(output_buffer);
338 }
339
340 decode_cb.Run(kOk);
341 }
342
ConfigureDecoder()343 bool OpusAudioDecoder::ConfigureDecoder() {
344 if (config_.codec() != kCodecOpus) {
345 DVLOG(1) << "Codec must be kCodecOpus.";
346 return false;
347 }
348
349 const int channel_count =
350 ChannelLayoutToChannelCount(config_.channel_layout());
351 if (!config_.IsValidConfig() || channel_count > kMaxVorbisChannels) {
352 DLOG(ERROR) << "Invalid or unsupported audio stream -"
353 << " codec: " << config_.codec()
354 << " channel count: " << channel_count
355 << " channel layout: " << config_.channel_layout()
356 << " bits per channel: " << config_.bits_per_channel()
357 << " samples per second: " << config_.samples_per_second();
358 return false;
359 }
360
361 if (config_.is_encrypted()) {
362 DLOG(ERROR) << "Encrypted audio stream not supported.";
363 return false;
364 }
365
366 // Clean up existing decoder if necessary.
367 CloseDecoder();
368
369 // Parse the Opus Extra Data.
370 OpusExtraData opus_extra_data;
371 if (!ParseOpusExtraData(config_.extra_data(), config_.extra_data_size(),
372 config_,
373 &opus_extra_data))
374 return false;
375
376 if (config_.codec_delay() < 0) {
377 DLOG(ERROR) << "Invalid file. Incorrect value for codec delay: "
378 << config_.codec_delay();
379 return false;
380 }
381
382 if (config_.codec_delay() != opus_extra_data.skip_samples) {
383 DLOG(ERROR) << "Invalid file. Codec Delay in container does not match the "
384 << "value in Opus Extra Data. " << config_.codec_delay()
385 << " vs " << opus_extra_data.skip_samples;
386 return false;
387 }
388
389 uint8 channel_mapping[kMaxVorbisChannels] = {0};
390 memcpy(&channel_mapping,
391 kDefaultOpusChannelLayout,
392 kMaxChannelsWithDefaultLayout);
393
394 if (channel_count > kMaxChannelsWithDefaultLayout) {
395 RemapOpusChannelLayout(opus_extra_data.stream_map,
396 channel_count,
397 channel_mapping);
398 }
399
400 // Init Opus.
401 int status = OPUS_INVALID_STATE;
402 opus_decoder_ = opus_multistream_decoder_create(config_.samples_per_second(),
403 channel_count,
404 opus_extra_data.num_streams,
405 opus_extra_data.num_coupled,
406 channel_mapping,
407 &status);
408 if (!opus_decoder_ || status != OPUS_OK) {
409 DLOG(ERROR) << "opus_multistream_decoder_create failed status="
410 << opus_strerror(status);
411 return false;
412 }
413
414 status = opus_multistream_decoder_ctl(
415 opus_decoder_, OPUS_SET_GAIN(opus_extra_data.gain_db));
416 if (status != OPUS_OK) {
417 DLOG(ERROR) << "Failed to set OPUS header gain; status="
418 << opus_strerror(status);
419 return false;
420 }
421
422 discard_helper_.reset(
423 new AudioDiscardHelper(config_.samples_per_second(), 0));
424 start_input_timestamp_ = kNoTimestamp();
425 return true;
426 }
427
CloseDecoder()428 void OpusAudioDecoder::CloseDecoder() {
429 if (opus_decoder_) {
430 opus_multistream_decoder_destroy(opus_decoder_);
431 opus_decoder_ = NULL;
432 }
433 }
434
ResetTimestampState()435 void OpusAudioDecoder::ResetTimestampState() {
436 discard_helper_->Reset(
437 discard_helper_->TimeDeltaToFrames(config_.seek_preroll()));
438 }
439
Decode(const scoped_refptr<DecoderBuffer> & input,scoped_refptr<AudioBuffer> * output_buffer)440 bool OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& input,
441 scoped_refptr<AudioBuffer>* output_buffer) {
442 // Allocate a buffer for the output samples.
443 *output_buffer = AudioBuffer::CreateBuffer(
444 config_.sample_format(),
445 config_.channel_layout(),
446 ChannelLayoutToChannelCount(config_.channel_layout()),
447 config_.samples_per_second(),
448 kMaxOpusOutputPacketSizeSamples);
449 const int buffer_size =
450 output_buffer->get()->channel_count() *
451 output_buffer->get()->frame_count() *
452 SampleFormatToBytesPerChannel(config_.sample_format());
453
454 float* float_output_buffer = reinterpret_cast<float*>(
455 output_buffer->get()->channel_data()[0]);
456 const int frames_decoded =
457 opus_multistream_decode_float(opus_decoder_,
458 input->data(),
459 input->data_size(),
460 float_output_buffer,
461 buffer_size,
462 0);
463
464 if (frames_decoded < 0) {
465 DLOG(ERROR) << "opus_multistream_decode failed for"
466 << " timestamp: " << input->timestamp().InMicroseconds()
467 << " us, duration: " << input->duration().InMicroseconds()
468 << " us, packet size: " << input->data_size() << " bytes with"
469 << " status: " << opus_strerror(frames_decoded);
470 return false;
471 }
472
473 // Trim off any extraneous allocation.
474 DCHECK_LE(frames_decoded, output_buffer->get()->frame_count());
475 const int trim_frames = output_buffer->get()->frame_count() - frames_decoded;
476 if (trim_frames > 0)
477 output_buffer->get()->TrimEnd(trim_frames);
478
479 // Handles discards and timestamping. Discard the buffer if more data needed.
480 if (!discard_helper_->ProcessBuffers(input, *output_buffer))
481 *output_buffer = NULL;
482
483 return true;
484 }
485
486 } // namespace media
487