Index: content/common/gpu/media/dxva_video_decode_accelerator.cc |
=================================================================== |
--- content/common/gpu/media/dxva_video_decode_accelerator.cc (revision 0) |
+++ content/common/gpu/media/dxva_video_decode_accelerator.cc (revision 0) |
@@ -0,0 +1,832 @@ |
+// Copyright (c) 2011 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 "content/common/gpu/media/dxva_video_decode_accelerator.h" |
+ |
+#include <ks.h> |
+#include <codecapi.h> |
+#include <d3dx9tex.h> |
+#include <mfapi.h> |
+#include <mferror.h> |
+#include <wmcodecdsp.h> |
+ |
+#include "base/lazy_instance.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_handle.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/shared_memory.h" |
+#include "base/time.h" |
+#include "media/video/video_decode_accelerator.h" |
+#include "third_party/angle/include/EGL/egl.h" |
Ami GONE FROM CHROMIUM
2011/12/13 02:37:12
minimize headers?
ananta
2011/12/13 02:51:18
Done.
|
+#include "third_party/angle/include/GLES2/gl2.h" |
+#include "third_party/angle/include/GLES2/gl2ext.h" |
+ |
+namespace { |
+ |
+static const int kNumPictureBuffers = 5; |
+static const int kNumInputs = 100; |
+ |
+IMFSample* CreateEmptySample() { |
+ HRESULT hr = E_FAIL; |
+ base::win::ScopedComPtr<IMFSample> sample; |
+ hr = MFCreateSample(sample.Receive()); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Unable to create an empty sample"; |
+ return NULL; |
+ } |
+ return sample.Detach(); |
+} |
+ |
+// Creates a Media Foundation sample with one buffer of length |buffer_length| |
+// on a |align|-byte boundary. Alignment must be a perfect power of 2 or 0. |
+// If |align| is 0, then no alignment is specified. |
+IMFSample* CreateEmptySampleWithBuffer(int buffer_length, int align) { |
+ CHECK_GT(buffer_length, 0); |
+ base::win::ScopedComPtr<IMFSample> sample; |
+ sample.Attach(CreateEmptySample()); |
+ if (!sample.get()) |
+ return NULL; |
+ base::win::ScopedComPtr<IMFMediaBuffer> buffer; |
+ HRESULT hr = E_FAIL; |
+ if (align == 0) { |
+ // Note that MFCreateMemoryBuffer is same as MFCreateAlignedMemoryBuffer |
+ // with the align argument being 0. |
+ hr = MFCreateMemoryBuffer(buffer_length, buffer.Receive()); |
+ } else { |
+ hr = MFCreateAlignedMemoryBuffer(buffer_length, |
+ align - 1, |
+ buffer.Receive()); |
+ } |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Unable to create an empty buffer"; |
+ return NULL; |
+ } |
+ hr = sample->AddBuffer(buffer.get()); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to add empty buffer to sample"; |
+ return NULL; |
+ } |
+ return sample.Detach(); |
+} |
+ |
+// Creates a Media Foundation sample with one buffer containing a copy of the |
+// given Annex B stream data. |
+// If duration and sample time are not known, provide 0. |
+// |min_size| specifies the minimum size of the buffer (might be required by |
+// the decoder for input). The times here should be given in 100ns units. |
+// |alignment| specifies the buffer in the sample to be aligned. If no |
+// alignment is required, provide 0 or 1. |
+static IMFSample* CreateInputSample(const uint8* stream, int size, |
+ int64 timestamp, int64 duration, |
+ int min_size, int alignment) { |
+ CHECK(stream); |
+ CHECK_GT(size, 0); |
+ base::win::ScopedComPtr<IMFSample> sample; |
+ sample.Attach(CreateEmptySampleWithBuffer(std::max(min_size, size), |
+ alignment)); |
+ if (!sample.get()) { |
+ NOTREACHED() << "Failed to create empty buffer for input"; |
+ return NULL; |
+ } |
+ HRESULT hr = E_FAIL; |
+ if (duration > 0) { |
+ hr = sample->SetSampleDuration(duration); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to set sample duration"; |
+ return NULL; |
+ } |
+ } |
+ if (timestamp > 0) { |
+ hr = sample->SetSampleTime(timestamp); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to set sample time"; |
+ return NULL; |
+ } |
+ } |
+ base::win::ScopedComPtr<IMFMediaBuffer> buffer; |
+ hr = sample->GetBufferByIndex(0, buffer.Receive()); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to get buffer in sample"; |
+ return NULL; |
+ } |
+ DWORD max_length = 0, current_length = 0; |
+ uint8* destination = NULL; |
+ hr = buffer->Lock(&destination, &max_length, ¤t_length); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to lock buffer"; |
+ return NULL; |
+ } |
+ CHECK_EQ(current_length, 0u); |
+ CHECK_GE(static_cast<int>(max_length), size); |
+ memcpy(destination, stream, size); |
+ CHECK(SUCCEEDED(buffer->Unlock())); |
+ hr = buffer->SetCurrentLength(size); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to set current length to " << size; |
+ return NULL; |
+ } |
+ hr = sample->SetUINT32(MFSampleExtension_CleanPoint, TRUE); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to mark sample as key sample"; |
+ return NULL; |
+ } |
+ return sample.Detach(); |
+} |
+ |
+} // namespace |
+ |
+DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator( |
+ media::VideoDecodeAccelerator::Client* client, |
+ base::ProcessHandle renderer_process) |
+ : client_(client), |
+ message_loop_(MessageLoop::current()), |
+ surface_width_(0), |
+ surface_height_(0), |
+ state_(kUninitialized), |
+ input_stream_info_(), |
+ output_stream_info_(), |
+ pictures_requested_(false), |
+ renderer_process_(renderer_process) { |
+ input_buffer_frame_times_.reserve(kNumInputs); |
+ |
+#if !defined(NDEBUG) |
+ decode_start_time_ = 0; |
+ inputs_before_decode_ = 0; |
+#endif // !defined(NDEBUG) |
+} |
+ |
+DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() { |
+ DCHECK_EQ(message_loop_, MessageLoop::current()); |
+ client_ = NULL; |
+ message_loop_ = NULL; |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::Initialize(Profile profile) { |
+ DCHECK_EQ(message_loop_, MessageLoop::current()); |
+ |
+ if (state_ != kUninitialized) { |
+ NOTREACHED() << "Initialize: invalid state: " |
+ << state_; |
+ return false; |
+ } |
+ |
+ HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "MFStartup failed. Error:" |
+ << std::hex << std::showbase << hr; |
+ return false; |
+ } |
+ if (!CreateD3DDevManager()) |
+ return false; |
+ if (!InitDecoder()) |
+ return false; |
+ if (!GetStreamsInfoAndBufferReqs()) |
+ return false; |
+ if (SendMFTMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)) { |
+ state_ = DXVAVideoDecodeAccelerator::kNormal; |
+ client_->NotifyInitializeDone(); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+void DXVAVideoDecodeAccelerator::Decode( |
+ const media::BitstreamBuffer& bitstream_buffer) { |
+ DCHECK_EQ(message_loop_, MessageLoop::current()); |
+ if (state_ == DXVAVideoDecodeAccelerator::kUninitialized) { |
+ NOTREACHED() << "ConsumeVideoSample: invalid state"; |
+ return; |
+ } |
+ |
+ HANDLE shared_memory_handle = NULL; |
+ if (!::DuplicateHandle(renderer_process_, |
+ bitstream_buffer.handle(), |
+ ::GetCurrentProcess(), |
+ &shared_memory_handle, |
+ 0, |
+ FALSE, |
+ DUPLICATE_SAME_ACCESS)) { |
+ NOTREACHED() << "Failed to open duplicate shared mem handle"; |
+ return; |
+ } |
+ |
+ base::SharedMemory shm(shared_memory_handle, true); |
+ if (!shm.Map(bitstream_buffer.size())) { |
+ NOTREACHED() << "Failed in SharedMemory::Map()"; |
+ return; |
+ } |
+ |
+ base::Time::Exploded exploded; |
+ base::Time::Now().LocalExplode(&exploded); |
+ base::win::ScopedComPtr<IMFSample> sample; |
+ sample.Attach(CreateInputSample(reinterpret_cast<const uint8*>(shm.memory()), |
+ bitstream_buffer.size(), |
+ exploded.second * 10000000, |
+ 400000, |
+ input_stream_info_.cbSize, |
+ input_stream_info_.cbAlignment)); |
+ if (!sample.get()) { |
+ NOTREACHED() << "Failed to create an input sample"; |
+ return; |
+ } else { |
+#if !defined(NDEBUG) |
+ inputs_before_decode_++; |
+ if (!decode_start_time_) |
+ decode_start_time_ = ::GetTickCount(); |
+#endif // !defined(NDEBUG) |
+ SendMFTMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0); |
+ |
+ if (FAILED(decoder_->ProcessInput(0, sample.get(), 0))) { |
+ NOTREACHED() << "Failed to process input"; |
+ return; |
+ } |
+ } |
+ if (state_ != DXVAVideoDecodeAccelerator::kEosDrain) { |
+ if (!SendMFTMessage(MFT_MESSAGE_NOTIFY_END_OF_STREAM, 0)) { |
+ NOTREACHED() << "Failed to send eos message to MFT"; |
+ } else { |
+ state_ = DXVAVideoDecodeAccelerator::kEosDrain; |
+ } |
+ } |
+ input_buffer_frame_times_.push_back(bitstream_buffer.id()); |
+ DoDecode(); |
+ client_->NotifyEndOfBitstreamBuffer(bitstream_buffer.id()); |
+} |
+ |
+void DXVAVideoDecodeAccelerator::AssignPictureBuffers( |
+ const std::vector<media::PictureBuffer>& buffers) { |
+ DCHECK_EQ(message_loop_, MessageLoop::current()); |
+ // Copy the picture buffers provided by the client to the available list, |
+ // and mark these buffers as available for use. |
+ for (size_t buffer_index = 0; buffer_index < buffers.size(); ++buffer_index) { |
+ DXVAPictureBuffer picture_buffer; |
+ picture_buffer.available = true; |
+ picture_buffer.picture_buffer = buffers[buffer_index]; |
+ |
+ DCHECK(available_pictures_.find(buffers[buffer_index].id()) == |
+ available_pictures_.end()); |
+ available_pictures_[buffers[buffer_index].id()] = picture_buffer; |
+ } |
+ int buffer_index = 0; |
+ PendingOutputSamples::iterator sample_index = |
+ pending_output_samples_.begin(); |
+ HRESULT hr = E_FAIL; |
+ |
+ ProcessPendingSamples(); |
+} |
+ |
+void DXVAVideoDecodeAccelerator::ReusePictureBuffer( |
+ int32 picture_buffer_id) { |
+ DCHECK_EQ(message_loop_, MessageLoop::current()); |
+ DCHECK(available_pictures_.find(picture_buffer_id) != |
+ available_pictures_.end()); |
+ available_pictures_[picture_buffer_id].available = true; |
+ |
+ PendingOutputSamples::iterator sample_index = |
+ pending_output_samples_.begin(); |
+ |
+ if (sample_index != pending_output_samples_.end()) { |
+ const PendingSampleInfo& sample_info = *sample_index; |
+ CopyOutputSampleDataToPictureBuffer( |
+ sample_info.surface, |
+ available_pictures_[picture_buffer_id].picture_buffer, |
+ sample_info.input_buffer_id); |
+ available_pictures_[picture_buffer_id].available = false; |
+ pending_output_samples_.erase(sample_index); |
+ } |
+} |
+ |
+void DXVAVideoDecodeAccelerator::Flush() { |
+ DCHECK_EQ(message_loop_, MessageLoop::current()); |
+ |
+ LOG(INFO) << "DXVAVideoDecodeAccelerator::Flush"; |
+ if (state_ == DXVAVideoDecodeAccelerator::kUninitialized) { |
+ NOTREACHED() << "ConsumeVideoSample: invalid state"; |
+ client_->NotifyFlushDone(); |
+ return; |
+ } |
+ |
+ if (state_ == DXVAVideoDecodeAccelerator::kStopped) { |
+ LOG(INFO) << "No data available from the decoder"; |
+ client_->NotifyFlushDone(); |
+ return; |
+ } |
+ |
+ state_ = DXVAVideoDecodeAccelerator::kEosDrain; |
+ if (!SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0)) { |
+ LOG(WARNING) << "Failed to send drain message"; |
+ state_ = DXVAVideoDecodeAccelerator::kStopped; |
+ client_->NotifyFlushDone(); |
+ return; |
+ } |
+ |
+ // As per MSDN docs after the client sends this message, it calls |
+ // IMFTransform::ProcessOutput in a loop, until ProcessOutput returns the |
+ // error code MF_E_TRANSFORM_NEED_MORE_INPUT. The DoDecode function sets the |
+ // state to DXVAVideoDecodeAccelerator::kStopped when the decoder returns |
+ // MF_E_TRANSFORM_NEED_MORE_INPUT. |
+ while (state_ != DXVAVideoDecodeAccelerator::kStopped) { |
+ DoDecode(); |
+ } |
+ client_->NotifyFlushDone(); |
+} |
+ |
+void DXVAVideoDecodeAccelerator::Reset() { |
+ DCHECK_EQ(message_loop_, MessageLoop::current()); |
+ LOG(INFO) << "DXVAVideoDecodeAccelerator::Reset"; |
+ if (state_ != kNormal && state_ != kStopped) { |
+ NOTREACHED() << "Reset: invalid state"; |
+ client_->NotifyResetDone(); |
+ return; |
+ } |
+ |
+ state_ = DXVAVideoDecodeAccelerator::kResetting; |
+ if (!SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0)) { |
+ LOG(WARNING) << "DXVAVideoDecodeAccelerator::Flush failed to send message"; |
+ client_->NotifyResetDone(); |
+ return; |
+ } |
+ state_ = DXVAVideoDecodeAccelerator::kNormal; |
+ client_->NotifyResetDone(); |
+ input_buffer_frame_times_.clear(); |
+} |
+ |
+void DXVAVideoDecodeAccelerator::Destroy() { |
+ DCHECK_EQ(message_loop_, MessageLoop::current()); |
+ OutputBuffers::iterator index; |
+ for (index = available_pictures_.begin(); index != available_pictures_.end(); |
+ ++index) { |
+ client_->DismissPictureBuffer(index->second.picture_buffer.id()); |
+ } |
+ available_pictures_.clear(); |
+ pending_output_samples_.clear(); |
+ input_buffer_frame_times_.clear(); |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::CreateD3DDevManager() { |
+ d3d9_.Attach(Direct3DCreate9(D3D_SDK_VERSION)); |
+ if (d3d9_.get() == NULL) { |
+ NOTREACHED() << "Failed to create D3D9"; |
+ return false; |
+ } |
+ |
+ D3DPRESENT_PARAMETERS present_params = {0}; |
+ present_params.BackBufferWidth = 0; |
+ present_params.BackBufferHeight = 0; |
+ present_params.BackBufferFormat = D3DFMT_UNKNOWN; |
+ present_params.BackBufferCount = 1; |
+ present_params.SwapEffect = D3DSWAPEFFECT_DISCARD; |
+ present_params.hDeviceWindow = GetShellWindow(); |
+ present_params.Windowed = TRUE; |
+ present_params.Flags = D3DPRESENTFLAG_VIDEO; |
+ present_params.FullScreen_RefreshRateInHz = 0; |
+ present_params.PresentationInterval = 0; |
+ |
+ HRESULT hr = d3d9_->CreateDevice(D3DADAPTER_DEFAULT, |
+ D3DDEVTYPE_HAL, |
+ GetShellWindow(), |
+ (D3DCREATE_HARDWARE_VERTEXPROCESSING | |
+ D3DCREATE_MULTITHREADED), |
+ &present_params, |
+ device_.Receive()); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to create D3D Device"; |
+ return false; |
+ } |
+ |
+ UINT dev_manager_reset_token = 0; |
+ hr = DXVA2CreateDirect3DDeviceManager9(&dev_manager_reset_token, |
+ device_manager_.Receive()); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Couldn't create D3D Device manager"; |
+ return false; |
+ } |
+ |
+ hr = device_manager_->ResetDevice(device_.get(), |
+ dev_manager_reset_token); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to set device to device manager"; |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+ |
+bool DXVAVideoDecodeAccelerator::InitDecoder() { |
+ HRESULT hr = CoCreateInstance(__uuidof(CMSH264DecoderMFT), |
+ NULL, |
+ CLSCTX_INPROC_SERVER, |
+ __uuidof(IMFTransform), |
+ reinterpret_cast<void**>(decoder_.Receive())); |
+ if (FAILED(hr) || !decoder_.get()) { |
+ NOTREACHED() << "CoCreateInstance failed " |
+ << std::hex << std::showbase << hr; |
+ return false; |
+ } |
+ |
+ if (!CheckDecoderDxvaSupport()) |
+ return false; |
+ hr = decoder_->ProcessMessage( |
+ MFT_MESSAGE_SET_D3D_MANAGER, |
+ reinterpret_cast<ULONG_PTR>(device_manager_.get())); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to pass D3D9 device to decoder " |
+ << std::hex << hr; |
+ return false; |
+ } |
+ return SetDecoderMediaTypes(); |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::CheckDecoderDxvaSupport() { |
+ base::win::ScopedComPtr<IMFAttributes> attributes; |
+ HRESULT hr = decoder_->GetAttributes(attributes.Receive()); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Unlock: Failed to get attributes, hr = " |
+ << std::hex << std::showbase << hr; |
+ return false; |
+ } |
+ UINT32 dxva = 0; |
+ hr = attributes->GetUINT32(MF_SA_D3D_AWARE, &dxva); |
+ if (FAILED(hr) || !dxva) { |
+ NOTREACHED() << "Failed to get DXVA attr. Error:" |
+ << std::hex << std::showbase << hr |
+ << " .This might not be the right decoder."; |
+ return false; |
+ } |
+ |
+ hr = attributes->SetUINT32(CODECAPI_AVDecVideoAcceleration_H264, TRUE); |
+ DCHECK(SUCCEEDED(hr)); |
+ return true; |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::SetDecoderMediaTypes() { |
+ MFT_REGISTER_TYPE_INFO* input_types = NULL; |
+ MFT_REGISTER_TYPE_INFO* output_types = NULL; |
+ uint32 num_inputs = 0; |
+ uint32 num_outputs = 0; |
+ HRESULT hr = MFTGetInfo(__uuidof(CMSH264DecoderMFT), NULL, &input_types, |
+ &num_inputs, &output_types, &num_outputs, NULL); |
+ if (!SetDecoderInputMediaType()) |
+ return false; |
+ return SetDecoderOutputMediaType(MFVideoFormat_NV12); |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::SetDecoderInputMediaType() { |
+ base::win::ScopedComPtr<IMFMediaType> media_type; |
+ HRESULT hr = MFCreateMediaType(media_type.Receive()); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to create empty media type object"; |
+ return false; |
+ } |
+ |
+ hr = media_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "SetGUID for major type failed"; |
+ return false; |
+ } |
+ |
+ hr = media_type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_H264); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "SetGUID for subtype failed"; |
+ return false; |
+ } |
+ |
+ hr = decoder_->SetInputType(0, media_type.get(), 0); // No flags |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to set decoder's input type"; |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::SetDecoderOutputMediaType( |
+ const GUID& subtype) { |
+ DWORD i = 0; |
+ IMFMediaType* out_media_type = NULL; |
+ bool found = false; |
+ while (SUCCEEDED(decoder_->GetOutputAvailableType(0, i, &out_media_type))) { |
+ GUID out_subtype = {0}; |
+ HRESULT hr = out_media_type->GetGUID(MF_MT_SUBTYPE, &out_subtype); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to GetGUID() on GetOutputAvailableType() " |
+ << i; |
+ out_media_type->Release(); |
+ continue; |
+ } |
+ if (out_subtype == subtype) { |
+ hr = decoder_->SetOutputType(0, out_media_type, 0); // No flags |
+ hr = MFGetAttributeSize(out_media_type, MF_MT_FRAME_SIZE, |
+ reinterpret_cast<UINT32*>(&surface_width_), |
+ reinterpret_cast<UINT32*>(&surface_height_)); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to SetOutputType to |subtype| or obtain " |
+ << "width/height " << std::hex << hr; |
+ } |
+ out_media_type->Release(); |
+ return true; |
+ } |
+ i++; |
+ out_media_type->Release(); |
+ } |
+ return false; |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::SendMFTMessage(MFT_MESSAGE_TYPE msg, |
+ int32 param) { |
+ HRESULT hr = decoder_->ProcessMessage(msg, param); |
+ return SUCCEEDED(hr); |
+} |
+ |
+// Gets the minimum buffer sizes for input and output samples. |
+// The MFT will not allocate buffer for neither input nor output, so we have |
+// to do it ourselves and make sure they're the correct size. |
+// Exception is when dxva is enabled, the decoder will allocate output. |
+bool DXVAVideoDecodeAccelerator::GetStreamsInfoAndBufferReqs() { |
+ HRESULT hr = decoder_->GetInputStreamInfo(0, &input_stream_info_); |
+ if (FAILED(hr)) { |
+ LOG(ERROR) << "Failed to get input stream info"; |
+ return false; |
+ } |
+ LOG(INFO) << "Input stream info: "; |
+ LOG(INFO) << "Max latency: " << input_stream_info_.hnsMaxLatency; |
+ // There should be three flags, one for requiring a whole frame be in a |
+ // single sample, one for requiring there be one buffer only in a single |
+ // sample, and one that specifies a fixed sample size. (as in cbSize) |
+ LOG(INFO) << "Flags: " |
+ << std::hex << std::showbase << input_stream_info_.dwFlags; |
+ CHECK_EQ(input_stream_info_.dwFlags, 0x7u); |
+ LOG(INFO) << "Min buffer size: " << input_stream_info_.cbSize; |
+ LOG(INFO) << "Max lookahead: " << input_stream_info_.cbMaxLookahead; |
+ LOG(INFO) << "Alignment: " << input_stream_info_.cbAlignment; |
+ |
+ hr = decoder_->GetOutputStreamInfo(0, &output_stream_info_); |
+ if (FAILED(hr)) { |
+ LOG(ERROR) << "Failed to get output stream info"; |
+ return false; |
+ } |
+ LOG(INFO) << "Output stream info: "; |
+ // The flags here should be the same and mean the same thing, except when |
+ // DXVA is enabled, there is an extra 0x100 flag meaning decoder will |
+ // allocate its own sample. |
+ LOG(INFO) << "Flags: " |
+ << std::hex << std::showbase << output_stream_info_.dwFlags; |
+ CHECK_EQ(output_stream_info_.dwFlags, 0x107u); |
+ LOG(INFO) << "Min buffer size: " << output_stream_info_.cbSize; |
+ LOG(INFO) << "Alignment: " << output_stream_info_.cbAlignment; |
+ return true; |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::DoDecode() { |
+ if (state_ != kNormal && state_ != kEosDrain) { |
+ NOTREACHED() << "DoDecode: not in normal or drain state"; |
+ return false; |
+ } |
+ // scoped_refptr<VideoFrame> frame; |
+ base::win::ScopedComPtr<IMFSample> output_sample; |
+ MFT_OUTPUT_DATA_BUFFER output_data_buffer; |
+ DWORD status = 0; |
+ |
+ memset(&output_data_buffer, 0, sizeof(output_data_buffer)); |
+ output_data_buffer.pSample = output_sample; |
+ status = 0; |
+ |
+ HRESULT hr = decoder_->ProcessOutput(0, // No flags |
+ 1, // # of out streams to pull from |
+ &output_data_buffer, |
+ &status); |
+ IMFCollection* events = output_data_buffer.pEvents; |
+ if (events != NULL) { |
+ LOG(INFO) << "Got events from ProcessOuput, but discarding"; |
+ events->Release(); |
+ } |
+ if (FAILED(hr)) { |
+ if (hr == MF_E_TRANSFORM_STREAM_CHANGE) { |
+ hr = SetDecoderOutputMediaType(MFVideoFormat_NV12); |
+ if (SUCCEEDED(hr)) { |
+ return true; |
+ } else { |
+ NOTREACHED() << "Failed to set decoder output media type"; |
+ return false; |
+ } |
+ } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) { |
+ // No more output from the decoder. Notify EOS and stop playback. |
+ state_ = DXVAVideoDecodeAccelerator::kStopped; |
+ return false; |
+ } else { |
+ NOTREACHED() << "Unhandled error in DoDecode()"; |
+ state_ = DXVAVideoDecodeAccelerator::kStopped; |
+ return false; |
+ } |
+ } |
+ |
+#if !defined(NDEBUG) |
+ DLOG(INFO) << "Number of input packets before successful decode: " |
+ << inputs_before_decode_; |
+ inputs_before_decode_ = 0; |
+ uint32 end_decode = GetTickCount(); |
+ DLOG(INFO) << "Total time for decode: " |
+ << end_decode - decode_start_time_; |
+ decode_start_time_ = 0; |
+#endif // !defined(NDEBUG) |
+ if (!ProcessOutputSample(output_data_buffer.pSample)) { |
+ state_ = DXVAVideoDecodeAccelerator::kStopped; |
+ return false; |
+ } |
+ |
+ input_buffer_frame_times_.clear(); |
+ state_ = DXVAVideoDecodeAccelerator::kNormal; |
+ return true; |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) { |
+ if (!sample) { |
+ NOTREACHED() << "ProcessOutput succeeded, but did not get a sample back"; |
+ return false; |
+ } |
+ base::win::ScopedComPtr<IMFSample> output_sample; |
+ output_sample.Attach(sample); |
+ |
+ if (!input_buffer_frame_times_.size()) { |
+ DLOG(INFO) << "In input buffers left to process output"; |
+ return false; |
+ } |
+ int32 input_buffer_id = |
+ input_buffer_frame_times_[input_buffer_frame_times_.size() - 1]; |
+ input_buffer_frame_times_.clear(); |
+ |
+ base::win::ScopedComPtr<IMFMediaBuffer> output_buffer; |
+ HRESULT hr = sample->GetBufferByIndex(0, output_buffer.Receive()); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to get buffer from sample"; |
+ return false; |
+ } |
+ base::win::ScopedComPtr<IDirect3DSurface9> surface; |
+ hr = MFGetService(output_buffer, MR_BUFFER_SERVICE, |
+ IID_PPV_ARGS(surface.Receive())); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to get surface from buffer"; |
+ return false; |
+ } |
+ |
+ D3DSURFACE_DESC surface_desc; |
+ hr = surface->GetDesc(&surface_desc); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to get surface description"; |
+ return false; |
+ } |
+ |
+#if !defined(NDEBUG) |
+ DWORD start_surface_time = GetTickCount(); |
+#endif // !defined(NDEBUG) |
+ |
+ // TODO(ananta) |
+ // The code below may not be necessary once we have an ANGLE extension which |
+ // allows us to pass the Direct 3D surface directly for rendering. |
+ |
+ // The decoded bits in the source direct 3d surface are in the YUV |
+ // format. Angle does not support that. As a workaround we create an |
+ // offscreen surface in the RGB format and copy the source surface |
+ // to this surface. |
+ base::win::ScopedComPtr<IDirect3DSurface9> dest_surface; |
+ hr = device_->CreateOffscreenPlainSurface(surface_desc.Width, |
+ surface_desc.Height, |
+ D3DFMT_A8R8G8B8, |
+ D3DPOOL_DEFAULT, |
+ dest_surface.Receive(), |
+ NULL); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to create offscreen surface"; |
+ return false; |
+ } |
+ |
+ hr = D3DXLoadSurfaceFromSurface(dest_surface, NULL, NULL, surface, NULL, |
+ NULL, 0, 0); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to copy source surface to dest."; |
+ return false; |
+ } |
+ |
+#if !defined(NDEBUG) |
+ DWORD end_surface_time = GetTickCount(); |
+ DLOG(INFO) << "Time to create and copy new surface is " |
+ << end_surface_time - start_surface_time; |
+#endif // !defined(NDEBUG) |
+ |
+ PendingSampleInfo sample_info; |
+ |
+ sample_info.input_buffer_id = input_buffer_id; |
+ sample_info.surface = dest_surface; |
+ pending_output_samples_.push_back(sample_info); |
+ |
+ // 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 (available_pictures_.size()) { |
+ ProcessPendingSamples(); |
+ return true; |
+ } |
+ |
+ if (pictures_requested_) { |
+ DLOG(INFO) << "Waiting for picture slots from the client."; |
+ return true; |
+ } |
+ // Go ahead and request picture buffers. |
+ client_->ProvidePictureBuffers( |
+ kNumPictureBuffers, gfx::Size(surface_desc.Width, surface_desc.Height)); |
+ pictures_requested_ = true; |
+ return true; |
+} |
+ |
+bool DXVAVideoDecodeAccelerator::CopyOutputSampleDataToPictureBuffer( |
+ IDirect3DSurface9* dest_surface, media::PictureBuffer picture_buffer, |
+ int32 input_buffer_id) { |
+ DCHECK(dest_surface); |
+ |
+ // Get the currently loaded bitmap from the DC. |
+ HDC hdc = NULL; |
+ |
+ HRESULT hr = dest_surface->GetDC(&hdc); |
+ if (FAILED(hr)) { |
+ NOTREACHED() << "Failed to get HDC for dest offscreen surface"; |
+ return false; |
+ } |
+ |
+ HBITMAP bitmap = |
+ reinterpret_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP)); |
+ if (!bitmap) { |
+ NOTREACHED() << "Failed to get bitmap from DC"; |
+ return false; |
+ } |
+ // TODO(ananta) |
+ // The code below may not be necessary once we have an ANGLE extension which |
+ // allows us to pass the Direct 3D surface directly for rendering. |
+ |
+ // The Device dependent bitmap is upside down for OpenGL. We convert the |
+ // bitmap to a DIB and render it on the texture instead. |
+ BITMAP bitmap_basic_info = {0}; |
+ GetObject(bitmap, sizeof(BITMAP), &bitmap_basic_info); |
+ |
+ BITMAPINFO bitmap_info = {0}; |
+ bitmap_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
+ bitmap_info.bmiHeader.biWidth = bitmap_basic_info.bmWidth; |
+ bitmap_info.bmiHeader.biHeight = bitmap_basic_info.bmHeight; |
+ bitmap_info.bmiHeader.biPlanes = 1; |
+ bitmap_info.bmiHeader.biBitCount = bitmap_basic_info.bmBitsPixel; |
+ bitmap_info.bmiHeader.biCompression = BI_RGB; |
+ bitmap_info.bmiHeader.biSizeImage = 0; |
+ bitmap_info.bmiHeader.biClrUsed = 0; |
+ |
+ int ret = GetDIBits(hdc, bitmap, 0, 0, NULL, &bitmap_info, DIB_RGB_COLORS); |
+ if (bitmap_info.bmiHeader.biSizeImage <= 0) { |
+ NOTREACHED() << "Failed to read bitmap size"; |
+ return false; |
+ } |
+ scoped_ptr<char> bits(new char[bitmap_info.bmiHeader.biSizeImage]); |
+ ret = GetDIBits(hdc, bitmap, 0, bitmap_basic_info.bmHeight, bits.get(), |
+ &bitmap_info, DIB_RGB_COLORS); |
+ DCHECK_GT(ret, 0); |
+ |
+ D3DSURFACE_DESC surface_desc; |
+ hr = dest_surface->GetDesc(&surface_desc); |
+ DCHECK(SUCCEEDED(hr)); |
+ |
+ glBindTexture(GL_TEXTURE_2D, picture_buffer.texture_id()); |
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, surface_desc.Width, |
+ surface_desc.Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
+ reinterpret_cast<GLvoid*>(bits.get())); |
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
+ glBindTexture(GL_TEXTURE_2D, 0); |
apatrick_chromium
2011/12/13 02:00:13
You should restore the texture binding to whatever
ananta
2011/12/13 02:51:18
Dunno how to restore the binding. Will look into t
ananta
2011/12/13 23:29:15
Done.
|
+ dest_surface->ReleaseDC(hdc); |
+ |
+ media::Picture output_picture(picture_buffer.id(), input_buffer_id); |
+ client_->PictureReady(output_picture); |
+ return true; |
+} |
+ |
+void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { |
+ PendingOutputSamples::iterator sample_index = |
+ pending_output_samples_.begin(); |
+ HRESULT hr = E_FAIL; |
+ |
+ while (sample_index != pending_output_samples_.end()) { |
+ const PendingSampleInfo& sample_info = *sample_index; |
+ OutputBuffers::iterator index; |
+ for (index = available_pictures_.begin(); |
+ index != available_pictures_.end(); |
+ ++index) { |
+ if (index->second.available) { |
+ CopyOutputSampleDataToPictureBuffer(sample_info.surface, |
+ index->second.picture_buffer, |
+ sample_info.input_buffer_id); |
+ index->second.available = false; |
+ sample_index = pending_output_samples_.erase(sample_index); |
+ break; |
+ } |
+ } |
+ if (index == available_pictures_.end()) { |
+ DLOG(INFO) << "No available picture slots for output"; |
+ break; |
+ } |
+ } |
+} |
+ |