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/ffmpeg_audio_decoder.h" | 5 #include "media/filters/ffmpeg_audio_decoder.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "media/base/audio_buffer.h" | 9 #include "media/base/audio_buffer.h" |
10 #include "media/base/audio_bus.h" | 10 #include "media/base/audio_bus.h" |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 void FFmpegAudioDecoder::DecodeBuffer( | 206 void FFmpegAudioDecoder::DecodeBuffer( |
207 const scoped_refptr<DecoderBuffer>& buffer, | 207 const scoped_refptr<DecoderBuffer>& buffer, |
208 const DecodeCB& decode_cb) { | 208 const DecodeCB& decode_cb) { |
209 DCHECK(task_runner_->BelongsToCurrentThread()); | 209 DCHECK(task_runner_->BelongsToCurrentThread()); |
210 DCHECK_NE(state_, kUninitialized); | 210 DCHECK_NE(state_, kUninitialized); |
211 DCHECK_NE(state_, kDecodeFinished); | 211 DCHECK_NE(state_, kDecodeFinished); |
212 DCHECK_NE(state_, kError); | 212 DCHECK_NE(state_, kError); |
213 | 213 |
214 DCHECK(buffer); | 214 DCHECK(buffer); |
215 | 215 |
216 // During decode, because reads are issued asynchronously, it is possible to | |
217 // receive multiple end of stream buffers since each decode is acked. When the | |
218 // first end of stream buffer is read, FFmpeg may still have frames queued | |
219 // up in the decoder so we need to go through the decode loop until it stops | |
220 // giving sensible data. After that, the decoder should output empty | |
221 // frames. There are three states the decoder can be in: | |
222 // | |
223 // kNormal: This is the starting state. Buffers are decoded. Decode errors | |
224 // are discarded. | |
225 // kFlushCodec: There isn't any more input data. Call avcodec_decode_audio4 | |
226 // until no more data is returned to flush out remaining | |
227 // frames. The input buffer is ignored at this point. | |
228 // kDecodeFinished: All calls return empty frames. | |
229 // kError: Unexpected error happened. | |
230 // | |
231 // These are the possible state transitions. | |
232 // | |
233 // kNormal -> kFlushCodec: | |
234 // When buffer->end_of_stream() is first true. | |
235 // kNormal -> kError: | |
236 // A decoding error occurs and decoding needs to stop. | |
237 // kFlushCodec -> kDecodeFinished: | |
238 // When avcodec_decode_audio4() returns 0 data. | |
239 // kFlushCodec -> kError: | |
240 // When avcodec_decode_audio4() errors out. | |
241 // (any state) -> kNormal: | |
242 // Any time Reset() is called. | |
243 | |
244 // Make sure we are notified if http://crbug.com/49709 returns. Issue also | 216 // Make sure we are notified if http://crbug.com/49709 returns. Issue also |
245 // occurs with some damaged files. | 217 // occurs with some damaged files. |
246 if (!buffer->end_of_stream() && buffer->timestamp() == kNoTimestamp()) { | 218 if (!buffer->end_of_stream() && buffer->timestamp() == kNoTimestamp()) { |
247 DVLOG(1) << "Received a buffer without timestamps!"; | 219 DVLOG(1) << "Received a buffer without timestamps!"; |
248 decode_cb.Run(kDecodeError); | 220 decode_cb.Run(kDecodeError); |
249 return; | 221 return; |
250 } | 222 } |
251 | 223 |
252 if (!buffer->end_of_stream() && !discard_helper_->initialized() && | 224 if (!buffer->end_of_stream() && !discard_helper_->initialized() && |
253 codec_context_->codec_id == AV_CODEC_ID_VORBIS && | 225 codec_context_->codec_id == AV_CODEC_ID_VORBIS && |
254 buffer->timestamp() < base::TimeDelta()) { | 226 buffer->timestamp() < base::TimeDelta()) { |
255 // Dropping frames for negative timestamps as outlined in section A.2 | 227 // Dropping frames for negative timestamps as outlined in section A.2 |
256 // in the Vorbis spec. http://xiph.org/vorbis/doc/Vorbis_I_spec.html | 228 // in the Vorbis spec. http://xiph.org/vorbis/doc/Vorbis_I_spec.html |
257 const int discard_frames = | 229 const int discard_frames = |
258 discard_helper_->TimeDeltaToFrames(-buffer->timestamp()); | 230 discard_helper_->TimeDeltaToFrames(-buffer->timestamp()); |
259 discard_helper_->Reset(discard_frames); | 231 discard_helper_->Reset(discard_frames); |
260 } | 232 } |
261 | 233 |
262 if (!FFmpegDecode(buffer)) { | 234 if (!FFmpegDecode(buffer)) { |
263 state_ = kError; | 235 state_ = kError; |
264 decode_cb.Run(kDecodeError); | 236 decode_cb.Run(kDecodeError); |
265 return; | 237 return; |
266 } | 238 } |
267 | 239 |
268 if (buffer->end_of_stream()) { | 240 if (buffer->end_of_stream()) |
269 state_ = kDecodeFinished; | 241 state_ = kDecodeFinished; |
270 output_cb_.Run(AudioBuffer::CreateEOSBuffer()); | |
271 } | |
272 | 242 |
273 decode_cb.Run(kOk); | 243 decode_cb.Run(kOk); |
274 } | 244 } |
275 | 245 |
276 bool FFmpegAudioDecoder::FFmpegDecode( | 246 bool FFmpegAudioDecoder::FFmpegDecode( |
277 const scoped_refptr<DecoderBuffer>& buffer) { | 247 const scoped_refptr<DecoderBuffer>& buffer) { |
278 AVPacket packet; | 248 AVPacket packet; |
279 av_init_packet(&packet); | 249 av_init_packet(&packet); |
280 if (buffer->end_of_stream()) { | 250 if (buffer->end_of_stream()) { |
281 packet.data = NULL; | 251 packet.data = NULL; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 DCHECK_GE(unread_frames, 0); | 320 DCHECK_GE(unread_frames, 0); |
351 if (unread_frames > 0) | 321 if (unread_frames > 0) |
352 output->TrimEnd(unread_frames); | 322 output->TrimEnd(unread_frames); |
353 av_frame_unref(av_frame_.get()); | 323 av_frame_unref(av_frame_.get()); |
354 } | 324 } |
355 | 325 |
356 // WARNING: |av_frame_| no longer has valid data at this point. | 326 // WARNING: |av_frame_| no longer has valid data at this point. |
357 const int decoded_frames = frame_decoded ? output->frame_count() : 0; | 327 const int decoded_frames = frame_decoded ? output->frame_count() : 0; |
358 if (IsEndOfStream(result, decoded_frames, buffer)) { | 328 if (IsEndOfStream(result, decoded_frames, buffer)) { |
359 DCHECK_EQ(packet.size, 0); | 329 DCHECK_EQ(packet.size, 0); |
360 output_cb_.Run(AudioBuffer::CreateEOSBuffer()); | |
361 } else if (discard_helper_->ProcessBuffers(buffer, output)) { | 330 } else if (discard_helper_->ProcessBuffers(buffer, output)) { |
362 output_cb_.Run(output); | 331 output_cb_.Run(output); |
363 } | 332 } |
364 } while (packet.size > 0); | 333 } while (packet.size > 0); |
365 | 334 |
366 return true; | 335 return true; |
367 } | 336 } |
368 | 337 |
369 void FFmpegAudioDecoder::ReleaseFFmpegResources() { | 338 void FFmpegAudioDecoder::ReleaseFFmpegResources() { |
370 codec_context_.reset(); | 339 codec_context_.reset(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 | 394 |
426 ResetTimestampState(); | 395 ResetTimestampState(); |
427 return true; | 396 return true; |
428 } | 397 } |
429 | 398 |
430 void FFmpegAudioDecoder::ResetTimestampState() { | 399 void FFmpegAudioDecoder::ResetTimestampState() { |
431 discard_helper_->Reset(config_.codec_delay()); | 400 discard_helper_->Reset(config_.codec_delay()); |
432 } | 401 } |
433 | 402 |
434 } // namespace media | 403 } // namespace media |
OLD | NEW |