Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / dxva_video_decode_accelerator.cc
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.
4
5 #include "content/common/gpu/media/dxva_video_decode_accelerator.h"
6
7 #if !defined(OS_WIN)
8 #error This file should only be built on Windows.
9 #endif   // !defined(OS_WIN)
10
11 #include <ks.h>
12 #include <codecapi.h>
13 #include <mfapi.h>
14 #include <mferror.h>
15 #include <wmcodecdsp.h>
16
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"
30
31 namespace content {
32
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;
37
38 #define RETURN_ON_FAILURE(result, log, ret)  \
39   do {                                       \
40     if (!(result)) {                         \
41       DLOG(ERROR) << log;                    \
42       return ret;                            \
43     }                                        \
44   } while (0)
45
46 #define RETURN_ON_HR_FAILURE(result, log, ret)                    \
47   RETURN_ON_FAILURE(SUCCEEDED(result),                            \
48                     log << ", HRESULT: 0x" << std::hex << result, \
49                     ret);
50
51 #define RETURN_AND_NOTIFY_ON_FAILURE(result, log, error_code, ret)  \
52   do {                                                              \
53     if (!(result)) {                                                \
54       DVLOG(1) << log;                                              \
55       StopOnError(error_code);                                      \
56       return ret;                                                   \
57     }                                                               \
58   } while (0)
59
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, \
63                                error_code, ret);
64
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
67 // rendered.
68 enum { kMaxIterationsForD3DFlush = 10 };
69
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();
75 }
76
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);
81
82   base::win::ScopedComPtr<IMFSample> sample;
83   sample.Attach(CreateEmptySample());
84
85   base::win::ScopedComPtr<IMFMediaBuffer> buffer;
86   HRESULT hr = E_FAIL;
87   if (align == 0) {
88     // Note that MFCreateMemoryBuffer is same as MFCreateAlignedMemoryBuffer
89     // with the align argument being 0.
90     hr = MFCreateMemoryBuffer(buffer_length, buffer.Receive());
91   } else {
92     hr = MFCreateAlignedMemoryBuffer(buffer_length,
93                                      align - 1,
94                                      buffer.Receive());
95   }
96   RETURN_ON_HR_FAILURE(hr, "Failed to create memory buffer for sample", NULL);
97
98   hr = sample->AddBuffer(buffer);
99   RETURN_ON_HR_FAILURE(hr, "Failed to add buffer to sample", NULL);
100
101   return sample.Detach();
102 }
103
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) {
111   CHECK(stream);
112   CHECK_GT(size, 0);
113   base::win::ScopedComPtr<IMFSample> sample;
114   sample.Attach(CreateEmptySampleWithBuffer(std::max(min_size, size),
115                                             alignment));
116   RETURN_ON_FAILURE(sample, "Failed to create empty sample", NULL);
117
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);
121
122   DWORD max_length = 0;
123   DWORD current_length = 0;
124   uint8* destination = NULL;
125   hr = buffer->Lock(&destination, &max_length, &current_length);
126   RETURN_ON_HR_FAILURE(hr, "Failed to lock buffer", NULL);
127
128   CHECK_EQ(current_length, 0u);
129   CHECK_GE(static_cast<int>(max_length), size);
130   memcpy(destination, stream, size);
131
132   hr = buffer->Unlock();
133   RETURN_ON_HR_FAILURE(hr, "Failed to unlock buffer", NULL);
134
135   hr = buffer->SetCurrentLength(size);
136   RETURN_ON_HR_FAILURE(hr, "Failed to set buffer length", NULL);
137
138   return sample.Detach();
139 }
140
141 static IMFSample* CreateSampleFromInputBuffer(
142     const media::BitstreamBuffer& bitstream_buffer,
143     DWORD stream_size,
144     DWORD alignment) {
145   base::SharedMemory shm(bitstream_buffer.handle(), true);
146   RETURN_ON_FAILURE(shm.Map(bitstream_buffer.size()),
147                     "Failed in base::SharedMemory::Map", NULL);
148
149   return CreateInputSample(reinterpret_cast<const uint8*>(shm.memory()),
150                            bitstream_buffer.size(),
151                            stream_size,
152                            alignment);
153 }
154
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 {
158  public:
159   static linked_ptr<DXVAPictureBuffer> Create(
160       const DXVAVideoDecodeAccelerator& decoder,
161       const media::PictureBuffer& buffer,
162       EGLConfig egl_config);
163   ~DXVAPictureBuffer();
164
165   void ReusePictureBuffer();
166   // Copies the output sample data to the picture buffer provided by the
167   // client.
168   // The dest_surface parameter contains the decoded bits.
169   bool CopyOutputSampleDataToPictureBuffer(
170       const DXVAVideoDecodeAccelerator& decoder,
171       IDirect3DSurface9* dest_surface);
172
173   bool available() const {
174     return available_;
175   }
176
177   void set_available(bool available) {
178     available_ = available;
179   }
180
181   int id() const {
182     return picture_buffer_.id();
183   }
184
185   gfx::Size size() const {
186     return picture_buffer_.size();
187   }
188
189  private:
190   explicit DXVAPictureBuffer(const media::PictureBuffer& buffer);
191
192   bool available_;
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.
197   // Defaults to true.
198   bool use_rgb_;
199
200   DISALLOW_COPY_AND_ASSIGN(DXVAPictureBuffer);
201 };
202
203 // static
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));
210
211   EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
212
213   EGLint use_rgb = 1;
214   eglGetConfigAttrib(egl_display, egl_config, EGL_BIND_TO_TEXTURE_RGB,
215                      &use_rgb);
216
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,
222     EGL_NONE
223   };
224
225   picture_buffer->decoding_surface_ = eglCreatePbufferSurface(
226       egl_display,
227       egl_config,
228       attrib_list);
229   RETURN_ON_FAILURE(picture_buffer->decoding_surface_,
230                     "Failed to create surface",
231                     linked_ptr<DXVAPictureBuffer>(NULL));
232
233   HANDLE share_handle = NULL;
234   EGLBoolean ret = eglQuerySurfacePointerANGLE(
235       egl_display,
236       picture_buffer->decoding_surface_,
237       EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE,
238       &share_handle);
239
240   RETURN_ON_FAILURE(share_handle && ret == EGL_TRUE,
241                     "Failed to query ANGLE surface pointer",
242                     linked_ptr<DXVAPictureBuffer>(NULL));
243
244   HRESULT hr = decoder.device_->CreateTexture(
245       buffer.size().width(),
246       buffer.size().height(),
247       1,
248       D3DUSAGE_RENDERTARGET,
249       use_rgb ? D3DFMT_X8R8G8B8 : D3DFMT_A8R8G8B8,
250       D3DPOOL_DEFAULT,
251       picture_buffer->decoding_texture_.Receive(),
252       &share_handle);
253
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;
258 }
259
260 DXVAVideoDecodeAccelerator::DXVAPictureBuffer::DXVAPictureBuffer(
261     const media::PictureBuffer& buffer)
262     : available_(true),
263       picture_buffer_(buffer),
264       decoding_surface_(NULL),
265       use_rgb_(true) {
266 }
267
268 DXVAVideoDecodeAccelerator::DXVAPictureBuffer::~DXVAPictureBuffer() {
269   if (decoding_surface_) {
270     EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
271
272     eglReleaseTexImage(
273         egl_display,
274         decoding_surface_,
275         EGL_BACK_BUFFER);
276
277     eglDestroySurface(
278         egl_display,
279         decoding_surface_);
280     decoding_surface_ = NULL;
281   }
282 }
283
284 void DXVAVideoDecodeAccelerator::DXVAPictureBuffer::ReusePictureBuffer() {
285   DCHECK(decoding_surface_);
286   EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
287   eglReleaseTexImage(
288     egl_display,
289     decoding_surface_,
290     EGL_BACK_BUFFER);
291   set_available(true);
292 }
293
294 bool DXVAVideoDecodeAccelerator::DXVAPictureBuffer::
295     CopyOutputSampleDataToPictureBuffer(
296         const DXVAVideoDecodeAccelerator& decoder,
297         IDirect3DSurface9* dest_surface) {
298   DCHECK(dest_surface);
299
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);
303
304   D3DSURFACE_DESC texture_desc;
305   decoding_texture_->GetLevelDesc(0, &texture_desc);
306
307   if (texture_desc.Width != surface_desc.Width ||
308       texture_desc.Height != surface_desc.Height) {
309     NOTREACHED() << "Decode surface of different dimension than texture";
310     return false;
311   }
312
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);
317
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, &current_texture);
322
323   glBindTexture(GL_TEXTURE_2D, picture_buffer_.texture_id());
324
325   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
326
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);
330
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",
334                         false);
335
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);
340
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
351   // infinite loop.
352   // Workaround is to have an upper limit of 10 on the number of iterations to
353   // wait for the Flush to finish.
354   int iterations = 0;
355   while ((decoder.query_->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE) &&
356          ++iterations < kMaxIterationsForD3DFlush) {
357     Sleep(1);  // Poor-man's Yield().
358   }
359   EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
360   eglBindTexImage(
361       egl_display,
362       decoding_surface_,
363       EGL_BACK_BUFFER);
364   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
365   glBindTexture(GL_TEXTURE_2D, current_texture);
366   return true;
367 }
368
369 DXVAVideoDecodeAccelerator::PendingSampleInfo::PendingSampleInfo(
370     int32 buffer_id, IMFSample* sample)
371     : input_buffer_id(buffer_id) {
372   output_sample.Attach(sample);
373 }
374
375 DXVAVideoDecodeAccelerator::PendingSampleInfo::~PendingSampleInfo() {}
376
377 // static
378 bool DXVAVideoDecodeAccelerator::CreateD3DDevManager() {
379   TRACE_EVENT0("gpu", "DXVAVideoDecodeAccelerator_CreateD3DDevManager");
380
381   HRESULT hr = Direct3DCreate9Ex(D3D_SDK_VERSION, d3d9_.Receive());
382   RETURN_ON_HR_FAILURE(hr, "Direct3DCreate9Ex failed", false);
383
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;
395
396   hr = d3d9_->CreateDeviceEx(D3DADAPTER_DEFAULT,
397                              D3DDEVTYPE_HAL,
398                              ::GetShellWindow(),
399                              D3DCREATE_FPU_PRESERVE |
400                              D3DCREATE_SOFTWARE_VERTEXPROCESSING |
401                              D3DCREATE_DISABLE_PSGP_THREADING |
402                              D3DCREATE_MULTITHREADED,
403                              &present_params,
404                              NULL,
405                              device_.Receive());
406   RETURN_ON_HR_FAILURE(hr, "Failed to create D3D device", false);
407
408   hr = DXVA2CreateDirect3DDeviceManager9(&dev_manager_reset_token_,
409                                          device_manager_.Receive());
410   RETURN_ON_HR_FAILURE(hr, "DXVA2CreateDirect3DDeviceManager9 failed", false);
411
412   hr = device_manager_->ResetDevice(device_, dev_manager_reset_token_);
413   RETURN_ON_HR_FAILURE(hr, "Failed to reset device", false);
414
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);
421   return true;
422 }
423
424 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator(
425     const base::Callback<bool(void)>& make_context_current)
426     : client_(NULL),
427       dev_manager_reset_token_(0),
428       egl_config_(NULL),
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_));
436 }
437
438 DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() {
439   client_ = NULL;
440 }
441
442 bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile,
443                                             Client* client) {
444   DCHECK(CalledOnValidThread());
445
446   client_ = client;
447
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.
451   //
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);
455
456   // TODO(ananta)
457   // H264PROFILE_HIGH video decoding is janky at times. Needs more
458   // investigation.
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);
464   }
465
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",
469       PLATFORM_FAILURE,
470       false);
471
472   RETURN_AND_NOTIFY_ON_FAILURE((state_ == kUninitialized),
473       "Initialize: invalid state: " << state_, ILLEGAL_STATE, false);
474
475   HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL);
476   RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "MFStartup failed.", PLATFORM_FAILURE,
477       false);
478
479   RETURN_AND_NOTIFY_ON_FAILURE(CreateD3DDevManager(),
480                                "Failed to initialize D3D device and manager",
481                                PLATFORM_FAILURE,
482                                false);
483
484   RETURN_AND_NOTIFY_ON_FAILURE(InitDecoder(profile),
485       "Failed to initialize decoder", PLATFORM_FAILURE, false);
486
487   RETURN_AND_NOTIFY_ON_FAILURE(GetStreamsInfoAndBufferReqs(),
488       "Failed to get input/output stream info.", PLATFORM_FAILURE, false);
489
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);
494
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);
499
500   state_ = kNormal;
501   return true;
502 }
503
504 void DXVAVideoDecodeAccelerator::Decode(
505     const media::BitstreamBuffer& bitstream_buffer) {
506   DCHECK(CalledOnValidThread());
507
508   RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped ||
509                                 state_ == kFlushing),
510       "Invalid state: " << state_, ILLEGAL_STATE,);
511
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",
517                                PLATFORM_FAILURE,);
518
519   RETURN_AND_NOTIFY_ON_HR_FAILURE(sample->SetSampleTime(bitstream_buffer.id()),
520       "Failed to associate input buffer id with sample", PLATFORM_FAILURE,);
521
522   DecodeInternal(sample);
523 }
524
525 void DXVAVideoDecodeAccelerator::AssignPictureBuffers(
526     const std::vector<media::PictureBuffer>& buffers) {
527   DCHECK(CalledOnValidThread());
528
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,);
534
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();
538        ++buffer_index) {
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,);
543
544     bool inserted = output_picture_buffers_.insert(std::make_pair(
545         buffers[buffer_index].id(), picture_buffer)).second;
546     DCHECK(inserted);
547   }
548   ProcessPendingSamples();
549   if (state_ == kFlushing && pending_output_samples_.empty())
550     FlushInternal();
551 }
552
553 void DXVAVideoDecodeAccelerator::ReusePictureBuffer(
554     int32 picture_buffer_id) {
555   DCHECK(CalledOnValidThread());
556
557   RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized),
558       "Invalid state: " << state_, ILLEGAL_STATE,);
559
560   if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty())
561     return;
562
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));
577     return;
578   }
579
580   it->second->ReusePictureBuffer();
581   ProcessPendingSamples();
582
583   if (state_ == kFlushing && pending_output_samples_.empty())
584     FlushInternal();
585 }
586
587 void DXVAVideoDecodeAccelerator::Flush() {
588   DCHECK(CalledOnValidThread());
589
590   DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush";
591
592   RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped),
593       "Unexpected decoder state: " << state_, ILLEGAL_STATE,);
594
595   state_ = kFlushing;
596
597   RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0),
598       "Failed to send drain message", PLATFORM_FAILURE,);
599
600   if (!pending_output_samples_.empty())
601     return;
602
603   FlushInternal();
604 }
605
606 void DXVAVideoDecodeAccelerator::Reset() {
607   DCHECK(CalledOnValidThread());
608
609   DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset";
610
611   RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped),
612       "Reset: invalid state: " << state_, ILLEGAL_STATE,);
613
614   state_ = kResetting;
615
616   pending_output_samples_.clear();
617
618   NotifyInputBuffersDropped();
619
620   RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0),
621       "Reset: Failed to send message.", PLATFORM_FAILURE,);
622
623   base::MessageLoop::current()->PostTask(
624       FROM_HERE,
625       base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone,
626                  weak_this_factory_.GetWeakPtr()));
627
628   state_ = DXVAVideoDecodeAccelerator::kNormal;
629 }
630
631 void DXVAVideoDecodeAccelerator::Destroy() {
632   DCHECK(CalledOnValidThread());
633   Invalidate();
634   delete this;
635 }
636
637 bool DXVAVideoDecodeAccelerator::CanDecodeOnIOThread() {
638   return false;
639 }
640
641 bool DXVAVideoDecodeAccelerator::InitDecoder(media::VideoCodecProfile profile) {
642   if (profile < media::H264PROFILE_MIN || profile > media::H264PROFILE_MAX)
643     return false;
644
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",
652                     false);
653
654   typedef HRESULT(WINAPI * GetClassObject)(
655       const CLSID & clsid, const IID & iid, void * *object);
656
657   GetClassObject get_class_object = reinterpret_cast<GetClassObject>(
658       GetProcAddress(decoder_dll, "DllGetClassObject"));
659   RETURN_ON_FAILURE(
660       get_class_object, "Failed to get DllGetClassObject pointer", false);
661
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);
667
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);
672
673   RETURN_ON_FAILURE(CheckDecoderDxvaSupport(),
674                     "Failed to check decoder DXVA support", false);
675
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);
680
681   EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
682
683   EGLint config_attribs[] = {
684     EGL_BUFFER_SIZE, 32,
685     EGL_RED_SIZE, 8,
686     EGL_GREEN_SIZE, 8,
687     EGL_BLUE_SIZE, 8,
688     EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
689     EGL_ALPHA_SIZE, 0,
690     EGL_NONE
691   };
692
693   EGLint num_configs;
694
695   if (!eglChooseConfig(
696       egl_display,
697       config_attribs,
698       &egl_config_,
699       1,
700       &num_configs))
701     return false;
702
703   return SetDecoderMediaTypes();
704 }
705
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);
710
711   UINT32 dxva = 0;
712   hr = attributes->GetUINT32(MF_SA_D3D_AWARE, &dxva);
713   RETURN_ON_HR_FAILURE(hr, "Failed to check if decoder supports DXVA", false);
714
715   hr = attributes->SetUINT32(CODECAPI_AVDecVideoAcceleration_H264, TRUE);
716   RETURN_ON_HR_FAILURE(hr, "Failed to enable DXVA H/W decoding", false);
717   return true;
718 }
719
720 bool DXVAVideoDecodeAccelerator::SetDecoderMediaTypes() {
721   RETURN_ON_FAILURE(SetDecoderInputMediaType(),
722                     "Failed to set decoder input media type", false);
723   return SetDecoderOutputMediaType(MFVideoFormat_NV12);
724 }
725
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);
730
731   hr = media_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
732   RETURN_ON_HR_FAILURE(hr, "Failed to set major input type", false);
733
734   hr = media_type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_H264);
735   RETURN_ON_HR_FAILURE(hr, "Failed to set subtype", false);
736
737   // Not sure about this. msdn recommends setting this value on the input
738   // media type.
739   hr = media_type->SetUINT32(MF_MT_INTERLACE_MODE,
740                              MFVideoInterlace_MixedInterlaceOrProgressive);
741   RETURN_ON_HR_FAILURE(hr, "Failed to set interlace mode", false);
742
743   hr = decoder_->SetInputType(0, media_type, 0);  // No flags
744   RETURN_ON_HR_FAILURE(hr, "Failed to set decoder input type", false);
745   return true;
746 }
747
748 bool DXVAVideoDecodeAccelerator::SetDecoderOutputMediaType(
749     const GUID& subtype) {
750   base::win::ScopedComPtr<IMFMediaType> out_media_type;
751
752   for (uint32 i = 0;
753        SUCCEEDED(decoder_->GetOutputAvailableType(0, i,
754                                                   out_media_type.Receive()));
755        ++i) {
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);
759
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);
763       return true;
764     }
765     out_media_type.Release();
766   }
767   return false;
768 }
769
770 bool DXVAVideoDecodeAccelerator::SendMFTMessage(MFT_MESSAGE_TYPE msg,
771                                                 int32 param) {
772   HRESULT hr = decoder_->ProcessMessage(msg, param);
773   return SUCCEEDED(hr);
774 }
775
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);
782
783   hr = decoder_->GetOutputStreamInfo(0, &output_stream_info_);
784   RETURN_ON_HR_FAILURE(hr, "Failed to get decoder output stream info", false);
785
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);
792
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;
796
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;
806   return true;
807 }
808
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 ||
813                                 state_ == kStopped),
814       "DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,);
815
816   MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0};
817   DWORD status = 0;
818
819   HRESULT hr = decoder_->ProcessOutput(0,  // No flags
820                                        1,  // # of out streams to pull from
821                                        &output_data_buffer,
822                                        &status);
823   IMFCollection* events = output_data_buffer.pEvents;
824   if (events != NULL) {
825     VLOG(1) << "Got events from ProcessOuput, but discarding";
826     events->Release();
827   }
828   if (FAILED(hr)) {
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";
836         state_ = kStopped;
837       } else {
838         DVLOG(1) << "Received output format change from the decoder."
839                     " Recursively invoking DoDecode";
840         DoDecode();
841       }
842       return;
843     } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
844       // No more output from the decoder. Stop playback.
845       state_ = kStopped;
846       return;
847     } else {
848       NOTREACHED() << "Unhandled error in DoDecode()";
849       return;
850     }
851   }
852   TRACE_EVENT_END_ETW("DXVAVideoDecodeAccelerator.Decoding", this, "");
853
854   TRACE_COUNTER1("DXVA Decoding", "TotalPacketsBeforeDecode",
855                  inputs_before_decode_);
856
857   inputs_before_decode_ = 0;
858
859   RETURN_AND_NOTIFY_ON_FAILURE(ProcessOutputSample(output_data_buffer.pSample),
860       "Failed to process output sample.", PLATFORM_FAILURE,);
861 }
862
863 bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) {
864   RETURN_ON_FAILURE(sample, "Decode succeeded with NULL output sample", false);
865
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);
869
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",
874                        false);
875
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",
879                        false);
880
881   pending_output_samples_.push_back(
882       PendingSampleInfo(input_buffer_id, sample));
883
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();
888     return true;
889   }
890   if (pictures_requested_) {
891     DVLOG(1) << "Waiting for picture slots from the client.";
892     return true;
893   }
894
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
897   // are reused.
898   D3DSURFACE_DESC surface_desc;
899   hr = surface->GetDesc(&surface_desc);
900   RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false);
901
902   // Go ahead and request picture buffers.
903   base::MessageLoop::current()->PostTask(
904       FROM_HERE,
905       base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers,
906                  weak_this_factory_.GetWeakPtr(),
907                  surface_desc.Width,
908                  surface_desc.Height));
909
910   pictures_requested_ = true;
911   return true;
912 }
913
914 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() {
915   RETURN_AND_NOTIFY_ON_FAILURE(make_context_current_.Run(),
916       "Failed to make context current", PLATFORM_FAILURE,);
917
918   OutputBuffers::iterator index;
919
920   for (index = output_picture_buffers_.begin();
921        index != output_picture_buffers_.end() &&
922        !pending_output_samples_.empty();
923        ++index) {
924     if (index->second->available()) {
925       PendingSampleInfo sample_info = pending_output_samples_.front();
926
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,);
932
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",
938           PLATFORM_FAILURE,);
939
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,);
944
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);
950         return;
951       }
952
953       RETURN_AND_NOTIFY_ON_FAILURE(
954           index->second->CopyOutputSampleDataToPictureBuffer(*this, surface),
955           "Failed to copy output sample",
956           PLATFORM_FAILURE, );
957
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(
962           FROM_HERE,
963           base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady,
964                      weak_this_factory_.GetWeakPtr(),
965                      output_picture));
966
967       index->second->set_available(false);
968       pending_output_samples_.pop_front();
969     }
970   }
971
972   if (!pending_input_buffers_.empty() && pending_output_samples_.empty()) {
973     base::MessageLoop::current()->PostTask(
974         FROM_HERE,
975         base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
976                    weak_this_factory_.GetWeakPtr()));
977   }
978 }
979
980 void DXVAVideoDecodeAccelerator::StopOnError(
981   media::VideoDecodeAccelerator::Error error) {
982   DCHECK(CalledOnValidThread());
983
984   if (client_)
985     client_->NotifyError(error);
986   client_ = NULL;
987
988   if (state_ != kUninitialized) {
989     Invalidate();
990   }
991 }
992
993 void DXVAVideoDecodeAccelerator::Invalidate() {
994   if (state_ == kUninitialized)
995     return;
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();
1001   decoder_.Release();
1002   MFShutdown();
1003   state_ = kUninitialized;
1004 }
1005
1006 void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) {
1007   if (client_)
1008     client_->NotifyEndOfBitstreamBuffer(input_buffer_id);
1009 }
1010
1011 void DXVAVideoDecodeAccelerator::NotifyFlushDone() {
1012   if (client_)
1013     client_->NotifyFlushDone();
1014 }
1015
1016 void DXVAVideoDecodeAccelerator::NotifyResetDone() {
1017   if (client_)
1018     client_->NotifyResetDone();
1019 }
1020
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(
1025         kNumPictureBuffers,
1026         gfx::Size(width, height),
1027         GL_TEXTURE_2D);
1028   }
1029 }
1030
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);
1036 }
1037
1038 void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() {
1039   if (!client_ || !pending_output_samples_.empty())
1040     return;
1041
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);
1048   }
1049   pending_input_buffers_.clear();
1050 }
1051
1052 void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() {
1053   RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized),
1054       "Invalid state: " << state_, ILLEGAL_STATE,);
1055
1056   if (pending_input_buffers_.empty() || !pending_output_samples_.empty())
1057     return;
1058
1059   PendingInputs pending_input_buffers_copy;
1060   std::swap(pending_input_buffers_, pending_input_buffers_copy);
1061
1062   for (PendingInputs::iterator it = pending_input_buffers_copy.begin();
1063        it != pending_input_buffers_copy.end(); ++it) {
1064     DecodeInternal(*it);
1065   }
1066 }
1067
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) {
1075     DoDecode();
1076     if (!pending_output_samples_.empty())
1077       return;
1078   }
1079
1080   base::MessageLoop::current()->PostTask(
1081       FROM_HERE,
1082       base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone,
1083                  weak_this_factory_.GetWeakPtr()));
1084
1085   state_ = kNormal;
1086 }
1087
1088 void DXVAVideoDecodeAccelerator::DecodeInternal(
1089     const base::win::ScopedComPtr<IMFSample>& sample) {
1090   DCHECK(CalledOnValidThread());
1091
1092   if (state_ == kUninitialized)
1093     return;
1094
1095   if (!pending_output_samples_.empty() || !pending_input_buffers_.empty()) {
1096     pending_input_buffers_.push_back(sample);
1097     return;
1098   }
1099
1100   if (!inputs_before_decode_) {
1101     TRACE_EVENT_BEGIN_ETW("DXVAVideoDecodeAccelerator.Decoding", this, "");
1102   }
1103   inputs_before_decode_++;
1104
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
1114   // decoder failure.
1115   if (hr == MF_E_NOTACCEPTING) {
1116     DoDecode();
1117     RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal),
1118         "Failed to process output. Unexpected decoder state: " << state_,
1119         PLATFORM_FAILURE,);
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(
1134             FROM_HERE,
1135             base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1136                        weak_this_factory_.GetWeakPtr()));
1137       }
1138       return;
1139     }
1140   }
1141   RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "Failed to process input sample",
1142       PLATFORM_FAILURE,);
1143
1144   DoDecode();
1145
1146   RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal),
1147       "Failed to process output. Unexpected decoder state: " << state_,
1148       ILLEGAL_STATE,);
1149
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.
1158   // TODO(ananta)
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(
1164       FROM_HERE,
1165       base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead,
1166                  weak_this_factory_.GetWeakPtr(),
1167                  input_buffer_id));
1168 }
1169
1170 void DXVAVideoDecodeAccelerator::HandleResolutionChanged(int width,
1171                                                          int height) {
1172   base::MessageLoop::current()->PostTask(
1173       FROM_HERE,
1174       base::Bind(&DXVAVideoDecodeAccelerator::DismissStaleBuffers,
1175                  weak_this_factory_.GetWeakPtr()));
1176
1177   base::MessageLoop::current()->PostTask(
1178       FROM_HERE,
1179       base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers,
1180                  weak_this_factory_.GetWeakPtr(),
1181                  width,
1182                  height));
1183 }
1184
1185 void DXVAVideoDecodeAccelerator::DismissStaleBuffers() {
1186   OutputBuffers::iterator index;
1187
1188   for (index = output_picture_buffers_.begin();
1189        index != output_picture_buffers_.end();
1190        ++index) {
1191     if (index->second->available()) {
1192       DVLOG(1) << "Dismissing picture id: " << index->second->id();
1193       client_->DismissPictureBuffer(index->second->id());
1194     } else {
1195       // Move to |stale_output_picture_buffers_| for deferred deletion.
1196       stale_output_picture_buffers_.insert(
1197           std::make_pair(index->first, index->second));
1198     }
1199   }
1200
1201   output_picture_buffers_.clear();
1202 }
1203
1204 void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer(
1205     int32 picture_buffer_id) {
1206   OutputBuffers::iterator it = stale_output_picture_buffers_.find(
1207       picture_buffer_id);
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);
1212 }
1213
1214 }  // namespace content