Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Unified Diff: content/common/gpu/media/dxva_video_decode_accelerator.cc

Issue 8510039: Initial implementation of the DXVA 2.0 H.264 hardware decoder for pepper for Windows. The decodin... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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,837 @@
+// 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.
+
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Assert this file is only built on OS_WIN: #if !def
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
+#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"
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 remove
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
+#include "base/logging.h"
+#include "base/memory/scoped_handle.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop.h"
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 can remove if you take my NonThreadSafe suggestion
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
+#include "base/shared_memory.h"
+#include "base/time.h"
+#include "media/video/video_decode_accelerator.h"
+#include "third_party/angle/include/GLES2/gl2.h"
+#include "third_party/angle/include/GLES2/gl2ext.h"
+
+namespace {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Since almost all contents of this are static and a
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
+
+static const int kNumPictureBuffers = 5;
+static const int kNumInputs = 100;
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Are these magic DXVA constants? Where do they com
+
+IMFSample* CreateEmptySample() {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 static
ananta 2011/12/13 23:29:15 Done.
+ HRESULT hr = E_FAIL;
+ base::win::ScopedComPtr<IMFSample> sample;
+ hr = MFCreateSample(sample.Receive());
+ if (FAILED(hr)) {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 This seems like a pretty common pattern (similar t
ananta 2011/12/13 23:29:15 Done.
+ 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.
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 drop this line?
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
+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.
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Units of 100ns is pretty unchrome-y. Is it not a
ananta 2011/12/13 23:29:15 Unnecessary now. Removed these parameters as we do
ananta 2011/12/13 23:29:15 Removed these parameters as they are not needed.
+// |alignment| specifies the buffer in the sample to be aligned. If no
+// alignment is required, provide 0 or 1.
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 "0 or 1"? Any reason to allow/suggest both?
ananta 2011/12/13 23:29:15 Bad comment. Fixed.
ananta 2011/12/13 23:29:15 Fixed
+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;
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 comma is unchrome-y.
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
+ uint8* destination = NULL;
+ hr = buffer->Lock(&destination, &max_length, &current_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()->message_loop_proxy()),
+ surface_width_(0),
+ surface_height_(0),
+ state_(kUninitialized),
+ input_stream_info_(),
+ output_stream_info_(),
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 This and the previous line are unnecessary, I thin
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
+ pictures_requested_(false),
+ renderer_process_(renderer_process),
+ dev_manager_reset_token_(0) {
+ input_buffer_frame_times_.reserve(kNumInputs);
+
+#if !defined(NDEBUG)
+ decode_start_time_ = 0;
+ inputs_before_decode_ = 0;
+#endif // !defined(NDEBUG)
+}
+
+DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() {
+ client_ = NULL;
+ message_loop_ = NULL;
+}
+
+bool DXVAVideoDecodeAccelerator::Initialize(Profile profile) {
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 profile is unused in this file. Does the MF decod
ananta 2011/12/13 23:29:15 We don't need to pass it specifically.
+ DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
+
+ if (state_ != kUninitialized) {
+ NOTREACHED() << "Initialize: invalid state: "
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 One line.
ananta 2011/12/13 23:29:15 Done.
+ << state_;
+ return false;
+ }
+
+ HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL);
+ if (FAILED(hr)) {
+ NOTREACHED() << "MFStartup failed. Error:"
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Single line (here and below; I'm going to stop com
ananta 2011/12/13 23:29:15 Done.
+ << std::hex << std::showbase << hr;
cpu_(ooo_6.6-7.5) 2011/12/13 19:28:57 is the object behind NOTREACHED an actual iostream
Ami GONE FROM CHROMIUM 2011/12/13 19:30:23 Yes; NOTREACHED is (always) a DCHECK(false) under
+ return false;
+ }
+ if (!CreateD3DDevManager())
+ return false;
+ if (!InitDecoder())
+ return false;
+ if (!GetStreamsInfoAndBufferReqs())
+ return false;
+ if (SendMFTMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)) {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 If you reverse the test (!) you get to: - drop the
ananta 2011/12/13 23:29:15 Done.
+ state_ = DXVAVideoDecodeAccelerator::kNormal;
+ client_->NotifyInitializeDone();
+ return true;
+ }
+ return false;
+}
+
+void DXVAVideoDecodeAccelerator::Decode(
+ const media::BitstreamBuffer& bitstream_buffer) {
+ DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
+
+ if (state_ == DXVAVideoDecodeAccelerator::kUninitialized) {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 What about the other states (resetting/flushing)?
ananta 2011/12/13 23:29:15 Will do that once this change clears up. Fixed thi
+ NOTREACHED() << "ConsumeVideoSample: invalid state";
+ return;
+ }
+
+ HANDLE shared_memory_handle = NULL;
+ if (!::DuplicateHandle(renderer_process_,
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Is this really necessary? I expected the fact tha
apatrick_chromium 2011/12/13 19:51:01 Last time I checked, the IPC system didn't know ho
ananta 2011/12/13 23:29:15 The shared memory handle is not duplicated into th
+ 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,
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Wait, what? This is putting Now() into the decode
ananta 2011/12/13 23:29:15 Removed. I had put that only for debugging i.e. to
+ 400000,
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Where does 400k come from?
ananta 2011/12/13 23:29:15 From thin air. :) Removed this parameter.
+ 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);
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 No error-checking necessary?
ananta 2011/12/13 23:29:15 Done.
+
+ 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;
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 You're draining the decoder after each sample? Th
ananta 2011/12/13 23:29:15 I am not draining the decoder. The state is marked
+ }
+ }
+ input_buffer_frame_times_.push_back(bitstream_buffer.id());
+ DoDecode();
+ client_->NotifyEndOfBitstreamBuffer(bitstream_buffer.id());
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 This is supposed to be called when the buffer has
ananta 2011/12/13 23:29:15 We already handed the decoder the buffer to be dec
+}
+
+void DXVAVideoDecodeAccelerator::AssignPictureBuffers(
+ const std::vector<media::PictureBuffer>& buffers) {
+ DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
+ // 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;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Use the ctor instead (but see below and just drop
ananta 2011/12/13 23:29:15 Done.
+ 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;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 A more efficient way is to replace the above 3 lin
ananta 2011/12/13 23:29:15 The find above is just checking whether the pictur
+ }
+ int buffer_index = 0;
+ PendingOutputSamples::iterator sample_index =
+ pending_output_samples_.begin();
+ HRESULT hr = E_FAIL;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 None of the above 4 lines are used. I would have e
ananta 2011/12/13 23:29:15 Dunno why. These statements were left out by mista
+
+ ProcessPendingSamples();
+}
+
+void DXVAVideoDecodeAccelerator::ReusePictureBuffer(
+ int32 picture_buffer_id) {
+ DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
+
+ DCHECK(available_pictures_.find(picture_buffer_id) !=
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 FWIW, instead of find() != end(), it's shorter to
ananta 2011/12/13 23:29:15 Done.
+ available_pictures_.end());
+ available_pictures_[picture_buffer_id].available = true;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Since you end up needing this several more times i
ananta 2011/12/13 23:29:15 Done.
+
+ PendingOutputSamples::iterator sample_index =
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This would be clearer as if (pending_output_sampl
ananta 2011/12/13 23:29:15 Done.
+ 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);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 pop_front if you take my suggestion above (and I'd
ananta 2011/12/13 23:29:15 Ignoring this as I took your above suggestion to c
+ }
+}
+
+void DXVAVideoDecodeAccelerator::Flush() {
+ DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
+
+ VLOG(1) << "DXVAVideoDecodeAccelerator::Flush";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 FWIW s/VLOG/DVLOG/g everywhere in this CL (see chr
+ if (state_ == DXVAVideoDecodeAccelerator::kUninitialized) {
+ NOTREACHED() << "ConsumeVideoSample: invalid state";
+ client_->NotifyFlushDone();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 It is generally not OK to call PPP_* methods from
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Shouldn't this be a NotifyError(ILLEGAL_STATE)? In
ananta 2011/12/13 23:29:15 Done.
+ return;
+ }
+
+ if (state_ == DXVAVideoDecodeAccelerator::kStopped) {
+ VLOG(1) << "No data available from the decoder";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This doesn't seem to be worthy of a VLOG to me.
ananta 2011/12/13 23:29:15 Rewrote this function. PTAL.
+ client_->NotifyFlushDone();
+ return;
+ }
+
+ state_ = DXVAVideoDecodeAccelerator::kEosDrain;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 qualification unnecessary (here and elsewhere belo
ananta 2011/12/13 23:29:15 Done.
+ if (!SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0)) {
+ LOG(WARNING) << "Failed to send drain message";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Consistency with other MF failures.
ananta 2011/12/13 23:29:15 This function has been rewritten to make it look b
+ state_ = DXVAVideoDecodeAccelerator::kStopped;
+ client_->NotifyFlushDone();
+ return;
+ }
+
+ // As per MSDN docs after the client sends this message, it calls
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 s/this/the above MFT_MESSAGE_COMMAND_DRAIN/ ?
ananta 2011/12/13 23:29:15 Yes.
+ // 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();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 I guess DoDecode() really is synchronous. Are you
ananta 2011/12/13 23:29:15 Worried a little yes. However the MFT decoder uses
+ }
+ client_->NotifyFlushDone();
+}
+
+void DXVAVideoDecodeAccelerator::Reset() {
+ DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
+
+ VLOG(1) << "DXVAVideoDecodeAccelerator::Reset";
+ if (state_ != kNormal && state_ != kStopped) {
+ NOTREACHED() << "Reset: invalid state";
+ client_->NotifyResetDone();
+ return;
+ }
+
+ state_ = DXVAVideoDecodeAccelerator::kResetting;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Whenever you have code like this that checks that
+ if (!SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0)) {
+ LOG(WARNING) << "DXVAVideoDecodeAccelerator::Flush failed to send message";
+ client_->NotifyResetDone();
+ return;
+ }
+ state_ = DXVAVideoDecodeAccelerator::kNormal;
+ client_->NotifyResetDone();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This should be the last call in the method.
ananta 2011/12/13 23:29:15 Done.
+ input_buffer_frame_times_.clear();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 What about the rest of the pending state?
ananta 2011/12/13 23:29:15 Done.
+}
+
+void DXVAVideoDecodeAccelerator::Destroy() {
+ DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
+
+ 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();
+}
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Does nothing need to be done to tear down the deco
+
+bool DXVAVideoDecodeAccelerator::CreateD3DDevManager() {
+ base::win::ScopedComPtr<IDirect3D9Ex> d3d9;
+
+ Direct3DCreate9Ex(D3D_SDK_VERSION, d3d9.Receive());
apatrick_chromium 2011/12/13 19:51:01 If the delay loading of d3d9.dll doesn't work for
+ if (d3d9.get() == NULL) {
+ NOTREACHED() << "Failed to create D3D9";
+ return false;
+ }
+
+ D3DPRESENT_PARAMETERS present_params = {0};
+ present_params.BackBufferWidth = 1;
+ present_params.BackBufferHeight = 1;
+ present_params.BackBufferFormat = D3DFMT_UNKNOWN;
+ present_params.BackBufferCount = 1;
+ present_params.SwapEffect = D3DSWAPEFFECT_DISCARD;
+ present_params.hDeviceWindow = GetShellWindow();
apatrick_chromium 2011/12/13 19:51:01 Question for cpu: what will GetShellWindow() do in
+ present_params.Windowed = TRUE;
+ present_params.Flags = D3DPRESENTFLAG_VIDEO;
+ present_params.FullScreen_RefreshRateInHz = 0;
+ present_params.PresentationInterval = 0;
+
+ HRESULT hr = d3d9->CreateDeviceEx(D3DADAPTER_DEFAULT,
+ D3DDEVTYPE_HAL,
+ GetShellWindow(),
+ D3DCREATE_MIXED_VERTEXPROCESSING,
apatrick_chromium 2011/12/13 19:51:01 I think MIXED won't work on cards that do not supp
+ &present_params,
+ NULL,
+ device_.Receive());
+ if (FAILED(hr)) {
+ NOTREACHED() << "Failed to create D3D Device";
+ return false;
+ }
+
+ 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;
+ base::win::ScopedComPtr<IMFMediaType> out_media_type;
+ bool found = false;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 unused
ananta 2011/12/13 23:29:15 Done.
+ while (SUCCEEDED(decoder_->GetOutputAvailableType(
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 ISTM this would be more natural as a for-loop for
ananta 2011/12/13 23:29:15 Done.
+ 0, i, out_media_type.Receive()))) {
+ 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();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Unnecessary? (here and elsewhere)
ananta 2011/12/13 23:29:15 We have to release the COM interface here inorder
+ continue;
+ }
+ if (out_subtype == subtype) {
+ hr = decoder_->SetOutputType(0, out_media_type, 0); // No flags
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 No checking for failures?
ananta 2011/12/13 23:29:15 Done.
+ hr = MFGetAttributeSize(out_media_type, MF_MT_FRAME_SIZE,
+ reinterpret_cast<UINT32*>(&surface_width_),
+ reinterpret_cast<UINT32*>(&surface_height_));
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 surface_{width,height}_ don't seem to be used anyw
ananta 2011/12/13 23:29:15 Done. PTAL at this function. Rewritten to abort on
+ 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 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.
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 What's the alternative? Can dxva be "disabled" an
ananta 2011/12/13 23:29:15 Unfortunate cut paste. Rephrased the comment.
+bool DXVAVideoDecodeAccelerator::GetStreamsInfoAndBufferReqs() {
+ HRESULT hr = decoder_->GetInputStreamInfo(0, &input_stream_info_);
+ if (FAILED(hr)) {
+ LOG(ERROR) << "Failed to get input stream info";
+ return false;
+ }
+ VLOG(1) << "Input stream info: ";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Between the VLOGs and the error-handling it's real
ananta 2011/12/13 23:29:15 Fixed this function to get the input and output st
+ VLOG(1) << "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)
+ VLOG(1) << "Flags: "
+ << std::hex << std::showbase << input_stream_info_.dwFlags;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Unnecessary since the CHECK_EQ in the next line wi
+ CHECK_EQ(input_stream_info_.dwFlags, 0x7u);
+ VLOG(1) << "Min buffer size: " << input_stream_info_.cbSize;
+ VLOG(1) << "Max lookahead: " << input_stream_info_.cbMaxLookahead;
+ VLOG(1) << "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;
+ }
+ VLOG(1) << "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.
+ VLOG(1) << "Flags: "
+ << std::hex << std::showbase << output_stream_info_.dwFlags;
+ CHECK_EQ(output_stream_info_.dwFlags, 0x107u);
+ VLOG(1) << "Min buffer size: " << output_stream_info_.cbSize;
+ VLOG(1) << "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;
+ }
+ 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));
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This is incorrect in general; prefer the = {0}; st
ananta 2011/12/13 23:29:15 Done.
+ output_data_buffer.pSample = output_sample;
+ status = 0;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 redundant.
ananta 2011/12/13 23:29:15 Done.
+
+ 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) {
+ VLOG(1) << "Got events from ProcessOuput, but discarding";
+ events->Release();
+ }
+ if (FAILED(hr)) {
+ if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
+ hr = SetDecoderOutputMediaType(MFVideoFormat_NV12);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 I don't understand this - the stream changed nothi
ananta 2011/12/13 23:29:15 This is how MSDN mentions that we should handle th
+ 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.
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 DCHECK that we were waiting for this?
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This doesn't actually Notify or stop, it just sign
ananta 2011/12/13 23:29:15 Could you clarify this?.
+ state_ = DXVAVideoDecodeAccelerator::kStopped;
+ return false;
+ } else {
+ NOTREACHED() << "Unhandled error in DoDecode()";
+ state_ = DXVAVideoDecodeAccelerator::kStopped;
+ return false;
+ }
+ }
+
+#if !defined(NDEBUG)
+ VLOG(1) << "Number of input packets before successful decode: "
+ << inputs_before_decode_;
+ inputs_before_decode_ = 0;
+ uint32 end_decode = GetTickCount();
+ VLOG(1) << "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;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 More like error than stopped. NotifyError?
ananta 2011/12/13 23:29:15 Done.
+ return false;
+ }
+
+ input_buffer_frame_times_.clear();
+ state_ = DXVAVideoDecodeAccelerator::kNormal;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Why overwrite the state if we were stopping?
ananta 2011/12/13 23:29:15 When we arrive here we are not stopping as we have
+ return true;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Return value is ignored at all call-sites.
ananta 2011/12/13 23:29:15 Done.
+}
+
+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()) {
+ VLOG(1) << "In input buffers left to process output";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 what?
ananta 2011/12/13 23:29:15 Replaced with No. :(
+ return false;
+ }
+ int32 input_buffer_id =
+ input_buffer_frame_times_[input_buffer_frame_times_.size() - 1];
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 I extra don't understand input_buffer_frame_times_
+ 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)
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 TODOs are usually marked w/ an email address. s/a
Ami GONE FROM CHROMIUM 2011/12/13 20:24:38 oops, I see that's your @chromium address. Disreg
+ // 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();
+ VLOG(1) << "Time to create and copy new surface is "
+ << end_surface_time - start_surface_time;
+#endif // !defined(NDEBUG)
+
+ PendingSampleInfo sample_info;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Give PSI a ctor and use it here? In fact can repla
ananta 2011/12/13 23:29:15 Done.
+
+ 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_) {
+ VLOG(1) << "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;
cpu_(ooo_6.6-7.5) 2011/12/13 19:28:57 so pictures_requested_ does not become false again
ananta 2011/12/13 23:29:15 We only do this upfront. After that we just rotate
+ 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;
+
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 drop \n
ananta 2011/12/13 23:29:15 Done.
+ 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)
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 ditto
ananta 2011/12/13 23:29:15 Done.
+ // 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);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 failure handling
ananta 2011/12/13 23:29:15 Done.
+
+ 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);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 failure handling
ananta 2011/12/13 23:29:15 Done.
+ if (bitmap_info.bmiHeader.biSizeImage <= 0) {
+ NOTREACHED() << "Failed to read bitmap size";
+ return false;
+ }
+ scoped_ptr<char> bits(new char[bitmap_info.bmiHeader.biSizeImage]);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 scoped_array
ananta 2011/12/13 23:29:15 Done.
+ ret = GetDIBits(hdc, bitmap, 0, bitmap_basic_info.bmHeight, bits.get(),
+ &bitmap_info, DIB_RGB_COLORS);
+ DCHECK_GT(ret, 0);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 You seem to have given up on error handling from h
ananta 2011/12/13 23:29:15 Done.
+
+ 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);
+ 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;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Unused.
ananta 2011/12/13 23:29:15 Done.
+
+ while (sample_index != pending_output_samples_.end()) {
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This while smells like a for, but more than that t
ananta 2011/12/13 23:29:15 Done.
+ 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()) {
+ VLOG(1) << "No available picture slots for output";
+ break;
+ }
+ }
+}
+

Powered by Google App Engine
This is Rietveld 408576698