Index: content/common/gpu/media/dxva_video_decode_accelerator.cc |
diff --git a/content/common/gpu/media/dxva_video_decode_accelerator.cc b/content/common/gpu/media/dxva_video_decode_accelerator.cc |
index fffceb706fa9f1746844ccc2f7974c98684b6831..f68ae249323254e88938d1d8f389383a10acbc2e 100644 |
--- a/content/common/gpu/media/dxva_video_decode_accelerator.cc |
+++ b/content/common/gpu/media/dxva_video_decode_accelerator.cc |
@@ -338,7 +338,6 @@ bool DXVAVideoDecodeAccelerator::DXVAPictureBuffer:: |
// the texture. Flush it once here though. |
hr = decoder.query_->Issue(D3DISSUE_END); |
RETURN_ON_HR_FAILURE(hr, "Failed to issue END", false); |
- |
// The DXVA decoder has its own device which it uses for decoding. ANGLE |
// has its own device which we don't have access to. |
// The above code attempts to copy the decoded picture into a surface |
@@ -431,7 +430,8 @@ DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator( |
pictures_requested_(false), |
inputs_before_decode_(0), |
make_context_current_(make_context_current), |
- weak_this_factory_(this) { |
+ weak_this_factory_(this), |
+ decoder_thread_("DXVAVideoDecoderThread") { |
memset(&input_stream_info_, 0, sizeof(input_stream_info_)); |
memset(&output_stream_info_, 0, sizeof(output_stream_info_)); |
} |
@@ -454,11 +454,9 @@ bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, |
HMODULE mfplat_dll = ::LoadLibrary(L"MFPlat.dll"); |
RETURN_ON_FAILURE(mfplat_dll, "MFPlat.dll is required for decoding", false); |
- // TODO(ananta) |
- // H264PROFILE_HIGH video decoding is janky at times. Needs more |
- // investigation. http://crbug.com/426707 |
if (profile != media::H264PROFILE_BASELINE && |
- profile != media::H264PROFILE_MAIN) { |
+ profile != media::H264PROFILE_MAIN && |
+ profile != media::H264PROFILE_HIGH) { |
RETURN_AND_NOTIFY_ON_FAILURE(false, |
"Unsupported h264 profile", PLATFORM_FAILURE, false); |
} |
@@ -469,8 +467,9 @@ bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, |
PLATFORM_FAILURE, |
false); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kUninitialized), |
- "Initialize: invalid state: " << state_, ILLEGAL_STATE, false); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kUninitialized), |
+ "Initialize: invalid state: " << state, ILLEGAL_STATE, false); |
HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL); |
RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "MFStartup failed.", PLATFORM_FAILURE, |
@@ -497,7 +496,13 @@ bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, |
"Send MFT_MESSAGE_NOTIFY_START_OF_STREAM notification failed", |
PLATFORM_FAILURE, false); |
- state_ = kNormal; |
+ SetState(kNormal); |
+ |
+ main_thread_task_runner_ = base::MessageLoop::current()->task_runner(); |
+ |
+ decoder_thread_.init_com_with_mta(false); |
+ decoder_thread_.Start(); |
+ decoder_thread_task_runner_ = decoder_thread_.task_runner(); |
return true; |
} |
@@ -505,9 +510,10 @@ void DXVAVideoDecodeAccelerator::Decode( |
const media::BitstreamBuffer& bitstream_buffer) { |
DCHECK(CalledOnValidThread()); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped || |
- state_ == kFlushing), |
- "Invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped || |
+ state == kFlushing), |
+ "Invalid state: " << state, ILLEGAL_STATE,); |
base::win::ScopedComPtr<IMFSample> sample; |
sample.Attach(CreateSampleFromInputBuffer(bitstream_buffer, |
@@ -518,16 +524,19 @@ void DXVAVideoDecodeAccelerator::Decode( |
RETURN_AND_NOTIFY_ON_HR_FAILURE(sample->SetSampleTime(bitstream_buffer.id()), |
"Failed to associate input buffer id with sample", PLATFORM_FAILURE,); |
- |
- DecodeInternal(sample); |
+ |
+ decoder_thread_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::DecodeInternal, |
+ base::Unretained(this), sample)); |
} |
void DXVAVideoDecodeAccelerator::AssignPictureBuffers( |
const std::vector<media::PictureBuffer>& buffers) { |
DCHECK(CalledOnValidThread()); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), |
- "Invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), |
+ "Invalid state: " << state, ILLEGAL_STATE,); |
RETURN_AND_NOTIFY_ON_FAILURE((kNumPictureBuffers == buffers.size()), |
"Failed to provide requested picture buffers. (Got " << buffers.size() << |
", requested " << kNumPictureBuffers << ")", INVALID_ARGUMENT,); |
@@ -546,7 +555,8 @@ void DXVAVideoDecodeAccelerator::AssignPictureBuffers( |
DCHECK(inserted); |
} |
ProcessPendingSamples(); |
- if (state_ == kFlushing && pending_output_samples_.empty()) |
+ |
+ if (GetState() == kFlushing && pending_output_samples_.empty()) |
FlushInternal(); |
} |
@@ -554,8 +564,9 @@ void DXVAVideoDecodeAccelerator::ReusePictureBuffer( |
int32 picture_buffer_id) { |
DCHECK(CalledOnValidThread()); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), |
- "Invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), |
+ "Invalid state: " << state, ILLEGAL_STATE,); |
if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty()) |
return; |
@@ -580,7 +591,7 @@ void DXVAVideoDecodeAccelerator::ReusePictureBuffer( |
it->second->ReusePictureBuffer(); |
ProcessPendingSamples(); |
- if (state_ == kFlushing && pending_output_samples_.empty()) |
+ if (GetState() == kFlushing && pending_output_samples_.empty()) |
FlushInternal(); |
} |
@@ -589,10 +600,11 @@ void DXVAVideoDecodeAccelerator::Flush() { |
DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush"; |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), |
- "Unexpected decoder state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped), |
+ "Unexpected decoder state: " << state, ILLEGAL_STATE,); |
- state_ = kFlushing; |
+ SetState(kFlushing); |
RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0), |
"Failed to send drain message", PLATFORM_FAILURE,); |
@@ -604,28 +616,18 @@ void DXVAVideoDecodeAccelerator::Flush() { |
} |
void DXVAVideoDecodeAccelerator::Reset() { |
- DCHECK(CalledOnValidThread()); |
- |
DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset"; |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), |
- "Reset: invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped), |
+ "Reset: invalid state: " << state, ILLEGAL_STATE,); |
- state_ = kResetting; |
- |
- pending_output_samples_.clear(); |
- |
- NotifyInputBuffersDropped(); |
- |
- RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0), |
- "Reset: Failed to send message.", PLATFORM_FAILURE,); |
- |
- base::MessageLoop::current()->PostTask( |
- FROM_HERE, |
- base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone, |
- weak_this_factory_.GetWeakPtr())); |
- |
- state_ = DXVAVideoDecodeAccelerator::kNormal; |
+ if (decoder_thread_.thread_id() != ::GetCurrentThreadId()) { |
+ decoder_thread_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::ResetHelper, |
+ base::Unretained(this))); |
+ return; |
+ } |
} |
void DXVAVideoDecodeAccelerator::Destroy() { |
@@ -822,9 +824,10 @@ bool DXVAVideoDecodeAccelerator::GetStreamsInfoAndBufferReqs() { |
void DXVAVideoDecodeAccelerator::DoDecode() { |
// This function is also called from FlushInternal in a loop which could |
// result in the state transitioning to kStopped due to no decoded output. |
+ State state = GetState(); |
RETURN_AND_NOTIFY_ON_FAILURE( |
- (state_ == kNormal || state_ == kFlushing || |
- state_ == kStopped || state_ == kFlushingPendingInputBuffers), |
+ (state == kNormal || state == kFlushing || |
+ state == kStopped || state == kFlushingPendingInputBuffers), |
"DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,); |
MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0}; |
@@ -847,7 +850,7 @@ void DXVAVideoDecodeAccelerator::DoDecode() { |
// Decoder didn't let us set NV12 output format. Not sure as to why |
// this can happen. Give up in disgust. |
NOTREACHED() << "Failed to set decoder output media type to NV12"; |
- state_ = kStopped; |
+ SetState(kStopped); |
} else { |
DVLOG(1) << "Received output format change from the decoder." |
" Recursively invoking DoDecode"; |
@@ -856,8 +859,8 @@ void DXVAVideoDecodeAccelerator::DoDecode() { |
return; |
} else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) { |
// No more output from the decoder. Stop playback. |
- if (state_ != kFlushingPendingInputBuffers) |
- state_ = kStopped; |
+ if (GetState() != kFlushingPendingInputBuffers) |
+ SetState(kStopped); |
return; |
} else { |
NOTREACHED() << "Unhandled error in DoDecode()"; |
@@ -899,7 +902,10 @@ bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) { |
// If we have available picture buffers to copy the output data then use the |
// first one and then flag it as not being available for use. |
if (output_picture_buffers_.size()) { |
- ProcessPendingSamples(); |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::ProcessPendingSamples, |
+ weak_this_factory_.GetWeakPtr())); |
return true; |
} |
if (pictures_requested_) { |
@@ -915,7 +921,7 @@ bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) { |
RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false); |
// Go ahead and request picture buffers. |
- base::MessageLoop::current()->PostTask( |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, |
weak_this_factory_.GetWeakPtr(), |
@@ -973,7 +979,7 @@ void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { |
media::Picture output_picture(index->second->id(), |
sample_info.input_buffer_id, |
gfx::Rect(index->second->size())); |
- base::MessageLoop::current()->PostTask( |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady, |
weak_this_factory_.GetWeakPtr(), |
@@ -983,13 +989,10 @@ void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { |
pending_output_samples_.pop_front(); |
} |
} |
- |
- if (!pending_input_buffers_.empty() && pending_output_samples_.empty()) { |
- base::MessageLoop::current()->PostTask( |
- FROM_HERE, |
- base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, |
- weak_this_factory_.GetWeakPtr())); |
- } |
+ decoder_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, |
+ base::Unretained(this))); |
} |
void DXVAVideoDecodeAccelerator::StopOnError( |
@@ -1000,14 +1003,15 @@ void DXVAVideoDecodeAccelerator::StopOnError( |
client_->NotifyError(error); |
client_ = NULL; |
- if (state_ != kUninitialized) { |
+ if (GetState() != kUninitialized) { |
Invalidate(); |
} |
} |
void DXVAVideoDecodeAccelerator::Invalidate() { |
- if (state_ == kUninitialized) |
+ if (GetState() == kUninitialized) |
return; |
+ decoder_thread_.Stop(); |
weak_this_factory_.InvalidateWeakPtrs(); |
output_picture_buffers_.clear(); |
stale_output_picture_buffers_.clear(); |
@@ -1015,7 +1019,7 @@ void DXVAVideoDecodeAccelerator::Invalidate() { |
pending_input_buffers_.clear(); |
decoder_.Release(); |
MFShutdown(); |
- state_ = kUninitialized; |
+ SetState(kUninitialized); |
} |
void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) { |
@@ -1029,13 +1033,14 @@ void DXVAVideoDecodeAccelerator::NotifyFlushDone() { |
} |
void DXVAVideoDecodeAccelerator::NotifyResetDone() { |
+ pending_output_samples_.clear(); |
if (client_) |
client_->NotifyResetDone(); |
} |
void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) { |
// This task could execute after the decoder has been torn down. |
- if (state_ != kUninitialized && client_) { |
+ if (GetState() != kUninitialized && client_) { |
client_->ProvidePictureBuffers( |
kNumPictureBuffers, |
gfx::Size(width, height), |
@@ -1046,27 +1051,28 @@ void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) { |
void DXVAVideoDecodeAccelerator::NotifyPictureReady( |
const media::Picture& picture) { |
// This task could execute after the decoder has been torn down. |
- if (state_ != kUninitialized && client_) |
+ if (GetState() != kUninitialized && client_) |
client_->PictureReady(picture); |
} |
-void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() { |
- if (!client_ || !pending_output_samples_.empty()) |
+void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped( |
+ const PendingInputs& input_buffers) { |
+ if (!client_) |
return; |
- for (PendingInputs::iterator it = pending_input_buffers_.begin(); |
- it != pending_input_buffers_.end(); ++it) { |
+ for (PendingInputs::const_iterator it = input_buffers.begin(); |
+ it != input_buffers.end(); ++it) { |
LONGLONG input_buffer_id = 0; |
RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id), |
"Failed to get buffer id associated with sample",); |
client_->NotifyEndOfBitstreamBuffer(input_buffer_id); |
} |
- pending_input_buffers_.clear(); |
} |
void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), |
- "Invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), |
+ "Invalid state: " << state, ILLEGAL_STATE,); |
if (pending_input_buffers_.empty() || !pending_output_samples_.empty()) |
return; |
@@ -1079,7 +1085,7 @@ void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
DecodeInternal(*it); |
} |
- if (state_ != kFlushingPendingInputBuffers) |
+ if (GetState() != kFlushingPendingInputBuffers) |
return; |
// If we are scheduled during a flush operation then mark the flush as |
@@ -1088,7 +1094,7 @@ void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
// scheduled again by the ProcessPendingSamples function once slots become |
// available. |
if (pending_input_buffers_.empty() && pending_output_samples_.empty()) { |
- state_ = kNormal; |
+ SetState(kNormal); |
base::MessageLoop::current()->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, |
@@ -1097,12 +1103,18 @@ void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
} |
void DXVAVideoDecodeAccelerator::FlushInternal() { |
+ if (decoder_thread_.thread_id() != ::GetCurrentThreadId()) { |
+ decoder_thread_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::FlushInternal, |
+ base::Unretained(this))); |
+ return; |
+ } |
// The DoDecode function sets the state to kStopped when the decoder returns |
// MF_E_TRANSFORM_NEED_MORE_INPUT. |
// The MFT decoder can buffer upto 30 frames worth of input before returning |
// an output frame. This loop here attempts to retrieve as many output frames |
// as possible from the buffered set. |
- while (state_ != kStopped) { |
+ while (GetState() != kStopped) { |
DoDecode(); |
if (!pending_output_samples_.empty()) |
return; |
@@ -1114,27 +1126,25 @@ void DXVAVideoDecodeAccelerator::FlushInternal() { |
// transitions in the decoder are a touch intertwined with other portions of |
// the code like AssignPictureBuffers, ReusePictureBuffers etc. |
if (!pending_input_buffers_.empty()) { |
- state_ = kFlushingPendingInputBuffers; |
- base::MessageLoop::current()->PostTask( |
+ SetState(kFlushingPendingInputBuffers); |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, |
weak_this_factory_.GetWeakPtr())); |
return; |
} |
- base::MessageLoop::current()->PostTask( |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, |
weak_this_factory_.GetWeakPtr())); |
- state_ = kNormal; |
+ SetState(kNormal); |
} |
void DXVAVideoDecodeAccelerator::DecodeInternal( |
const base::win::ScopedComPtr<IMFSample>& sample) { |
- DCHECK(CalledOnValidThread()); |
- |
- if (state_ == kUninitialized) |
+ if (GetState() == kUninitialized) |
return; |
if (!pending_output_samples_.empty() || !pending_input_buffers_.empty()) { |
@@ -1159,8 +1169,10 @@ void DXVAVideoDecodeAccelerator::DecodeInternal( |
// decoder failure. |
if (hr == MF_E_NOTACCEPTING) { |
DoDecode(); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal), |
- "Failed to process output. Unexpected decoder state: " << state_, |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal || |
+ state == kFlushing), |
+ "Failed to process output. Unexpected decoder state: " << state, |
PLATFORM_FAILURE,); |
hr = decoder_->ProcessInput(0, sample.get(), 0); |
// If we continue to get the MF_E_NOTACCEPTING error we do the following:- |
@@ -1188,9 +1200,11 @@ void DXVAVideoDecodeAccelerator::DecodeInternal( |
DoDecode(); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal || |
- state_ == kFlushingPendingInputBuffers), |
- "Failed to process output. Unexpected decoder state: " << state_, |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal || |
+ state == kFlushingPendingInputBuffers || |
+ state == kFlushing), |
ananta
2014/12/03 00:12:44
We need to allow kFlushing here and below because
|
+ "Failed to process output. Unexpected decoder state: " << state, |
ILLEGAL_STATE,); |
LONGLONG input_buffer_id = 0; |
@@ -1206,7 +1220,7 @@ void DXVAVideoDecodeAccelerator::DecodeInternal( |
// decoder to emit an output packet for every input packet. |
// http://code.google.com/p/chromium/issues/detail?id=108121 |
// http://code.google.com/p/chromium/issues/detail?id=150925 |
- base::MessageLoop::current()->PostTask( |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead, |
weak_this_factory_.GetWeakPtr(), |
@@ -1257,4 +1271,41 @@ void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer( |
stale_output_picture_buffers_.erase(it); |
} |
+DXVAVideoDecodeAccelerator::State |
+DXVAVideoDecodeAccelerator::GetState() const { |
+ State state = kUninitialized; |
+ ::InterlockedExchange(reinterpret_cast<long*>(&state), |
+ state_); |
+ return state; |
+} |
+ |
+void DXVAVideoDecodeAccelerator::SetState(State new_state) { |
+ ::InterlockedCompareExchange(reinterpret_cast<long*>(&state_), |
+ new_state, state_); |
+} |
+ |
+void DXVAVideoDecodeAccelerator::ResetHelper() { |
+ SetState(kResetting); |
+ |
+ PendingInputs pending_input_buffers_copy; |
+ std::swap(pending_input_buffers_, pending_input_buffers_copy); |
+ pending_input_buffers_.clear(); |
+ |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped, |
+ weak_this_factory_.GetWeakPtr(), |
+ pending_input_buffers_copy)); |
+ |
+ RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0), |
+ "Reset: Failed to send message.", PLATFORM_FAILURE,); |
+ |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone, |
+ weak_this_factory_.GetWeakPtr())); |
+ |
+ SetState(DXVAVideoDecodeAccelerator::kNormal); |
+} |
+ |
} // namespace content |