| 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_video_decoder.h" | 5 #include "media/filters/ffmpeg_video_decoder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 11 #include "media/base/demuxer_stream.h" | 12 #include "media/base/demuxer_stream.h" |
| 12 #include "media/base/filter_host.h" | 13 #include "media/base/filter_host.h" |
| 13 #include "media/base/limits.h" | 14 #include "media/base/limits.h" |
| 14 #include "media/base/media_switches.h" | 15 #include "media/base/media_switches.h" |
| 15 #include "media/base/pipeline.h" | 16 #include "media/base/pipeline.h" |
| 16 #include "media/base/video_decoder_config.h" | 17 #include "media/base/video_decoder_config.h" |
| 17 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 DCHECK(!read_cb.is_null()); | 206 DCHECK(!read_cb.is_null()); |
| 206 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | 207 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; |
| 207 | 208 |
| 208 // This can happen during shutdown after Stop() has been called. | 209 // This can happen during shutdown after Stop() has been called. |
| 209 if (state_ == kUninitialized) { | 210 if (state_ == kUninitialized) { |
| 210 return; | 211 return; |
| 211 } | 212 } |
| 212 | 213 |
| 213 // Return empty frames if decoding has finished. | 214 // Return empty frames if decoding has finished. |
| 214 if (state_ == kDecodeFinished) { | 215 if (state_ == kDecodeFinished) { |
| 215 read_cb.Run(VideoFrame::CreateEmptyFrame()); | 216 read_cb.Run(VideoFrame::CreateEmptyFrame(), kOk); |
| 216 return; | 217 return; |
| 217 } | 218 } |
| 218 | 219 |
| 219 read_cb_ = read_cb; | 220 read_cb_ = read_cb; |
| 220 ReadFromDemuxerStream(); | 221 ReadFromDemuxerStream(); |
| 221 } | 222 } |
| 222 | 223 |
| 223 | 224 |
| 224 void FFmpegVideoDecoder::ReadFromDemuxerStream() { | 225 void FFmpegVideoDecoder::ReadFromDemuxerStream() { |
| 225 DCHECK_NE(state_, kUninitialized); | 226 DCHECK_NE(state_, kUninitialized); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 // Transition to kFlushCodec on the first end of stream buffer. | 282 // Transition to kFlushCodec on the first end of stream buffer. |
| 282 if (state_ == kNormal && buffer->IsEndOfStream()) { | 283 if (state_ == kNormal && buffer->IsEndOfStream()) { |
| 283 state_ = kFlushCodec; | 284 state_ = kFlushCodec; |
| 284 } | 285 } |
| 285 | 286 |
| 286 scoped_refptr<Buffer> unencrypted_buffer = buffer; | 287 scoped_refptr<Buffer> unencrypted_buffer = buffer; |
| 287 if (buffer->GetDecryptConfig() && buffer->GetDataSize()) { | 288 if (buffer->GetDecryptConfig() && buffer->GetDataSize()) { |
| 288 unencrypted_buffer = decryptor_.Decrypt(buffer); | 289 unencrypted_buffer = decryptor_.Decrypt(buffer); |
| 289 if (!unencrypted_buffer || !unencrypted_buffer->GetDataSize()) { | 290 if (!unencrypted_buffer || !unencrypted_buffer->GetDataSize()) { |
| 290 state_ = kDecodeFinished; | 291 state_ = kDecodeFinished; |
| 291 DeliverFrame(VideoFrame::CreateEmptyFrame()); | 292 base::ResetAndReturn(&read_cb_).Run(NULL, kDecryptError); |
| 292 host()->SetError(PIPELINE_ERROR_DECRYPT); | |
| 293 return; | 293 return; |
| 294 } | 294 } |
| 295 } | 295 } |
| 296 | 296 |
| 297 scoped_refptr<VideoFrame> video_frame; | 297 scoped_refptr<VideoFrame> video_frame; |
| 298 if (!Decode(unencrypted_buffer, &video_frame)) { | 298 if (!Decode(unencrypted_buffer, &video_frame)) { |
| 299 state_ = kDecodeFinished; | 299 state_ = kDecodeFinished; |
| 300 DeliverFrame(VideoFrame::CreateEmptyFrame()); | 300 base::ResetAndReturn(&read_cb_).Run(NULL, kDecodeError); |
| 301 host()->SetError(PIPELINE_ERROR_DECODE); | |
| 302 return; | 301 return; |
| 303 } | 302 } |
| 304 | 303 |
| 305 // Any successful decode counts! | 304 // Any successful decode counts! |
| 306 if (buffer->GetDataSize()) { | 305 if (buffer->GetDataSize()) { |
| 307 PipelineStatistics statistics; | 306 PipelineStatistics statistics; |
| 308 statistics.video_bytes_decoded = buffer->GetDataSize(); | 307 statistics.video_bytes_decoded = buffer->GetDataSize(); |
| 309 statistics_cb_.Run(statistics); | 308 statistics_cb_.Run(statistics); |
| 310 } | 309 } |
| 311 | 310 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 CopyYPlane(av_frame_->data[0], av_frame_->linesize[0], y_rows, *video_frame); | 415 CopyYPlane(av_frame_->data[0], av_frame_->linesize[0], y_rows, *video_frame); |
| 417 CopyUPlane(av_frame_->data[1], av_frame_->linesize[1], uv_rows, *video_frame); | 416 CopyUPlane(av_frame_->data[1], av_frame_->linesize[1], uv_rows, *video_frame); |
| 418 CopyVPlane(av_frame_->data[2], av_frame_->linesize[2], uv_rows, *video_frame); | 417 CopyVPlane(av_frame_->data[2], av_frame_->linesize[2], uv_rows, *video_frame); |
| 419 | 418 |
| 420 return true; | 419 return true; |
| 421 } | 420 } |
| 422 | 421 |
| 423 void FFmpegVideoDecoder::DeliverFrame( | 422 void FFmpegVideoDecoder::DeliverFrame( |
| 424 const scoped_refptr<VideoFrame>& video_frame) { | 423 const scoped_refptr<VideoFrame>& video_frame) { |
| 425 // Reset the callback before running to protect against reentrancy. | 424 // Reset the callback before running to protect against reentrancy. |
| 426 ReadCB read_cb = read_cb_; | 425 base::ResetAndReturn(&read_cb_).Run(video_frame, kOk); |
| 427 read_cb_.Reset(); | |
| 428 read_cb.Run(video_frame); | |
| 429 } | 426 } |
| 430 | 427 |
| 431 void FFmpegVideoDecoder::ReleaseFFmpegResources() { | 428 void FFmpegVideoDecoder::ReleaseFFmpegResources() { |
| 432 if (codec_context_) { | 429 if (codec_context_) { |
| 433 av_free(codec_context_->extradata); | 430 av_free(codec_context_->extradata); |
| 434 avcodec_close(codec_context_); | 431 avcodec_close(codec_context_); |
| 435 av_free(codec_context_); | 432 av_free(codec_context_); |
| 436 codec_context_ = NULL; | 433 codec_context_ = NULL; |
| 437 } | 434 } |
| 438 if (av_frame_) { | 435 if (av_frame_) { |
| 439 av_free(av_frame_); | 436 av_free(av_frame_); |
| 440 av_frame_ = NULL; | 437 av_frame_ = NULL; |
| 441 } | 438 } |
| 442 } | 439 } |
| 443 | 440 |
| 444 scoped_refptr<VideoFrame> FFmpegVideoDecoder::AllocateVideoFrame() { | 441 scoped_refptr<VideoFrame> FFmpegVideoDecoder::AllocateVideoFrame() { |
| 445 VideoFrame::Format format = PixelFormatToVideoFormat(codec_context_->pix_fmt); | 442 VideoFrame::Format format = PixelFormatToVideoFormat(codec_context_->pix_fmt); |
| 446 size_t width = codec_context_->width; | 443 size_t width = codec_context_->width; |
| 447 size_t height = codec_context_->height; | 444 size_t height = codec_context_->height; |
| 448 | 445 |
| 449 return VideoFrame::CreateFrame(format, width, height, | 446 return VideoFrame::CreateFrame(format, width, height, |
| 450 kNoTimestamp(), kNoTimestamp()); | 447 kNoTimestamp(), kNoTimestamp()); |
| 451 } | 448 } |
| 452 | 449 |
| 453 } // namespace media | 450 } // namespace media |
| OLD | NEW |