• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bind.h"
10 #include "base/callback_helpers.h"
11 #include "base/location.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/sys_byteorder.h"
14 #include "media/base/audio_buffer.h"
15 #include "media/base/audio_decoder_config.h"
16 #include "media/base/audio_timestamp_helper.h"
17 #include "media/base/bind_to_loop.h"
18 #include "media/base/buffers.h"
19 #include "media/base/decoder_buffer.h"
20 #include "media/base/demuxer.h"
21 #include "media/base/pipeline.h"
22 #include "third_party/opus/src/include/opus.h"
23 #include "third_party/opus/src/include/opus_multistream.h"
24 
25 namespace media {
26 
ReadLE16(const uint8 * data,size_t data_size,int read_offset)27 static uint16 ReadLE16(const uint8* data, size_t data_size, int read_offset) {
28   uint16 value = 0;
29   DCHECK_LE(read_offset + sizeof(value), data_size);
30   memcpy(&value, data + read_offset, sizeof(value));
31   return base::ByteSwapToLE16(value);
32 }
33 
TimeDeltaToAudioFrames(base::TimeDelta time_delta,int frame_rate)34 static int TimeDeltaToAudioFrames(base::TimeDelta time_delta,
35                                   int frame_rate) {
36   return std::ceil(time_delta.InSecondsF() * frame_rate);
37 }
38 
39 // The Opus specification is part of IETF RFC 6716:
40 // http://tools.ietf.org/html/rfc6716
41 
42 // Opus uses Vorbis channel mapping, and Vorbis channel mapping specifies
43 // mappings for up to 8 channels. This information is part of the Vorbis I
44 // Specification:
45 // http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html
46 static const int kMaxVorbisChannels = 8;
47 
48 // Maximum packet size used in Xiph's opusdec and FFmpeg's libopusdec.
49 static const int kMaxOpusOutputPacketSizeSamples = 960 * 6;
50 
RemapOpusChannelLayout(const uint8 * opus_mapping,int num_channels,uint8 * channel_layout)51 static void RemapOpusChannelLayout(const uint8* opus_mapping,
52                                    int num_channels,
53                                    uint8* channel_layout) {
54   DCHECK_LE(num_channels, kMaxVorbisChannels);
55 
56   // Opus uses Vorbis channel layout.
57   const int32 num_layouts = kMaxVorbisChannels;
58   const int32 num_layout_values = kMaxVorbisChannels;
59 
60   // Vorbis channel ordering for streams with >= 2 channels:
61   // 2 Channels
62   //   L, R
63   // 3 Channels
64   //   L, Center, R
65   // 4 Channels
66   //   Front L, Front R, Back L, Back R
67   // 5 Channels
68   //   Front L, Center, Front R, Back L, Back R
69   // 6 Channels (5.1)
70   //   Front L, Center, Front R, Back L, Back R, LFE
71   // 7 channels (6.1)
72   //   Front L, Front Center, Front R, Side L, Side R, Back Center, LFE
73   // 8 Channels (7.1)
74   //   Front L, Center, Front R, Side L, Side R, Back L, Back R, LFE
75   //
76   // Channel ordering information is taken from section 4.3.9 of the Vorbis I
77   // Specification:
78   // http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
79 
80   // These are the FFmpeg channel layouts expressed using the position of each
81   // channel in the output stream from libopus.
82   const uint8 kFFmpegChannelLayouts[num_layouts][num_layout_values] = {
83     { 0 },
84 
85     // Stereo: No reorder.
86     { 0, 1 },
87 
88     // 3 Channels, from Vorbis order to:
89     //  L, R, Center
90     { 0, 2, 1 },
91 
92     // 4 Channels: No reorder.
93     { 0, 1, 2, 3 },
94 
95     // 5 Channels, from Vorbis order to:
96     //  Front L, Front R, Center, Back L, Back R
97     { 0, 2, 1, 3, 4 },
98 
99     // 6 Channels (5.1), from Vorbis order to:
100     //  Front L, Front R, Center, LFE, Back L, Back R
101     { 0, 2, 1, 5, 3, 4 },
102 
103     // 7 Channels (6.1), from Vorbis order to:
104     //  Front L, Front R, Front Center, LFE, Side L, Side R, Back Center
105     { 0, 2, 1, 6, 3, 4, 5 },
106 
107     // 8 Channels (7.1), from Vorbis order to:
108     //  Front L, Front R, Center, LFE, Back L, Back R, Side L, Side R
109     { 0, 2, 1, 7, 5, 6, 3, 4 },
110   };
111 
112   // Reorder the channels to produce the same ordering as FFmpeg, which is
113   // what the pipeline expects.
114   const uint8* vorbis_layout_offset = kFFmpegChannelLayouts[num_channels - 1];
115   for (int channel = 0; channel < num_channels; ++channel)
116     channel_layout[channel] = opus_mapping[vorbis_layout_offset[channel]];
117 }
118 
119 // Opus Extra Data contents:
120 // - "OpusHead" (64 bits)
121 // - version number (8 bits)
122 // - Channels C (8 bits)
123 // - Pre-skip (16 bits)
124 // - Sampling rate (32 bits)
125 // - Gain in dB (16 bits, S7.8)
126 // - Mapping (8 bits, 0=single stream (mono/stereo) 1=Vorbis mapping,
127 //            2..254: reserved, 255: multistream with no mapping)
128 //
129 // - if (mapping != 0)
130 //    - N = totel number of streams (8 bits)
131 //    - M = number of paired streams (8 bits)
132 //    - C times channel origin
133 //         - if (C<2*M)
134 //            - stream = byte/2
135 //            - if (byte&0x1 == 0)
136 //                - left
137 //              else
138 //                - right
139 //         - else
140 //            - stream = byte-M
141 
142 // Default audio output channel layout. Used to initialize |stream_map| in
143 // OpusExtraData, and passed to opus_multistream_decoder_create() when the
144 // extra data does not contain mapping information. The values are valid only
145 // for mono and stereo output: Opus streams with more than 2 channels require a
146 // stream map.
147 static const int kMaxChannelsWithDefaultLayout = 2;
148 static const uint8 kDefaultOpusChannelLayout[kMaxChannelsWithDefaultLayout] = {
149     0, 1 };
150 
151 // Size of the Opus extra data excluding optional mapping information.
152 static const int kOpusExtraDataSize = 19;
153 
154 // Offset to the channel count byte in the Opus extra data.
155 static const int kOpusExtraDataChannelsOffset = 9;
156 
157 // Offset to the pre-skip value in the Opus extra data.
158 static const int kOpusExtraDataSkipSamplesOffset = 10;
159 
160 // Offset to the gain value in the Opus extra data.
161 static const int kOpusExtraDataGainOffset = 16;
162 
163 // Offset to the channel mapping byte in the Opus extra data.
164 static const int kOpusExtraDataChannelMappingOffset = 18;
165 
166 // Extra Data contains a stream map. The mapping values are in extra data beyond
167 // the always present |kOpusExtraDataSize| bytes of data. The mapping data
168 // contains stream count, coupling information, and per channel mapping values:
169 //   - Byte 0: Number of streams.
170 //   - Byte 1: Number coupled.
171 //   - Byte 2: Starting at byte 2 are |extra_data->channels| uint8 mapping
172 //             values.
173 static const int kOpusExtraDataNumStreamsOffset = kOpusExtraDataSize;
174 static const int kOpusExtraDataNumCoupledOffset =
175     kOpusExtraDataNumStreamsOffset + 1;
176 static const int kOpusExtraDataStreamMapOffset =
177     kOpusExtraDataNumStreamsOffset + 2;
178 
179 struct OpusExtraData {
OpusExtraDatamedia::OpusExtraData180   OpusExtraData()
181       : channels(0),
182         skip_samples(0),
183         channel_mapping(0),
184         num_streams(0),
185         num_coupled(0),
186         gain_db(0) {
187     memcpy(stream_map,
188            kDefaultOpusChannelLayout,
189            kMaxChannelsWithDefaultLayout);
190   }
191   int channels;
192   uint16 skip_samples;
193   int channel_mapping;
194   int num_streams;
195   int num_coupled;
196   int16 gain_db;
197   uint8 stream_map[kMaxVorbisChannels];
198 };
199 
200 // Returns true when able to successfully parse and store Opus extra data in
201 // |extra_data|. Based on opus header parsing code in libopusdec from FFmpeg,
202 // and opus_header from Xiph's opus-tools project.
ParseOpusExtraData(const uint8 * data,int data_size,const AudioDecoderConfig & config,OpusExtraData * extra_data)203 static bool ParseOpusExtraData(const uint8* data, int data_size,
204                                const AudioDecoderConfig& config,
205                                OpusExtraData* extra_data) {
206   if (data_size < kOpusExtraDataSize) {
207     DLOG(ERROR) << "Extra data size is too small:" << data_size;
208     return false;
209   }
210 
211   extra_data->channels = *(data + kOpusExtraDataChannelsOffset);
212 
213   if (extra_data->channels <= 0 || extra_data->channels > kMaxVorbisChannels) {
214     DLOG(ERROR) << "invalid channel count in extra data: "
215                 << extra_data->channels;
216     return false;
217   }
218 
219   extra_data->skip_samples =
220       ReadLE16(data, data_size, kOpusExtraDataSkipSamplesOffset);
221   extra_data->gain_db = static_cast<int16>(
222       ReadLE16(data, data_size, kOpusExtraDataGainOffset));
223 
224   extra_data->channel_mapping = *(data + kOpusExtraDataChannelMappingOffset);
225 
226   if (!extra_data->channel_mapping) {
227     if (extra_data->channels > kMaxChannelsWithDefaultLayout) {
228       DLOG(ERROR) << "Invalid extra data, missing stream map.";
229       return false;
230     }
231 
232     extra_data->num_streams = 1;
233     extra_data->num_coupled =
234         (ChannelLayoutToChannelCount(config.channel_layout()) > 1) ? 1 : 0;
235     return true;
236   }
237 
238   if (data_size < kOpusExtraDataStreamMapOffset + extra_data->channels) {
239     DLOG(ERROR) << "Invalid stream map; insufficient data for current channel "
240                 << "count: " << extra_data->channels;
241     return false;
242   }
243 
244   extra_data->num_streams = *(data + kOpusExtraDataNumStreamsOffset);
245   extra_data->num_coupled = *(data + kOpusExtraDataNumCoupledOffset);
246 
247   if (extra_data->num_streams + extra_data->num_coupled != extra_data->channels)
248     DVLOG(1) << "Inconsistent channel mapping.";
249 
250   for (int i = 0; i < extra_data->channels; ++i)
251     extra_data->stream_map[i] = *(data + kOpusExtraDataStreamMapOffset + i);
252   return true;
253 }
254 
OpusAudioDecoder(const scoped_refptr<base::MessageLoopProxy> & message_loop)255 OpusAudioDecoder::OpusAudioDecoder(
256     const scoped_refptr<base::MessageLoopProxy>& message_loop)
257     : message_loop_(message_loop),
258       weak_factory_(this),
259       demuxer_stream_(NULL),
260       opus_decoder_(NULL),
261       channel_layout_(CHANNEL_LAYOUT_NONE),
262       samples_per_second_(0),
263       sample_format_(kSampleFormatF32),
264       bits_per_channel_(SampleFormatToBytesPerChannel(sample_format_) * 8),
265       last_input_timestamp_(kNoTimestamp()),
266       frames_to_discard_(0),
267       frame_delay_at_start_(0),
268       start_input_timestamp_(kNoTimestamp()) {
269 }
270 
Initialize(DemuxerStream * stream,const PipelineStatusCB & status_cb,const StatisticsCB & statistics_cb)271 void OpusAudioDecoder::Initialize(
272     DemuxerStream* stream,
273     const PipelineStatusCB& status_cb,
274     const StatisticsCB& statistics_cb) {
275   DCHECK(message_loop_->BelongsToCurrentThread());
276   PipelineStatusCB initialize_cb = BindToCurrentLoop(status_cb);
277 
278   if (demuxer_stream_) {
279     // TODO(scherkus): initialization currently happens more than once in
280     // PipelineIntegrationTest.BasicPlayback.
281     DLOG(ERROR) << "Initialize has already been called.";
282     CHECK(false);
283   }
284 
285   weak_this_ = weak_factory_.GetWeakPtr();
286   demuxer_stream_ = stream;
287 
288   if (!ConfigureDecoder()) {
289     initialize_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
290     return;
291   }
292 
293   statistics_cb_ = statistics_cb;
294   initialize_cb.Run(PIPELINE_OK);
295 }
296 
Read(const ReadCB & read_cb)297 void OpusAudioDecoder::Read(const ReadCB& read_cb) {
298   DCHECK(message_loop_->BelongsToCurrentThread());
299   DCHECK(!read_cb.is_null());
300   CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported.";
301   read_cb_ = BindToCurrentLoop(read_cb);
302 
303   ReadFromDemuxerStream();
304 }
305 
bits_per_channel()306 int OpusAudioDecoder::bits_per_channel() {
307   DCHECK(message_loop_->BelongsToCurrentThread());
308   return bits_per_channel_;
309 }
310 
channel_layout()311 ChannelLayout OpusAudioDecoder::channel_layout() {
312   DCHECK(message_loop_->BelongsToCurrentThread());
313   return channel_layout_;
314 }
315 
samples_per_second()316 int OpusAudioDecoder::samples_per_second() {
317   DCHECK(message_loop_->BelongsToCurrentThread());
318   return samples_per_second_;
319 }
320 
Reset(const base::Closure & closure)321 void OpusAudioDecoder::Reset(const base::Closure& closure) {
322   DCHECK(message_loop_->BelongsToCurrentThread());
323   base::Closure reset_cb = BindToCurrentLoop(closure);
324 
325   opus_multistream_decoder_ctl(opus_decoder_, OPUS_RESET_STATE);
326   ResetTimestampState();
327   reset_cb.Run();
328 }
329 
~OpusAudioDecoder()330 OpusAudioDecoder::~OpusAudioDecoder() {
331   // TODO(scherkus): should we require Stop() to be called? this might end up
332   // getting called on a random thread due to refcounting.
333   CloseDecoder();
334 }
335 
ReadFromDemuxerStream()336 void OpusAudioDecoder::ReadFromDemuxerStream() {
337   DCHECK(!read_cb_.is_null());
338   demuxer_stream_->Read(base::Bind(&OpusAudioDecoder::BufferReady, weak_this_));
339 }
340 
BufferReady(DemuxerStream::Status status,const scoped_refptr<DecoderBuffer> & input)341 void OpusAudioDecoder::BufferReady(
342     DemuxerStream::Status status,
343     const scoped_refptr<DecoderBuffer>& input) {
344   DCHECK(message_loop_->BelongsToCurrentThread());
345   DCHECK(!read_cb_.is_null());
346   DCHECK_EQ(status != DemuxerStream::kOk, !input.get()) << status;
347 
348   if (status == DemuxerStream::kAborted) {
349     DCHECK(!input.get());
350     base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
351     return;
352   }
353 
354   if (status == DemuxerStream::kConfigChanged) {
355     DCHECK(!input.get());
356     DVLOG(1) << "Config changed.";
357 
358     if (!ConfigureDecoder()) {
359       base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL);
360       return;
361     }
362 
363     ResetTimestampState();
364     ReadFromDemuxerStream();
365     return;
366   }
367 
368   DCHECK_EQ(status, DemuxerStream::kOk);
369   DCHECK(input.get());
370 
371   // Libopus does not buffer output. Decoding is complete when an end of stream
372   // input buffer is received.
373   if (input->end_of_stream()) {
374     base::ResetAndReturn(&read_cb_).Run(kOk, AudioBuffer::CreateEOSBuffer());
375     return;
376   }
377 
378   // Make sure we are notified if http://crbug.com/49709 returns.  Issue also
379   // occurs with some damaged files.
380   if (input->timestamp() == kNoTimestamp() &&
381       output_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
382     DLOG(ERROR) << "Received a buffer without timestamps!";
383     base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL);
384     return;
385   }
386 
387   if (last_input_timestamp_ != kNoTimestamp() &&
388       input->timestamp() != kNoTimestamp() &&
389       input->timestamp() < last_input_timestamp_) {
390     base::TimeDelta diff = input->timestamp() - last_input_timestamp_;
391     DLOG(ERROR) << "Input timestamps are not monotonically increasing! "
392                 << " ts " << input->timestamp().InMicroseconds() << " us"
393                 << " diff " << diff.InMicroseconds() << " us";
394     base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL);
395     return;
396   }
397 
398   // Apply the necessary codec delay.
399   if (start_input_timestamp_ == kNoTimestamp())
400     start_input_timestamp_ = input->timestamp();
401   if (last_input_timestamp_ == kNoTimestamp() &&
402       input->timestamp() == start_input_timestamp_) {
403     frames_to_discard_ = frame_delay_at_start_;
404   }
405 
406   last_input_timestamp_ = input->timestamp();
407 
408   scoped_refptr<AudioBuffer> output_buffer;
409 
410   if (!Decode(input, &output_buffer)) {
411     base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL);
412     return;
413   }
414 
415   if (output_buffer.get()) {
416     // Execute callback to return the decoded audio.
417     base::ResetAndReturn(&read_cb_).Run(kOk, output_buffer);
418   } else {
419     // We exhausted the input data, but it wasn't enough for a frame.  Ask for
420     // more data in order to fulfill this read.
421     ReadFromDemuxerStream();
422   }
423 }
424 
ConfigureDecoder()425 bool OpusAudioDecoder::ConfigureDecoder() {
426   const AudioDecoderConfig& config = demuxer_stream_->audio_decoder_config();
427 
428   if (config.codec() != kCodecOpus) {
429     DVLOG(1) << "Codec must be kCodecOpus.";
430     return false;
431   }
432 
433   const int channel_count =
434       ChannelLayoutToChannelCount(config.channel_layout());
435   if (!config.IsValidConfig() || channel_count > kMaxVorbisChannels) {
436     DLOG(ERROR) << "Invalid or unsupported audio stream -"
437                 << " codec: " << config.codec()
438                 << " channel count: " << channel_count
439                 << " channel layout: " << config.channel_layout()
440                 << " bits per channel: " << config.bits_per_channel()
441                 << " samples per second: " << config.samples_per_second();
442     return false;
443   }
444 
445   if (config.is_encrypted()) {
446     DLOG(ERROR) << "Encrypted audio stream not supported.";
447     return false;
448   }
449 
450   if (opus_decoder_ &&
451       (channel_layout_ != config.channel_layout() ||
452        samples_per_second_ != config.samples_per_second())) {
453     DLOG(ERROR) << "Unsupported config change -"
454                 << ", channel_layout: " << channel_layout_
455                 << " -> " << config.channel_layout()
456                 << ", sample_rate: " << samples_per_second_
457                 << " -> " << config.samples_per_second();
458     return false;
459   }
460 
461   // Clean up existing decoder if necessary.
462   CloseDecoder();
463 
464   // Parse the Opus Extra Data.
465   OpusExtraData opus_extra_data;
466   if (!ParseOpusExtraData(config.extra_data(), config.extra_data_size(),
467                           config,
468                           &opus_extra_data))
469     return false;
470 
471   // Convert from seconds to samples.
472   timestamp_offset_ = config.codec_delay();
473   frame_delay_at_start_ = TimeDeltaToAudioFrames(config.codec_delay(),
474                                                  config.samples_per_second());
475   if (timestamp_offset_ <= base::TimeDelta() || frame_delay_at_start_ < 0) {
476     DLOG(ERROR) << "Invalid file. Incorrect value for codec delay: "
477                 << config.codec_delay().InMicroseconds();
478     return false;
479   }
480 
481   if (frame_delay_at_start_ != opus_extra_data.skip_samples) {
482     DLOG(ERROR) << "Invalid file. Codec Delay in container does not match the "
483                 << "value in Opus Extra Data.";
484     return false;
485   }
486 
487   uint8 channel_mapping[kMaxVorbisChannels] = {0};
488   memcpy(&channel_mapping,
489          kDefaultOpusChannelLayout,
490          kMaxChannelsWithDefaultLayout);
491 
492   if (channel_count > kMaxChannelsWithDefaultLayout) {
493     RemapOpusChannelLayout(opus_extra_data.stream_map,
494                            channel_count,
495                            channel_mapping);
496   }
497 
498   // Init Opus.
499   int status = OPUS_INVALID_STATE;
500   opus_decoder_ = opus_multistream_decoder_create(config.samples_per_second(),
501                                                   channel_count,
502                                                   opus_extra_data.num_streams,
503                                                   opus_extra_data.num_coupled,
504                                                   channel_mapping,
505                                                   &status);
506   if (!opus_decoder_ || status != OPUS_OK) {
507     DLOG(ERROR) << "opus_multistream_decoder_create failed status="
508                 << opus_strerror(status);
509     return false;
510   }
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 
520   channel_layout_ = config.channel_layout();
521   samples_per_second_ = config.samples_per_second();
522   output_timestamp_helper_.reset(
523       new AudioTimestampHelper(config.samples_per_second()));
524   start_input_timestamp_ = kNoTimestamp();
525   return true;
526 }
527 
CloseDecoder()528 void OpusAudioDecoder::CloseDecoder() {
529   if (opus_decoder_) {
530     opus_multistream_decoder_destroy(opus_decoder_);
531     opus_decoder_ = NULL;
532   }
533 }
534 
ResetTimestampState()535 void OpusAudioDecoder::ResetTimestampState() {
536   output_timestamp_helper_->SetBaseTimestamp(kNoTimestamp());
537   last_input_timestamp_ = kNoTimestamp();
538   frames_to_discard_ = TimeDeltaToAudioFrames(
539       demuxer_stream_->audio_decoder_config().seek_preroll(),
540       samples_per_second_);
541 }
542 
Decode(const scoped_refptr<DecoderBuffer> & input,scoped_refptr<AudioBuffer> * output_buffer)543 bool OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& input,
544                               scoped_refptr<AudioBuffer>* output_buffer) {
545   // Allocate a buffer for the output samples.
546   *output_buffer = AudioBuffer::CreateBuffer(
547       sample_format_,
548       ChannelLayoutToChannelCount(channel_layout_),
549       kMaxOpusOutputPacketSizeSamples);
550   const int buffer_size =
551       output_buffer->get()->channel_count() *
552       output_buffer->get()->frame_count() *
553       SampleFormatToBytesPerChannel(sample_format_);
554 
555   float* float_output_buffer = reinterpret_cast<float*>(
556       output_buffer->get()->channel_data()[0]);
557   const int frames_decoded =
558       opus_multistream_decode_float(opus_decoder_,
559                                     input->data(),
560                                     input->data_size(),
561                                     float_output_buffer,
562                                     buffer_size,
563                                     0);
564 
565   if (frames_decoded < 0) {
566     DLOG(ERROR) << "opus_multistream_decode failed for"
567                 << " timestamp: " << input->timestamp().InMicroseconds()
568                 << " us, duration: " << input->duration().InMicroseconds()
569                 << " us, packet size: " << input->data_size() << " bytes with"
570                 << " status: " << opus_strerror(frames_decoded);
571     return false;
572   }
573 
574   if (output_timestamp_helper_->base_timestamp() == kNoTimestamp() &&
575       !input->end_of_stream()) {
576     DCHECK(input->timestamp() != kNoTimestamp());
577     output_timestamp_helper_->SetBaseTimestamp(input->timestamp());
578   }
579 
580   // Trim off any extraneous allocation.
581   DCHECK_LE(frames_decoded, output_buffer->get()->frame_count());
582   const int trim_frames = output_buffer->get()->frame_count() - frames_decoded;
583   if (trim_frames > 0)
584     output_buffer->get()->TrimEnd(trim_frames);
585 
586   // Handle frame discard and trimming.
587   int frames_to_output = frames_decoded;
588   if (frames_decoded > frames_to_discard_) {
589     if (frames_to_discard_ > 0) {
590       output_buffer->get()->TrimStart(frames_to_discard_);
591       frames_to_output -= frames_to_discard_;
592       frames_to_discard_ = 0;
593     }
594     if (input->discard_padding().InMicroseconds() > 0) {
595       int discard_padding = TimeDeltaToAudioFrames(input->discard_padding(),
596                                                    samples_per_second_);
597       if (discard_padding < 0 || discard_padding > frames_to_output) {
598         DVLOG(1) << "Invalid file. Incorrect discard padding value.";
599         return false;
600       }
601       output_buffer->get()->TrimEnd(discard_padding);
602       frames_to_output -= discard_padding;
603     }
604   } else {
605     frames_to_discard_ -= frames_to_output;
606     frames_to_output = 0;
607   }
608 
609   // Decoding finished successfully, update statistics.
610   PipelineStatistics statistics;
611   statistics.audio_bytes_decoded = input->data_size();
612   statistics_cb_.Run(statistics);
613 
614   // Assign timestamp and duration to the buffer.
615   output_buffer->get()->set_timestamp(
616       output_timestamp_helper_->GetTimestamp() - timestamp_offset_);
617   output_buffer->get()->set_duration(
618       output_timestamp_helper_->GetFrameDuration(frames_to_output));
619   output_timestamp_helper_->AddFrames(frames_decoded);
620 
621   // Discard the buffer to indicate we need more data.
622   if (!frames_to_output)
623     *output_buffer = NULL;
624 
625   return true;
626 }
627 
628 }  // namespace media
629