Chromium Code Reviews| Index: media/filters/vpx_video_decoder.cc |
| diff --git a/media/filters/vpx_video_decoder.cc b/media/filters/vpx_video_decoder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e2ab95023a066ea563d9229b7f968436e0a8054a |
| --- /dev/null |
| +++ b/media/filters/vpx_video_decoder.cc |
| @@ -0,0 +1,423 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/filters/vpx_video_decoder.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback_helpers.h" |
| +#include "base/command_line.h" |
| +#include "base/debug/trace_event.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "base/string_number_conversions.h" |
| +#include "media/base/bind_to_loop.h" |
| +#include "media/base/decoder_buffer.h" |
| +#include "media/base/decryptor.h" |
| +#include "media/base/demuxer_stream.h" |
| +#include "media/base/media_switches.h" |
| +#include "media/base/pipeline.h" |
| +#include "media/base/video_decoder_config.h" |
| +#include "media/base/video_frame.h" |
| +#include "media/base/video_util.h" |
| + |
| +// Include libvpx header files. |
| +// VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide |
| +// backwards compatibility for legacy applications using the library. |
| +#define VPX_CODEC_DISABLE_COMPAT 1 |
| +extern "C" { |
| +#include "third_party/libvpx/libvpx.h" |
| +} |
| + |
| +namespace media { |
| + |
| +#define BIND_TO_LOOP(function) \ |
|
scherkus (not reviewing)
2012/12/14 21:32:41
use BindToCurrentLoop() instead of this macro
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + media::BindToLoop(message_loop_, base::Bind(function, this)) |
| + |
| +// Always try to use three threads for video decoding. There is little reason |
| +// not to since current day CPUs tend to be multi-core and we measured |
| +// performance benefits on older machines such as P4s with hyperthreading. |
| +static const int kDecodeThreads = 2; |
| +static const int kMaxDecodeThreads = 16; |
| + |
| +// Returns the number of threads. |
| +static int GetThreadCount() { |
|
scherkus (not reviewing)
2012/12/14 21:32:41
you don't use this function
Tom Finegan
2012/12/15 06:13:30
Thanks, wasn't intentional. :)
|
| + // Refer to http://crbug.com/93932 for tsan suppressions on decoding. |
| + int decode_threads = kDecodeThreads; |
| + |
| + const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| + std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads)); |
| + if (threads.empty() || !base::StringToInt(threads, &decode_threads)) |
| + return decode_threads; |
| + |
| + decode_threads = std::max(decode_threads, 0); |
| + decode_threads = std::min(decode_threads, kMaxDecodeThreads); |
| + return decode_threads; |
| +} |
| + |
| +VpxVideoDecoder::VpxVideoDecoder( |
| + const scoped_refptr<base::MessageLoopProxy>& message_loop) |
| + : message_loop_(message_loop), |
| + state_(kUninitialized), |
| + vpx_codec_(NULL) { |
| +} |
| + |
| +VpxVideoDecoder::~VpxVideoDecoder() { |
| + DCHECK_EQ(kUninitialized, state_); |
| + CloseDecoder(); |
| +} |
| + |
| +void VpxVideoDecoder::Initialize( |
| + const scoped_refptr<DemuxerStream>& stream, |
| + const PipelineStatusCB& status_cb, |
| + const StatisticsCB& statistics_cb) { |
| + if (!message_loop_->BelongsToCurrentThread()) { |
|
scherkus (not reviewing)
2012/12/14 21:32:41
replace this with a DCHECK() for current thread
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + message_loop_->PostTask(FROM_HERE, base::Bind( |
| + &VpxVideoDecoder::Initialize, this, |
| + stream, status_cb, statistics_cb)); |
| + return; |
| + } |
| + |
| + DCHECK(!demuxer_stream_) << "Already initialized."; |
| + |
| + if (!stream) { |
| + status_cb.Run(PIPELINE_ERROR_DECODE); |
| + return; |
| + } |
| + |
| + demuxer_stream_ = stream; |
| + statistics_cb_ = statistics_cb; |
| + |
| + if (!ConfigureDecoder()) { |
| + status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); |
| + return; |
| + } |
| + |
| + // Success! |
| + state_ = kNormal; |
| + status_cb.Run(PIPELINE_OK); |
| +} |
| + |
| +bool VpxVideoDecoder::ConfigureDecoder() { |
| + const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); |
| + if (!config.IsValidConfig()) { |
| + DLOG(ERROR) << "Invalid video stream config: " |
| + << config.AsHumanReadableString(); |
| + return false; |
| + } |
| + |
| + if (config.codec() != kCodecVP9) |
| + return false; |
| + |
| + CloseDecoder(); |
| + |
| + vpx_codec_ = new vpx_codec_ctx(); |
| + vpx_codec_dec_cfg_t vpx_config = {0}; |
| + vpx_config.w = config.coded_size().width(); |
| + vpx_config.h = config.coded_size().height(); |
| + vpx_config.threads = kDecodeThreads; |
| + |
| + vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_, |
| + vpx_codec_vp8_dx(), |
| + &vpx_config, |
| + 0); |
| + if (status != VPX_CODEC_OK) { |
| + LOG(ERROR) << "ConfigureDecoder(): vpx_codec_dec_init failed, status=" |
| + << status; |
| + delete vpx_codec_; |
| + vpx_codec_ = NULL; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void VpxVideoDecoder::CloseDecoder() { |
| + if (vpx_codec_) { |
| + vpx_codec_destroy(vpx_codec_); |
| + delete vpx_codec_; |
| + vpx_codec_ = NULL; |
| + } |
| +} |
| + |
| +void VpxVideoDecoder::Read(const ReadCB& read_cb) { |
| + // Complete operation asynchronously on different stack of execution as per |
|
scherkus (not reviewing)
2012/12/14 21:32:41
replace this with a DCHECK() for current thread an
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + // the API contract of VideoDecoder::Read() |
| + message_loop_->PostTask(FROM_HERE, base::Bind( |
| + &VpxVideoDecoder::DoRead, this, read_cb)); |
| +} |
| + |
| +void VpxVideoDecoder::Reset(const base::Closure& closure) { |
| + if (!message_loop_->BelongsToCurrentThread()) { |
|
scherkus (not reviewing)
2012/12/14 21:32:41
replace this with a DCHECK() for current thread
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + message_loop_->PostTask(FROM_HERE, base::Bind( |
| + &VpxVideoDecoder::Reset, this, closure)); |
| + return; |
| + } |
| + |
| + DCHECK(reset_cb_.is_null()); |
| + reset_cb_ = closure; |
|
scherkus (not reviewing)
2012/12/14 21:32:41
use BindToCurrentLoop() on this closure
see ffmpe
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + |
| + // Defer the reset if a read is pending. |
| + if (!read_cb_.is_null()) |
| + return; |
| + |
| + DoReset(); |
| +} |
| + |
| +void VpxVideoDecoder::Stop(const base::Closure& closure) { |
| + if (!message_loop_->BelongsToCurrentThread()) { |
|
scherkus (not reviewing)
2012/12/14 21:32:41
replace this with a DCHECK() for current thread
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + message_loop_->PostTask(FROM_HERE, base::Bind( |
| + &VpxVideoDecoder::Stop, this, closure)); |
| + return; |
| + } |
| + |
| + if (state_ == kUninitialized) { |
| + closure.Run(); |
| + return; |
| + } |
| + |
| + if (!read_cb_.is_null()) |
| + base::ResetAndReturn(&read_cb_).Run(kOk, NULL); |
| + |
| + state_ = kUninitialized; |
| + closure.Run(); |
| +} |
| + |
| +void VpxVideoDecoder::DoRead(const ReadCB& read_cb) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(!read_cb.is_null()); |
| + CHECK_NE(state_, kUninitialized); |
| + CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; |
| + |
| + // Return empty frames if decoding has finished. |
| + if (state_ == kDecodeFinished) { |
| + read_cb.Run(kOk, VideoFrame::CreateEmptyFrame()); |
| + return; |
| + } |
| + |
| + read_cb_ = read_cb; |
| + ReadFromDemuxerStream(); |
| +} |
| + |
| +void VpxVideoDecoder::ReadFromDemuxerStream() { |
| + DCHECK_NE(state_, kUninitialized); |
| + DCHECK_NE(state_, kDecodeFinished); |
| + DCHECK(!read_cb_.is_null()); |
| + |
| + demuxer_stream_->Read(base::Bind( |
| + &VpxVideoDecoder::DoDecryptOrDecodeBuffer, this)); |
| +} |
| + |
| +void VpxVideoDecoder::DoDecryptOrDecodeBuffer( |
| + DemuxerStream::Status status, |
| + const scoped_refptr<DecoderBuffer>& buffer) { |
| + if (!message_loop_->BelongsToCurrentThread()) { |
|
scherkus (not reviewing)
2012/12/14 21:32:41
replace with check for correct thread
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + message_loop_->PostTask(FROM_HERE, base::Bind( |
| + &VpxVideoDecoder::DoDecryptOrDecodeBuffer, this, status, buffer)); |
| + return; |
| + } |
| + |
| + DCHECK_NE(state_, kDecodeFinished); |
| + DCHECK_EQ(status != DemuxerStream::kOk, !buffer) << status; |
| + |
| + if (state_ == kUninitialized) |
| + return; |
| + |
| + DCHECK(!read_cb_.is_null()); |
| + |
| + if (!reset_cb_.is_null()) { |
| + base::ResetAndReturn(&read_cb_).Run(kOk, NULL); |
| + DoReset(); |
| + return; |
| + } |
| + |
| + if (status == DemuxerStream::kAborted) { |
| + base::ResetAndReturn(&read_cb_).Run(kOk, NULL); |
| + return; |
| + } |
| + |
| + if (status == DemuxerStream::kConfigChanged) { |
| + if (!ConfigureDecoder()) { |
| + base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); |
| + return; |
| + } |
| + |
| + ReadFromDemuxerStream(); |
| + return; |
| + } |
| + |
| + DCHECK_EQ(status, DemuxerStream::kOk); |
| + DecodeBuffer(buffer); |
| +} |
| + |
| +void VpxVideoDecoder::DecodeBuffer( |
| + const scoped_refptr<DecoderBuffer>& buffer) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK_NE(state_, kUninitialized); |
| + DCHECK_NE(state_, kDecodeFinished); |
| + DCHECK(reset_cb_.is_null()); |
| + DCHECK(!read_cb_.is_null()); |
| + DCHECK(buffer); |
| + |
| + // During decode, because reads are issued asynchronously, it is possible to |
|
scherkus (not reviewing)
2012/12/14 21:32:41
this comment is copied from FFmpegVideoDecoder
ho
Tom Finegan
2012/12/15 06:13:30
It doesn't. Removed the comment.
|
| + // receive multiple end of stream buffers since each read is acked. When the |
| + // first end of stream buffer is read, libvpx may still have frames queued |
| + // up in the decoder so we need to go through the decode loop until it stops |
| + // giving sensible data. After that, the decoder should output empty |
| + // frames. There are three states the decoder can be in: |
| + // |
| + // kNormal: This is the starting state. Buffers are decoded. Decode errors |
| + // are discarded. |
| + // kFlushCodec: There isn't any more input data. Call |vpx_codec_decode()| |
| + // until no more data is returned to flush out any remaining |
| + // frames. The input buffer is ignored at this point. |
| + // kDecodeFinished: All calls return empty frames. |
| + // |
| + // These are the possible state transitions. |
| + // |
| + // kNormal -> kFlushCodec: |
| + // When buffer->IsEndOfStream() is first true. |
| + // kNormal -> kDecodeFinished: |
| + // A decoding error occurs and decoding needs to stop. |
| + // kFlushCodec -> kDecodeFinished: |
| + // When |vpx_codec_decode()| returns no data or errors out. |
| + // (any state) -> kNormal: |
| + // Any time Reset() is called. |
| + |
| + // Transition to kFlushCodec on the first end of stream buffer. |
| + if (state_ == kNormal && buffer->IsEndOfStream()) { |
| + state_ = kFlushCodec; |
| + } |
| + |
| + scoped_refptr<VideoFrame> video_frame; |
| + if (!Decode(buffer, &video_frame)) { |
| + state_ = kDecodeFinished; |
| + base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); |
| + return; |
| + } |
| + |
| + // Any successful decode counts! |
| + if (buffer->GetDataSize()) { |
| + PipelineStatistics statistics; |
| + statistics.video_bytes_decoded = buffer->GetDataSize(); |
| + statistics_cb_.Run(statistics); |
| + } |
| + |
| + // If we didn't get a frame then we've either completely finished decoding or |
| + // we need more data. |
| + if (!video_frame) { |
| + if (state_ == kFlushCodec) { |
|
scherkus (not reviewing)
2012/12/14 21:32:41
does vpx need to be flushed to get the last frames
Tom Finegan
2012/12/15 06:13:30
No. Removed this... I think I'm handling things co
|
| + state_ = kDecodeFinished; |
| + base::ResetAndReturn(&read_cb_).Run(kOk, VideoFrame::CreateEmptyFrame()); |
| + return; |
| + } |
| + |
| + ReadFromDemuxerStream(); |
| + return; |
| + } |
| + |
| + base::ResetAndReturn(&read_cb_).Run(kOk, video_frame); |
| +} |
| + |
| +bool VpxVideoDecoder::Decode( |
| + const scoped_refptr<DecoderBuffer>& buffer, |
| + scoped_refptr<VideoFrame>* video_frame) { |
| + DCHECK(video_frame); |
| + |
| + // Pass |buffer| to libvpx. |
| + int64 timestamp = buffer->GetTimestamp().InMicroseconds(); |
| + void* user_priv = reinterpret_cast<void*>(×tamp); |
| + vpx_codec_err_t status = vpx_codec_decode(vpx_codec_, |
| + buffer->GetData(), |
| + buffer->GetDataSize(), |
| + user_priv, |
| + 0); |
| + if (status != VPX_CODEC_OK) { |
| + LOG(ERROR) << "Decode(): vpx_codec_decode failed, status=" << status; |
|
scherkus (not reviewing)
2012/12/14 21:32:41
remove Decode() from LOG() here + below
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + return false; |
| + } |
| + |
| + // Gets pointer to decoded data. |
| + vpx_codec_iter_t iter = NULL; |
| + const vpx_image_t* vpx_image = vpx_codec_get_frame(vpx_codec_, &iter); |
| + if (!vpx_image) { |
| + *video_frame = NULL; |
| + return true; |
| + } |
| + |
| + if (vpx_image->user_priv != reinterpret_cast<void*>(×tamp)) { |
| + LOG(ERROR) << "Decode(): invalid output timestamp."; |
| + return false; |
| + } |
| + |
| + if (!CopyVpxImageTo(vpx_image, video_frame)) { |
| + LOG(ERROR) << "Decode(): could not copy vpx image to output buffer."; |
| + return false; |
| + } |
| + |
| + (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); |
| + |
| + return true; |
| +} |
| + |
| +void VpxVideoDecoder::DoReset() { |
| + DCHECK(read_cb_.is_null()); |
| + |
| + state_ = kNormal; |
| + reset_cb_.Run(); |
| + reset_cb_.Reset(); |
| +} |
| + |
| +bool VpxVideoDecoder::CopyVpxImageTo( |
| + const vpx_image* vpx_image, |
| + scoped_refptr<VideoFrame>* video_frame) { |
| + DCHECK(vpx_image); |
|
scherkus (not reviewing)
2012/12/14 21:32:41
don't dcheck then handle it gracefully
if it's no
Tom Finegan
2012/12/15 06:13:30
Made method void, removed graceful error handling,
|
| + if (!vpx_image) |
| + return false; |
| + |
| + DCHECK_EQ(vpx_image->d_w % 2, 0U); |
|
scherkus (not reviewing)
2012/12/14 21:32:41
if these DCHECKs fail in release mode, will we ope
Tom Finegan
2012/12/15 06:13:30
Used CHECKs.
|
| + DCHECK_EQ(vpx_image->d_h % 2, 0U); |
| + DCHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || |
| + vpx_image->fmt == VPX_IMG_FMT_YV12); |
| + |
| + gfx::Size size(vpx_image->d_w, vpx_image->d_h); |
| + gfx::Size natural_size = |
| + demuxer_stream_->video_decoder_config().natural_size(); |
| + |
| + const VideoFrame::Format format = VideoFrame::YV12; |
| + if (!VideoFrame::IsValidConfig(format, |
|
scherkus (not reviewing)
2012/12/14 21:32:41
is it actually valid for this to fail?
wouldn't i
Tom Finegan
2012/12/15 06:13:30
Yeah, I think you're right. Removed.
|
| + size, |
| + gfx::Rect(size), |
| + natural_size)) { |
| + LOG(ERROR) << "CopyVpxImageTo(): invalid video frame."; |
| + return false; |
| + } |
| + |
| + *video_frame = VideoFrame::CreateFrame(format, |
|
scherkus (not reviewing)
2012/12/14 21:32:41
OOC is there a way to use custom allocators with l
Tom Finegan
2012/12/15 06:13:30
No, not at present. :(
|
| + size, |
| + gfx::Rect(size), |
| + natural_size, |
| + kNoTimestamp()); |
| + if (!video_frame) { |
|
scherkus (not reviewing)
2012/12/14 21:32:41
this always returns true -- you do'nt need this ch
Tom Finegan
2012/12/15 06:13:30
Done.
|
| + LOG(ERROR) << "CopyVpxImageTo(): cannot create output video frame."; |
| + return false; |
| + } |
| + |
| + CopyYPlane(vpx_image->planes[VPX_PLANE_Y], |
| + vpx_image->stride[VPX_PLANE_Y], |
| + vpx_image->d_h, |
| + *video_frame); |
| + CopyUPlane(vpx_image->planes[VPX_PLANE_U], |
| + vpx_image->stride[VPX_PLANE_U], |
| + vpx_image->d_h / 2, |
| + *video_frame); |
| + CopyVPlane(vpx_image->planes[VPX_PLANE_V], |
| + vpx_image->stride[VPX_PLANE_V], |
| + vpx_image->d_h / 2, |
| + *video_frame); |
| + |
| + return true; |
| +} |
| + |
| +} // namespace media |