1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/common/gpu/media/dxva_video_decode_accelerator.h"
8 #error This file should only be built on Windows.
9 #endif // !defined(OS_WIN)
15 #include <wmcodecdsp.h>
17 #include "base/bind.h"
18 #include "base/callback.h"
19 #include "base/command_line.h"
20 #include "base/debug/trace_event.h"
21 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/memory/shared_memory.h"
24 #include "base/message_loop/message_loop.h"
25 #include "base/win/windows_version.h"
26 #include "media/video/video_decode_accelerator.h"
27 #include "ui/gl/gl_bindings.h"
28 #include "ui/gl/gl_surface_egl.h"
29 #include "ui/gl/gl_switches.h"
33 // We only request 5 picture buffers from the client which are used to hold the
34 // decoded samples. These buffers are then reused when the client tells us that
35 // it is done with the buffer.
36 static const int kNumPictureBuffers = 5;
38 #define RETURN_ON_FAILURE(result, log, ret) \
46 #define RETURN_ON_HR_FAILURE(result, log, ret) \
47 RETURN_ON_FAILURE(SUCCEEDED(result), \
48 log << ", HRESULT: 0x" << std::hex << result, \
51 #define RETURN_AND_NOTIFY_ON_FAILURE(result, log, error_code, ret) \
55 StopOnError(error_code); \
60 #define RETURN_AND_NOTIFY_ON_HR_FAILURE(result, log, error_code, ret) \
61 RETURN_AND_NOTIFY_ON_FAILURE(SUCCEEDED(result), \
62 log << ", HRESULT: 0x" << std::hex << result, \
65 // Maximum number of iterations we allow before aborting the attempt to flush
66 // the batched queries to the driver and allow torn/corrupt frames to be
68 enum { kMaxIterationsForD3DFlush = 10 };
70 static IMFSample* CreateEmptySample() {
71 base::win::ScopedComPtr<IMFSample> sample;
72 HRESULT hr = MFCreateSample(sample.Receive());
73 RETURN_ON_HR_FAILURE(hr, "MFCreateSample failed", NULL);
74 return sample.Detach();
77 // Creates a Media Foundation sample with one buffer of length |buffer_length|
78 // on a |align|-byte boundary. Alignment must be a perfect power of 2 or 0.
79 static IMFSample* CreateEmptySampleWithBuffer(int buffer_length, int align) {
80 CHECK_GT(buffer_length, 0);
82 base::win::ScopedComPtr<IMFSample> sample;
83 sample.Attach(CreateEmptySample());
85 base::win::ScopedComPtr<IMFMediaBuffer> buffer;
88 // Note that MFCreateMemoryBuffer is same as MFCreateAlignedMemoryBuffer
89 // with the align argument being 0.
90 hr = MFCreateMemoryBuffer(buffer_length, buffer.Receive());
92 hr = MFCreateAlignedMemoryBuffer(buffer_length,
96 RETURN_ON_HR_FAILURE(hr, "Failed to create memory buffer for sample", NULL);
98 hr = sample->AddBuffer(buffer);
99 RETURN_ON_HR_FAILURE(hr, "Failed to add buffer to sample", NULL);
101 return sample.Detach();
104 // Creates a Media Foundation sample with one buffer containing a copy of the
105 // given Annex B stream data.
106 // If duration and sample time are not known, provide 0.
107 // |min_size| specifies the minimum size of the buffer (might be required by
108 // the decoder for input). If no alignment is required, provide 0.
109 static IMFSample* CreateInputSample(const uint8* stream, int size,
110 int min_size, int alignment) {
113 base::win::ScopedComPtr<IMFSample> sample;
114 sample.Attach(CreateEmptySampleWithBuffer(std::max(min_size, size),
116 RETURN_ON_FAILURE(sample, "Failed to create empty sample", NULL);
118 base::win::ScopedComPtr<IMFMediaBuffer> buffer;
119 HRESULT hr = sample->GetBufferByIndex(0, buffer.Receive());
120 RETURN_ON_HR_FAILURE(hr, "Failed to get buffer from sample", NULL);
122 DWORD max_length = 0;
123 DWORD current_length = 0;
124 uint8* destination = NULL;
125 hr = buffer->Lock(&destination, &max_length, ¤t_length);
126 RETURN_ON_HR_FAILURE(hr, "Failed to lock buffer", NULL);
128 CHECK_EQ(current_length, 0u);
129 CHECK_GE(static_cast<int>(max_length), size);
130 memcpy(destination, stream, size);
132 hr = buffer->Unlock();
133 RETURN_ON_HR_FAILURE(hr, "Failed to unlock buffer", NULL);
135 hr = buffer->SetCurrentLength(size);
136 RETURN_ON_HR_FAILURE(hr, "Failed to set buffer length", NULL);
138 return sample.Detach();
141 static IMFSample* CreateSampleFromInputBuffer(
142 const media::BitstreamBuffer& bitstream_buffer,
145 base::SharedMemory shm(bitstream_buffer.handle(), true);
146 RETURN_ON_FAILURE(shm.Map(bitstream_buffer.size()),
147 "Failed in base::SharedMemory::Map", NULL);
149 return CreateInputSample(reinterpret_cast<const uint8*>(shm.memory()),
150 bitstream_buffer.size(),
155 // Maintains information about a DXVA picture buffer, i.e. whether it is
156 // available for rendering, the texture information, etc.
157 struct DXVAVideoDecodeAccelerator::DXVAPictureBuffer {
159 static linked_ptr<DXVAPictureBuffer> Create(
160 const DXVAVideoDecodeAccelerator& decoder,
161 const media::PictureBuffer& buffer,
162 EGLConfig egl_config);
163 ~DXVAPictureBuffer();
165 void ReusePictureBuffer();
166 // Copies the output sample data to the picture buffer provided by the
168 // The dest_surface parameter contains the decoded bits.
169 bool CopyOutputSampleDataToPictureBuffer(
170 const DXVAVideoDecodeAccelerator& decoder,
171 IDirect3DSurface9* dest_surface);
173 bool available() const {
177 void set_available(bool available) {
178 available_ = available;
182 return picture_buffer_.id();
185 gfx::Size size() const {
186 return picture_buffer_.size();
190 explicit DXVAPictureBuffer(const media::PictureBuffer& buffer);
193 media::PictureBuffer picture_buffer_;
194 EGLSurface decoding_surface_;
195 base::win::ScopedComPtr<IDirect3DTexture9> decoding_texture_;
196 // Set to true if RGB is supported by the texture.
200 DISALLOW_COPY_AND_ASSIGN(DXVAPictureBuffer);
204 linked_ptr<DXVAVideoDecodeAccelerator::DXVAPictureBuffer>
205 DXVAVideoDecodeAccelerator::DXVAPictureBuffer::Create(
206 const DXVAVideoDecodeAccelerator& decoder,
207 const media::PictureBuffer& buffer,
208 EGLConfig egl_config) {
209 linked_ptr<DXVAPictureBuffer> picture_buffer(new DXVAPictureBuffer(buffer));
211 EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
214 eglGetConfigAttrib(egl_display, egl_config, EGL_BIND_TO_TEXTURE_RGB,
217 EGLint attrib_list[] = {
218 EGL_WIDTH, buffer.size().width(),
219 EGL_HEIGHT, buffer.size().height(),
220 EGL_TEXTURE_FORMAT, use_rgb ? EGL_TEXTURE_RGB : EGL_TEXTURE_RGBA,
221 EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
225 picture_buffer->decoding_surface_ = eglCreatePbufferSurface(
229 RETURN_ON_FAILURE(picture_buffer->decoding_surface_,
230 "Failed to create surface",
231 linked_ptr<DXVAPictureBuffer>(NULL));
233 HANDLE share_handle = NULL;
234 EGLBoolean ret = eglQuerySurfacePointerANGLE(
236 picture_buffer->decoding_surface_,
237 EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE,
240 RETURN_ON_FAILURE(share_handle && ret == EGL_TRUE,
241 "Failed to query ANGLE surface pointer",
242 linked_ptr<DXVAPictureBuffer>(NULL));
244 HRESULT hr = decoder.device_->CreateTexture(
245 buffer.size().width(),
246 buffer.size().height(),
248 D3DUSAGE_RENDERTARGET,
249 use_rgb ? D3DFMT_X8R8G8B8 : D3DFMT_A8R8G8B8,
251 picture_buffer->decoding_texture_.Receive(),
254 RETURN_ON_HR_FAILURE(hr, "Failed to create texture",
255 linked_ptr<DXVAPictureBuffer>(NULL));
256 picture_buffer->use_rgb_ = !!use_rgb;
257 return picture_buffer;
260 DXVAVideoDecodeAccelerator::DXVAPictureBuffer::DXVAPictureBuffer(
261 const media::PictureBuffer& buffer)
263 picture_buffer_(buffer),
264 decoding_surface_(NULL),
268 DXVAVideoDecodeAccelerator::DXVAPictureBuffer::~DXVAPictureBuffer() {
269 if (decoding_surface_) {
270 EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
280 decoding_surface_ = NULL;
284 void DXVAVideoDecodeAccelerator::DXVAPictureBuffer::ReusePictureBuffer() {
285 DCHECK(decoding_surface_);
286 EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
294 bool DXVAVideoDecodeAccelerator::DXVAPictureBuffer::
295 CopyOutputSampleDataToPictureBuffer(
296 const DXVAVideoDecodeAccelerator& decoder,
297 IDirect3DSurface9* dest_surface) {
298 DCHECK(dest_surface);
300 D3DSURFACE_DESC surface_desc;
301 HRESULT hr = dest_surface->GetDesc(&surface_desc);
302 RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false);
304 D3DSURFACE_DESC texture_desc;
305 decoding_texture_->GetLevelDesc(0, &texture_desc);
307 if (texture_desc.Width != surface_desc.Width ||
308 texture_desc.Height != surface_desc.Height) {
309 NOTREACHED() << "Decode surface of different dimension than texture";
313 hr = decoder.d3d9_->CheckDeviceFormatConversion(
314 D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, surface_desc.Format,
315 use_rgb_ ? D3DFMT_X8R8G8B8 : D3DFMT_A8R8G8B8);
316 RETURN_ON_HR_FAILURE(hr, "Device does not support format converision", false);
318 // This function currently executes in the context of IPC handlers in the
319 // GPU process which ensures that there is always an OpenGL context.
320 GLint current_texture = 0;
321 glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
323 glBindTexture(GL_TEXTURE_2D, picture_buffer_.texture_id());
325 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
327 base::win::ScopedComPtr<IDirect3DSurface9> d3d_surface;
328 hr = decoding_texture_->GetSurfaceLevel(0, d3d_surface.Receive());
329 RETURN_ON_HR_FAILURE(hr, "Failed to get surface from texture", false);
331 hr = decoder.device_->StretchRect(
332 dest_surface, NULL, d3d_surface, NULL, D3DTEXF_NONE);
333 RETURN_ON_HR_FAILURE(hr, "Colorspace conversion via StretchRect failed",
336 // Ideally, this should be done immediately before the draw call that uses
337 // the texture. Flush it once here though.
338 hr = decoder.query_->Issue(D3DISSUE_END);
339 RETURN_ON_HR_FAILURE(hr, "Failed to issue END", false);
341 // The DXVA decoder has its own device which it uses for decoding. ANGLE
342 // has its own device which we don't have access to.
343 // The above code attempts to copy the decoded picture into a surface
344 // which is owned by ANGLE. As there are multiple devices involved in
345 // this, the StretchRect call above is not synchronous.
346 // We attempt to flush the batched operations to ensure that the picture is
347 // copied to the surface owned by ANGLE.
348 // We need to do this in a loop and call flush multiple times.
349 // We have seen the GetData call for flushing the command buffer fail to
350 // return success occassionally on multi core machines, leading to an
352 // Workaround is to have an upper limit of 10 on the number of iterations to
353 // wait for the Flush to finish.
355 while ((decoder.query_->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE) &&
356 ++iterations < kMaxIterationsForD3DFlush) {
357 Sleep(1); // Poor-man's Yield().
359 EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
364 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
365 glBindTexture(GL_TEXTURE_2D, current_texture);
369 DXVAVideoDecodeAccelerator::PendingSampleInfo::PendingSampleInfo(
370 int32 buffer_id, IMFSample* sample)
371 : input_buffer_id(buffer_id) {
372 output_sample.Attach(sample);
375 DXVAVideoDecodeAccelerator::PendingSampleInfo::~PendingSampleInfo() {}
378 bool DXVAVideoDecodeAccelerator::CreateD3DDevManager() {
379 TRACE_EVENT0("gpu", "DXVAVideoDecodeAccelerator_CreateD3DDevManager");
381 HRESULT hr = Direct3DCreate9Ex(D3D_SDK_VERSION, d3d9_.Receive());
382 RETURN_ON_HR_FAILURE(hr, "Direct3DCreate9Ex failed", false);
384 D3DPRESENT_PARAMETERS present_params = {0};
385 present_params.BackBufferWidth = 1;
386 present_params.BackBufferHeight = 1;
387 present_params.BackBufferFormat = D3DFMT_UNKNOWN;
388 present_params.BackBufferCount = 1;
389 present_params.SwapEffect = D3DSWAPEFFECT_DISCARD;
390 present_params.hDeviceWindow = ::GetShellWindow();
391 present_params.Windowed = TRUE;
392 present_params.Flags = D3DPRESENTFLAG_VIDEO;
393 present_params.FullScreen_RefreshRateInHz = 0;
394 present_params.PresentationInterval = 0;
396 hr = d3d9_->CreateDeviceEx(D3DADAPTER_DEFAULT,
399 D3DCREATE_FPU_PRESERVE |
400 D3DCREATE_SOFTWARE_VERTEXPROCESSING |
401 D3DCREATE_DISABLE_PSGP_THREADING |
402 D3DCREATE_MULTITHREADED,
406 RETURN_ON_HR_FAILURE(hr, "Failed to create D3D device", false);
408 hr = DXVA2CreateDirect3DDeviceManager9(&dev_manager_reset_token_,
409 device_manager_.Receive());
410 RETURN_ON_HR_FAILURE(hr, "DXVA2CreateDirect3DDeviceManager9 failed", false);
412 hr = device_manager_->ResetDevice(device_, dev_manager_reset_token_);
413 RETURN_ON_HR_FAILURE(hr, "Failed to reset device", false);
415 hr = device_->CreateQuery(D3DQUERYTYPE_EVENT, query_.Receive());
416 RETURN_ON_HR_FAILURE(hr, "Failed to create D3D device query", false);
417 // Ensure query_ API works (to avoid an infinite loop later in
418 // CopyOutputSampleDataToPictureBuffer).
419 hr = query_->Issue(D3DISSUE_END);
420 RETURN_ON_HR_FAILURE(hr, "Failed to issue END test query", false);
424 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator(
425 const base::Callback<bool(void)>& make_context_current)
427 dev_manager_reset_token_(0),
429 state_(kUninitialized),
430 pictures_requested_(false),
431 inputs_before_decode_(0),
432 make_context_current_(make_context_current),
433 weak_this_factory_(this) {
434 memset(&input_stream_info_, 0, sizeof(input_stream_info_));
435 memset(&output_stream_info_, 0, sizeof(output_stream_info_));
438 DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() {
442 bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile,
444 DCHECK(CalledOnValidThread());
448 // Not all versions of Windows 7 and later include Media Foundation DLLs.
449 // Instead of crashing while delay loading the DLL when calling MFStartup()
450 // below, probe whether we can successfully load the DLL now.
452 // See http://crbug.com/339678 for details.
453 HMODULE mfplat_dll = ::LoadLibrary(L"MFPlat.dll");
454 RETURN_ON_FAILURE(mfplat_dll, "MFPlat.dll is required for decoding", false);
457 // H264PROFILE_HIGH video decoding is janky at times. Needs more
459 if (profile != media::H264PROFILE_BASELINE &&
460 profile != media::H264PROFILE_MAIN &&
461 profile != media::H264PROFILE_HIGH) {
462 RETURN_AND_NOTIFY_ON_FAILURE(false,
463 "Unsupported h264 profile", PLATFORM_FAILURE, false);
466 RETURN_AND_NOTIFY_ON_FAILURE(
467 gfx::g_driver_egl.ext.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle,
468 "EGL_ANGLE_surface_d3d_texture_2d_share_handle unavailable",
472 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kUninitialized),
473 "Initialize: invalid state: " << state_, ILLEGAL_STATE, false);
475 HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL);
476 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "MFStartup failed.", PLATFORM_FAILURE,
479 RETURN_AND_NOTIFY_ON_FAILURE(CreateD3DDevManager(),
480 "Failed to initialize D3D device and manager",
484 RETURN_AND_NOTIFY_ON_FAILURE(InitDecoder(profile),
485 "Failed to initialize decoder", PLATFORM_FAILURE, false);
487 RETURN_AND_NOTIFY_ON_FAILURE(GetStreamsInfoAndBufferReqs(),
488 "Failed to get input/output stream info.", PLATFORM_FAILURE, false);
490 RETURN_AND_NOTIFY_ON_FAILURE(
491 SendMFTMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0),
492 "Send MFT_MESSAGE_NOTIFY_BEGIN_STREAMING notification failed",
493 PLATFORM_FAILURE, false);
495 RETURN_AND_NOTIFY_ON_FAILURE(
496 SendMFTMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0),
497 "Send MFT_MESSAGE_NOTIFY_START_OF_STREAM notification failed",
498 PLATFORM_FAILURE, false);
504 void DXVAVideoDecodeAccelerator::Decode(
505 const media::BitstreamBuffer& bitstream_buffer) {
506 DCHECK(CalledOnValidThread());
508 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped ||
509 state_ == kFlushing),
510 "Invalid state: " << state_, ILLEGAL_STATE,);
512 base::win::ScopedComPtr<IMFSample> sample;
513 sample.Attach(CreateSampleFromInputBuffer(bitstream_buffer,
514 input_stream_info_.cbSize,
515 input_stream_info_.cbAlignment));
516 RETURN_AND_NOTIFY_ON_FAILURE(sample, "Failed to create input sample",
519 RETURN_AND_NOTIFY_ON_HR_FAILURE(sample->SetSampleTime(bitstream_buffer.id()),
520 "Failed to associate input buffer id with sample", PLATFORM_FAILURE,);
522 DecodeInternal(sample);
525 void DXVAVideoDecodeAccelerator::AssignPictureBuffers(
526 const std::vector<media::PictureBuffer>& buffers) {
527 DCHECK(CalledOnValidThread());
529 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized),
530 "Invalid state: " << state_, ILLEGAL_STATE,);
531 RETURN_AND_NOTIFY_ON_FAILURE((kNumPictureBuffers == buffers.size()),
532 "Failed to provide requested picture buffers. (Got " << buffers.size() <<
533 ", requested " << kNumPictureBuffers << ")", INVALID_ARGUMENT,);
535 // Copy the picture buffers provided by the client to the available list,
536 // and mark these buffers as available for use.
537 for (size_t buffer_index = 0; buffer_index < buffers.size();
539 linked_ptr<DXVAPictureBuffer> picture_buffer =
540 DXVAPictureBuffer::Create(*this, buffers[buffer_index], egl_config_);
541 RETURN_AND_NOTIFY_ON_FAILURE(picture_buffer.get(),
542 "Failed to allocate picture buffer", PLATFORM_FAILURE,);
544 bool inserted = output_picture_buffers_.insert(std::make_pair(
545 buffers[buffer_index].id(), picture_buffer)).second;
548 ProcessPendingSamples();
549 if (state_ == kFlushing && pending_output_samples_.empty())
553 void DXVAVideoDecodeAccelerator::ReusePictureBuffer(
554 int32 picture_buffer_id) {
555 DCHECK(CalledOnValidThread());
557 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized),
558 "Invalid state: " << state_, ILLEGAL_STATE,);
560 if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty())
563 OutputBuffers::iterator it = output_picture_buffers_.find(picture_buffer_id);
564 // If we didn't find the picture id in the |output_picture_buffers_| map we
565 // try the |stale_output_picture_buffers_| map, as this may have been an
566 // output picture buffer from before a resolution change, that at resolution
567 // change time had yet to be displayed. The client is calling us back to tell
568 // us that we can now recycle this picture buffer, so if we were waiting to
569 // dispose of it we now can.
570 if (it == output_picture_buffers_.end()) {
571 it = stale_output_picture_buffers_.find(picture_buffer_id);
572 RETURN_AND_NOTIFY_ON_FAILURE(it != stale_output_picture_buffers_.end(),
573 "Invalid picture id: " << picture_buffer_id, INVALID_ARGUMENT,);
574 base::MessageLoop::current()->PostTask(FROM_HERE,
575 base::Bind(&DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer,
576 weak_this_factory_.GetWeakPtr(), picture_buffer_id));
580 it->second->ReusePictureBuffer();
581 ProcessPendingSamples();
583 if (state_ == kFlushing && pending_output_samples_.empty())
587 void DXVAVideoDecodeAccelerator::Flush() {
588 DCHECK(CalledOnValidThread());
590 DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush";
592 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped),
593 "Unexpected decoder state: " << state_, ILLEGAL_STATE,);
597 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0),
598 "Failed to send drain message", PLATFORM_FAILURE,);
600 if (!pending_output_samples_.empty())
606 void DXVAVideoDecodeAccelerator::Reset() {
607 DCHECK(CalledOnValidThread());
609 DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset";
611 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped),
612 "Reset: invalid state: " << state_, ILLEGAL_STATE,);
616 pending_output_samples_.clear();
618 NotifyInputBuffersDropped();
620 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0),
621 "Reset: Failed to send message.", PLATFORM_FAILURE,);
623 base::MessageLoop::current()->PostTask(
625 base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone,
626 weak_this_factory_.GetWeakPtr()));
628 state_ = DXVAVideoDecodeAccelerator::kNormal;
631 void DXVAVideoDecodeAccelerator::Destroy() {
632 DCHECK(CalledOnValidThread());
637 bool DXVAVideoDecodeAccelerator::CanDecodeOnIOThread() {
641 bool DXVAVideoDecodeAccelerator::InitDecoder(media::VideoCodecProfile profile) {
642 if (profile < media::H264PROFILE_MIN || profile > media::H264PROFILE_MAX)
645 // We mimic the steps CoCreateInstance uses to instantiate the object. This
646 // was previously done because it failed inside the sandbox, and now is done
647 // as a more minimal approach to avoid other side-effects CCI might have (as
648 // we are still in a reduced sandbox).
649 HMODULE decoder_dll = ::LoadLibrary(L"msmpeg2vdec.dll");
650 RETURN_ON_FAILURE(decoder_dll,
651 "msmpeg2vdec.dll required for decoding is not loaded",
654 typedef HRESULT(WINAPI * GetClassObject)(
655 const CLSID & clsid, const IID & iid, void * *object);
657 GetClassObject get_class_object = reinterpret_cast<GetClassObject>(
658 GetProcAddress(decoder_dll, "DllGetClassObject"));
660 get_class_object, "Failed to get DllGetClassObject pointer", false);
662 base::win::ScopedComPtr<IClassFactory> factory;
663 HRESULT hr = get_class_object(__uuidof(CMSH264DecoderMFT),
664 __uuidof(IClassFactory),
665 reinterpret_cast<void**>(factory.Receive()));
666 RETURN_ON_HR_FAILURE(hr, "DllGetClassObject for decoder failed", false);
668 hr = factory->CreateInstance(NULL,
669 __uuidof(IMFTransform),
670 reinterpret_cast<void**>(decoder_.Receive()));
671 RETURN_ON_HR_FAILURE(hr, "Failed to create decoder instance", false);
673 RETURN_ON_FAILURE(CheckDecoderDxvaSupport(),
674 "Failed to check decoder DXVA support", false);
676 hr = decoder_->ProcessMessage(
677 MFT_MESSAGE_SET_D3D_MANAGER,
678 reinterpret_cast<ULONG_PTR>(device_manager_.get()));
679 RETURN_ON_HR_FAILURE(hr, "Failed to pass D3D manager to decoder", false);
681 EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
683 EGLint config_attribs[] = {
688 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
695 if (!eglChooseConfig(
703 return SetDecoderMediaTypes();
706 bool DXVAVideoDecodeAccelerator::CheckDecoderDxvaSupport() {
707 base::win::ScopedComPtr<IMFAttributes> attributes;
708 HRESULT hr = decoder_->GetAttributes(attributes.Receive());
709 RETURN_ON_HR_FAILURE(hr, "Failed to get decoder attributes", false);
712 hr = attributes->GetUINT32(MF_SA_D3D_AWARE, &dxva);
713 RETURN_ON_HR_FAILURE(hr, "Failed to check if decoder supports DXVA", false);
715 hr = attributes->SetUINT32(CODECAPI_AVDecVideoAcceleration_H264, TRUE);
716 RETURN_ON_HR_FAILURE(hr, "Failed to enable DXVA H/W decoding", false);
720 bool DXVAVideoDecodeAccelerator::SetDecoderMediaTypes() {
721 RETURN_ON_FAILURE(SetDecoderInputMediaType(),
722 "Failed to set decoder input media type", false);
723 return SetDecoderOutputMediaType(MFVideoFormat_NV12);
726 bool DXVAVideoDecodeAccelerator::SetDecoderInputMediaType() {
727 base::win::ScopedComPtr<IMFMediaType> media_type;
728 HRESULT hr = MFCreateMediaType(media_type.Receive());
729 RETURN_ON_HR_FAILURE(hr, "MFCreateMediaType failed", false);
731 hr = media_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
732 RETURN_ON_HR_FAILURE(hr, "Failed to set major input type", false);
734 hr = media_type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_H264);
735 RETURN_ON_HR_FAILURE(hr, "Failed to set subtype", false);
737 // Not sure about this. msdn recommends setting this value on the input
739 hr = media_type->SetUINT32(MF_MT_INTERLACE_MODE,
740 MFVideoInterlace_MixedInterlaceOrProgressive);
741 RETURN_ON_HR_FAILURE(hr, "Failed to set interlace mode", false);
743 hr = decoder_->SetInputType(0, media_type, 0); // No flags
744 RETURN_ON_HR_FAILURE(hr, "Failed to set decoder input type", false);
748 bool DXVAVideoDecodeAccelerator::SetDecoderOutputMediaType(
749 const GUID& subtype) {
750 base::win::ScopedComPtr<IMFMediaType> out_media_type;
753 SUCCEEDED(decoder_->GetOutputAvailableType(0, i,
754 out_media_type.Receive()));
756 GUID out_subtype = {0};
757 HRESULT hr = out_media_type->GetGUID(MF_MT_SUBTYPE, &out_subtype);
758 RETURN_ON_HR_FAILURE(hr, "Failed to get output major type", false);
760 if (out_subtype == subtype) {
761 hr = decoder_->SetOutputType(0, out_media_type, 0); // No flags
762 RETURN_ON_HR_FAILURE(hr, "Failed to set decoder output type", false);
765 out_media_type.Release();
770 bool DXVAVideoDecodeAccelerator::SendMFTMessage(MFT_MESSAGE_TYPE msg,
772 HRESULT hr = decoder_->ProcessMessage(msg, param);
773 return SUCCEEDED(hr);
776 // Gets the minimum buffer sizes for input and output samples. The MFT will not
777 // allocate buffer for input nor output, so we have to do it ourselves and make
778 // sure they're the correct size. We only provide decoding if DXVA is enabled.
779 bool DXVAVideoDecodeAccelerator::GetStreamsInfoAndBufferReqs() {
780 HRESULT hr = decoder_->GetInputStreamInfo(0, &input_stream_info_);
781 RETURN_ON_HR_FAILURE(hr, "Failed to get input stream info", false);
783 hr = decoder_->GetOutputStreamInfo(0, &output_stream_info_);
784 RETURN_ON_HR_FAILURE(hr, "Failed to get decoder output stream info", false);
786 DVLOG(1) << "Input stream info: ";
787 DVLOG(1) << "Max latency: " << input_stream_info_.hnsMaxLatency;
788 // There should be three flags, one for requiring a whole frame be in a
789 // single sample, one for requiring there be one buffer only in a single
790 // sample, and one that specifies a fixed sample size. (as in cbSize)
791 CHECK_EQ(input_stream_info_.dwFlags, 0x7u);
793 DVLOG(1) << "Min buffer size: " << input_stream_info_.cbSize;
794 DVLOG(1) << "Max lookahead: " << input_stream_info_.cbMaxLookahead;
795 DVLOG(1) << "Alignment: " << input_stream_info_.cbAlignment;
797 DVLOG(1) << "Output stream info: ";
798 // The flags here should be the same and mean the same thing, except when
799 // DXVA is enabled, there is an extra 0x100 flag meaning decoder will
800 // allocate its own sample.
801 DVLOG(1) << "Flags: "
802 << std::hex << std::showbase << output_stream_info_.dwFlags;
803 CHECK_EQ(output_stream_info_.dwFlags, 0x107u);
804 DVLOG(1) << "Min buffer size: " << output_stream_info_.cbSize;
805 DVLOG(1) << "Alignment: " << output_stream_info_.cbAlignment;
809 void DXVAVideoDecodeAccelerator::DoDecode() {
810 // This function is also called from FlushInternal in a loop which could
811 // result in the state transitioning to kStopped due to no decoded output.
812 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kFlushing ||
814 "DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,);
816 MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0};
819 HRESULT hr = decoder_->ProcessOutput(0, // No flags
820 1, // # of out streams to pull from
823 IMFCollection* events = output_data_buffer.pEvents;
824 if (events != NULL) {
825 VLOG(1) << "Got events from ProcessOuput, but discarding";
829 // A stream change needs further ProcessInput calls to get back decoder
830 // output which is why we need to set the state to stopped.
831 if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
832 if (!SetDecoderOutputMediaType(MFVideoFormat_NV12)) {
833 // Decoder didn't let us set NV12 output format. Not sure as to why
834 // this can happen. Give up in disgust.
835 NOTREACHED() << "Failed to set decoder output media type to NV12";
838 DVLOG(1) << "Received output format change from the decoder."
839 " Recursively invoking DoDecode";
843 } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
844 // No more output from the decoder. Stop playback.
848 NOTREACHED() << "Unhandled error in DoDecode()";
852 TRACE_EVENT_END_ETW("DXVAVideoDecodeAccelerator.Decoding", this, "");
854 TRACE_COUNTER1("DXVA Decoding", "TotalPacketsBeforeDecode",
855 inputs_before_decode_);
857 inputs_before_decode_ = 0;
859 RETURN_AND_NOTIFY_ON_FAILURE(ProcessOutputSample(output_data_buffer.pSample),
860 "Failed to process output sample.", PLATFORM_FAILURE,);
863 bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) {
864 RETURN_ON_FAILURE(sample, "Decode succeeded with NULL output sample", false);
866 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer;
867 HRESULT hr = sample->GetBufferByIndex(0, output_buffer.Receive());
868 RETURN_ON_HR_FAILURE(hr, "Failed to get buffer from output sample", false);
870 base::win::ScopedComPtr<IDirect3DSurface9> surface;
871 hr = MFGetService(output_buffer, MR_BUFFER_SERVICE,
872 IID_PPV_ARGS(surface.Receive()));
873 RETURN_ON_HR_FAILURE(hr, "Failed to get D3D surface from output sample",
876 LONGLONG input_buffer_id = 0;
877 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id),
878 "Failed to get input buffer id associated with sample",
881 pending_output_samples_.push_back(
882 PendingSampleInfo(input_buffer_id, sample));
884 // If we have available picture buffers to copy the output data then use the
885 // first one and then flag it as not being available for use.
886 if (output_picture_buffers_.size()) {
887 ProcessPendingSamples();
890 if (pictures_requested_) {
891 DVLOG(1) << "Waiting for picture slots from the client.";
895 // We only read the surface description, which contains its width/height when
896 // we need the picture buffers from the client. Once we have those, then they
898 D3DSURFACE_DESC surface_desc;
899 hr = surface->GetDesc(&surface_desc);
900 RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false);
902 // Go ahead and request picture buffers.
903 base::MessageLoop::current()->PostTask(
905 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers,
906 weak_this_factory_.GetWeakPtr(),
908 surface_desc.Height));
910 pictures_requested_ = true;
914 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() {
915 RETURN_AND_NOTIFY_ON_FAILURE(make_context_current_.Run(),
916 "Failed to make context current", PLATFORM_FAILURE,);
918 OutputBuffers::iterator index;
920 for (index = output_picture_buffers_.begin();
921 index != output_picture_buffers_.end() &&
922 !pending_output_samples_.empty();
924 if (index->second->available()) {
925 PendingSampleInfo sample_info = pending_output_samples_.front();
927 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer;
928 HRESULT hr = sample_info.output_sample->GetBufferByIndex(
929 0, output_buffer.Receive());
930 RETURN_AND_NOTIFY_ON_HR_FAILURE(
931 hr, "Failed to get buffer from output sample", PLATFORM_FAILURE,);
933 base::win::ScopedComPtr<IDirect3DSurface9> surface;
934 hr = MFGetService(output_buffer, MR_BUFFER_SERVICE,
935 IID_PPV_ARGS(surface.Receive()));
936 RETURN_AND_NOTIFY_ON_HR_FAILURE(
937 hr, "Failed to get D3D surface from output sample",
940 D3DSURFACE_DESC surface_desc;
941 hr = surface->GetDesc(&surface_desc);
942 RETURN_AND_NOTIFY_ON_HR_FAILURE(
943 hr, "Failed to get surface description", PLATFORM_FAILURE,);
945 if (surface_desc.Width !=
946 static_cast<uint32>(index->second->size().width()) ||
947 surface_desc.Height !=
948 static_cast<uint32>(index->second->size().height())) {
949 HandleResolutionChanged(surface_desc.Width, surface_desc.Height);
953 RETURN_AND_NOTIFY_ON_FAILURE(
954 index->second->CopyOutputSampleDataToPictureBuffer(*this, surface),
955 "Failed to copy output sample",
958 media::Picture output_picture(index->second->id(),
959 sample_info.input_buffer_id,
960 gfx::Rect(index->second->size()));
961 base::MessageLoop::current()->PostTask(
963 base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady,
964 weak_this_factory_.GetWeakPtr(),
967 index->second->set_available(false);
968 pending_output_samples_.pop_front();
972 if (!pending_input_buffers_.empty() && pending_output_samples_.empty()) {
973 base::MessageLoop::current()->PostTask(
975 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
976 weak_this_factory_.GetWeakPtr()));
980 void DXVAVideoDecodeAccelerator::StopOnError(
981 media::VideoDecodeAccelerator::Error error) {
982 DCHECK(CalledOnValidThread());
985 client_->NotifyError(error);
988 if (state_ != kUninitialized) {
993 void DXVAVideoDecodeAccelerator::Invalidate() {
994 if (state_ == kUninitialized)
996 weak_this_factory_.InvalidateWeakPtrs();
997 output_picture_buffers_.clear();
998 stale_output_picture_buffers_.clear();
999 pending_output_samples_.clear();
1000 pending_input_buffers_.clear();
1003 state_ = kUninitialized;
1006 void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) {
1008 client_->NotifyEndOfBitstreamBuffer(input_buffer_id);
1011 void DXVAVideoDecodeAccelerator::NotifyFlushDone() {
1013 client_->NotifyFlushDone();
1016 void DXVAVideoDecodeAccelerator::NotifyResetDone() {
1018 client_->NotifyResetDone();
1021 void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) {
1022 // This task could execute after the decoder has been torn down.
1023 if (state_ != kUninitialized && client_) {
1024 client_->ProvidePictureBuffers(
1026 gfx::Size(width, height),
1031 void DXVAVideoDecodeAccelerator::NotifyPictureReady(
1032 const media::Picture& picture) {
1033 // This task could execute after the decoder has been torn down.
1034 if (state_ != kUninitialized && client_)
1035 client_->PictureReady(picture);
1038 void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() {
1039 if (!client_ || !pending_output_samples_.empty())
1042 for (PendingInputs::iterator it = pending_input_buffers_.begin();
1043 it != pending_input_buffers_.end(); ++it) {
1044 LONGLONG input_buffer_id = 0;
1045 RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id),
1046 "Failed to get buffer id associated with sample",);
1047 client_->NotifyEndOfBitstreamBuffer(input_buffer_id);
1049 pending_input_buffers_.clear();
1052 void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() {
1053 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized),
1054 "Invalid state: " << state_, ILLEGAL_STATE,);
1056 if (pending_input_buffers_.empty() || !pending_output_samples_.empty())
1059 PendingInputs pending_input_buffers_copy;
1060 std::swap(pending_input_buffers_, pending_input_buffers_copy);
1062 for (PendingInputs::iterator it = pending_input_buffers_copy.begin();
1063 it != pending_input_buffers_copy.end(); ++it) {
1064 DecodeInternal(*it);
1068 void DXVAVideoDecodeAccelerator::FlushInternal() {
1069 // The DoDecode function sets the state to kStopped when the decoder returns
1070 // MF_E_TRANSFORM_NEED_MORE_INPUT.
1071 // The MFT decoder can buffer upto 30 frames worth of input before returning
1072 // an output frame. This loop here attempts to retrieve as many output frames
1073 // as possible from the buffered set.
1074 while (state_ != kStopped) {
1076 if (!pending_output_samples_.empty())
1080 base::MessageLoop::current()->PostTask(
1082 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone,
1083 weak_this_factory_.GetWeakPtr()));
1088 void DXVAVideoDecodeAccelerator::DecodeInternal(
1089 const base::win::ScopedComPtr<IMFSample>& sample) {
1090 DCHECK(CalledOnValidThread());
1092 if (state_ == kUninitialized)
1095 if (!pending_output_samples_.empty() || !pending_input_buffers_.empty()) {
1096 pending_input_buffers_.push_back(sample);
1100 if (!inputs_before_decode_) {
1101 TRACE_EVENT_BEGIN_ETW("DXVAVideoDecodeAccelerator.Decoding", this, "");
1103 inputs_before_decode_++;
1105 HRESULT hr = decoder_->ProcessInput(0, sample, 0);
1106 // As per msdn if the decoder returns MF_E_NOTACCEPTING then it means that it
1107 // has enough data to produce one or more output samples. In this case the
1108 // recommended options are to
1109 // 1. Generate new output by calling IMFTransform::ProcessOutput until it
1110 // returns MF_E_TRANSFORM_NEED_MORE_INPUT.
1111 // 2. Flush the input data
1112 // We implement the first option, i.e to retrieve the output sample and then
1113 // process the input again. Failure in either of these steps is treated as a
1115 if (hr == MF_E_NOTACCEPTING) {
1117 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal),
1118 "Failed to process output. Unexpected decoder state: " << state_,
1120 hr = decoder_->ProcessInput(0, sample, 0);
1121 // If we continue to get the MF_E_NOTACCEPTING error we do the following:-
1122 // 1. Add the input sample to the pending queue.
1123 // 2. If we don't have any output samples we post the
1124 // DecodePendingInputBuffers task to process the pending input samples.
1125 // If we have an output sample then the above task is posted when the
1126 // output samples are sent to the client.
1127 // This is because we only support 1 pending output sample at any
1128 // given time due to the limitation with the Microsoft media foundation
1129 // decoder where it recycles the output Decoder surfaces.
1130 if (hr == MF_E_NOTACCEPTING) {
1131 pending_input_buffers_.push_back(sample);
1132 if (pending_output_samples_.empty()) {
1133 base::MessageLoop::current()->PostTask(
1135 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1136 weak_this_factory_.GetWeakPtr()));
1141 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "Failed to process input sample",
1146 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal),
1147 "Failed to process output. Unexpected decoder state: " << state_,
1150 LONGLONG input_buffer_id = 0;
1151 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id),
1152 "Failed to get input buffer id associated with sample",);
1153 // The Microsoft Media foundation decoder internally buffers up to 30 frames
1154 // before returning a decoded frame. We need to inform the client that this
1155 // input buffer is processed as it may stop sending us further input.
1156 // Note: This may break clients which expect every input buffer to be
1157 // associated with a decoded output buffer.
1159 // Do some more investigation into whether it is possible to get the MFT
1160 // decoder to emit an output packet for every input packet.
1161 // http://code.google.com/p/chromium/issues/detail?id=108121
1162 // http://code.google.com/p/chromium/issues/detail?id=150925
1163 base::MessageLoop::current()->PostTask(
1165 base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead,
1166 weak_this_factory_.GetWeakPtr(),
1170 void DXVAVideoDecodeAccelerator::HandleResolutionChanged(int width,
1172 base::MessageLoop::current()->PostTask(
1174 base::Bind(&DXVAVideoDecodeAccelerator::DismissStaleBuffers,
1175 weak_this_factory_.GetWeakPtr()));
1177 base::MessageLoop::current()->PostTask(
1179 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers,
1180 weak_this_factory_.GetWeakPtr(),
1185 void DXVAVideoDecodeAccelerator::DismissStaleBuffers() {
1186 OutputBuffers::iterator index;
1188 for (index = output_picture_buffers_.begin();
1189 index != output_picture_buffers_.end();
1191 if (index->second->available()) {
1192 DVLOG(1) << "Dismissing picture id: " << index->second->id();
1193 client_->DismissPictureBuffer(index->second->id());
1195 // Move to |stale_output_picture_buffers_| for deferred deletion.
1196 stale_output_picture_buffers_.insert(
1197 std::make_pair(index->first, index->second));
1201 output_picture_buffers_.clear();
1204 void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer(
1205 int32 picture_buffer_id) {
1206 OutputBuffers::iterator it = stale_output_picture_buffers_.find(
1208 DCHECK(it != stale_output_picture_buffers_.end());
1209 DVLOG(1) << "Dismissing picture id: " << it->second->id();
1210 client_->DismissPictureBuffer(it->second->id());
1211 stale_output_picture_buffers_.erase(it);
1214 } // namespace content