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 #ifndef CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_
6 #define CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_
9 // Work around bug in this header by disabling the relevant warning for it.
10 // https://connect.microsoft.com/VisualStudio/feedback/details/911260/dxva2api-h-in-win8-sdk-triggers-c4201-with-w4
12 #pragma warning(disable:4201)
20 #include "base/compiler_specific.h"
21 #include "base/memory/linked_ptr.h"
22 #include "base/memory/weak_ptr.h"
23 #include "base/threading/non_thread_safe.h"
24 #include "base/win/scoped_comptr.h"
25 #include "content/common/content_export.h"
26 #include "media/video/video_decode_accelerator.h"
29 interface IDirect3DSurface9
;
33 // Class to provide a DXVA 2.0 based accelerator using the Microsoft Media
34 // foundation APIs via the VideoDecodeAccelerator interface.
35 // This class lives on a single thread and DCHECKs that it is never accessed
37 class CONTENT_EXPORT DXVAVideoDecodeAccelerator
38 : public media::VideoDecodeAccelerator
,
39 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
42 kUninitialized
, // un-initialized.
43 kNormal
, // normal playing state.
44 kResetting
, // upon received Reset(), before ResetDone()
45 kStopped
, // upon output EOS received.
46 kFlushing
, // upon flush request received.
49 // Does not take ownership of |client| which must outlive |*this|.
50 explicit DXVAVideoDecodeAccelerator(
51 const base::Callback
<bool(void)>& make_context_current
);
52 virtual ~DXVAVideoDecodeAccelerator();
54 // media::VideoDecodeAccelerator implementation.
55 virtual bool Initialize(media::VideoCodecProfile profile
,
56 Client
* client
) OVERRIDE
;
57 virtual void Decode(const media::BitstreamBuffer
& bitstream_buffer
) OVERRIDE
;
58 virtual void AssignPictureBuffers(
59 const std::vector
<media::PictureBuffer
>& buffers
) OVERRIDE
;
60 virtual void ReusePictureBuffer(int32 picture_buffer_id
) OVERRIDE
;
61 virtual void Flush() OVERRIDE
;
62 virtual void Reset() OVERRIDE
;
63 virtual void Destroy() OVERRIDE
;
64 virtual bool CanDecodeOnIOThread() OVERRIDE
;
67 typedef void* EGLConfig
;
68 typedef void* EGLSurface
;
69 // Creates and initializes an instance of the D3D device and the
70 // corresponding device manager. The device manager instance is eventually
71 // passed to the IMFTransform interface implemented by the h.264 decoder.
72 bool CreateD3DDevManager();
74 // Creates, initializes and sets the media types for the h.264 decoder.
75 bool InitDecoder(media::VideoCodecProfile profile
);
77 // Validates whether the h.264 decoder supports hardware video acceleration.
78 bool CheckDecoderDxvaSupport();
80 // Returns information about the input and output streams. This includes
81 // alignment information, decoder support flags, minimum sample size, etc.
82 bool GetStreamsInfoAndBufferReqs();
84 // Registers the input and output media types on the h.264 decoder. This
85 // includes the expected input and output formats.
86 bool SetDecoderMediaTypes();
88 // Registers the input media type for the h.264 decoder.
89 bool SetDecoderInputMediaType();
91 // Registers the output media type for the h.264 decoder.
92 bool SetDecoderOutputMediaType(const GUID
& subtype
);
94 // Passes a command message to the decoder. This includes commands like
95 // start of stream, end of stream, flush, drain the decoder, etc.
96 bool SendMFTMessage(MFT_MESSAGE_TYPE msg
, int32 param
);
98 // The bulk of the decoding happens here. This function handles errors,
99 // format changes and processes decoded output.
102 // Invoked when we have a valid decoded output sample. Retrieves the D3D
103 // surface and maintains a copy of it which is passed eventually to the
104 // client when we have a picture buffer to copy the surface contents to.
105 bool ProcessOutputSample(IMFSample
* sample
);
107 // Processes pending output samples by copying them to available picture
109 void ProcessPendingSamples();
111 // Helper function to notify the accelerator client about the error.
112 void StopOnError(media::VideoDecodeAccelerator::Error error
);
114 // Transitions the decoder to the uninitialized state. The decoder will stop
115 // accepting requests in this state.
118 // Notifies the client that the input buffer identifed by input_buffer_id has
120 void NotifyInputBufferRead(int input_buffer_id
);
122 // Notifies the client that the decoder was flushed.
123 void NotifyFlushDone();
125 // Notifies the client that the decoder was reset.
126 void NotifyResetDone();
128 // Requests picture buffers from the client.
129 void RequestPictureBuffers(int width
, int height
);
131 // Notifies the client about the availability of a picture.
132 void NotifyPictureReady(const media::Picture
& picture
);
134 // Sends pending input buffer processed acks to the client if we don't have
135 // output samples waiting to be processed.
136 void NotifyInputBuffersDropped();
138 // Decodes pending input buffers.
139 void DecodePendingInputBuffers();
141 // Helper for handling the Flush operation.
142 void FlushInternal();
144 // Helper for handling the Decode operation.
145 void DecodeInternal(const base::win::ScopedComPtr
<IMFSample
>& input_sample
);
147 // Handles mid stream resolution changes.
148 void HandleResolutionChanged(int width
, int height
);
150 struct DXVAPictureBuffer
;
151 typedef std::map
<int32
, linked_ptr
<DXVAPictureBuffer
> > OutputBuffers
;
153 // Tells the client to dismiss the stale picture buffers passed in.
154 void DismissStaleBuffers(const OutputBuffers
& picture_buffers
);
156 // To expose client callbacks from VideoDecodeAccelerator.
157 media::VideoDecodeAccelerator::Client
* client_
;
159 base::win::ScopedComPtr
<IMFTransform
> decoder_
;
161 base::win::ScopedComPtr
<IDirect3D9Ex
> d3d9_
;
162 base::win::ScopedComPtr
<IDirect3DDevice9Ex
> device_
;
163 base::win::ScopedComPtr
<IDirect3DDeviceManager9
> device_manager_
;
164 base::win::ScopedComPtr
<IDirect3DQuery9
> query_
;
165 // Ideally the reset token would be a stack variable which is used while
166 // creating the device manager. However it seems that the device manager
167 // holds onto the token and attempts to access it if the underlying device
169 // TODO(ananta): This needs to be verified.
170 uint32 dev_manager_reset_token_
;
172 // The EGL config to use for decoded frames.
173 EGLConfig egl_config_
;
175 // Current state of the decoder.
178 MFT_INPUT_STREAM_INFO input_stream_info_
;
179 MFT_OUTPUT_STREAM_INFO output_stream_info_
;
181 // Contains information about a decoded sample.
182 struct PendingSampleInfo
{
183 PendingSampleInfo(int32 buffer_id
, IMFSample
* sample
);
184 ~PendingSampleInfo();
186 int32 input_buffer_id
;
187 base::win::ScopedComPtr
<IMFSample
> output_sample
;
190 typedef std::list
<PendingSampleInfo
> PendingOutputSamples
;
192 // List of decoded output samples.
193 PendingOutputSamples pending_output_samples_
;
195 // This map maintains the picture buffers passed the client for decoding.
196 // The key is the picture buffer id.
197 OutputBuffers output_picture_buffers_
;
199 // Set to true if we requested picture slots from the client.
200 bool pictures_requested_
;
202 // Counter which holds the number of input packets before a successful
204 int inputs_before_decode_
;
206 // List of input samples waiting to be processed.
207 typedef std::list
<base::win::ScopedComPtr
<IMFSample
>> PendingInputs
;
208 PendingInputs pending_input_buffers_
;
210 // Callback to set the correct gl context.
211 base::Callback
<bool(void)> make_context_current_
;
213 // WeakPtrFactory for posting tasks back to |this|.
214 base::WeakPtrFactory
<DXVAVideoDecodeAccelerator
> weak_this_factory_
;
217 } // namespace content
219 #endif // CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_