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_
15 #include "base/compiler_specific.h"
16 #include "base/memory/linked_ptr.h"
17 #include "base/threading/non_thread_safe.h"
18 #include "base/win/scoped_comptr.h"
19 #include "content/common/content_export.h"
20 #include "content/common/gpu/media/video_decode_accelerator_impl.h"
21 #include "media/video/video_decode_accelerator.h"
24 interface IDirect3DSurface9
;
28 // Class to provide a DXVA 2.0 based accelerator using the Microsoft Media
29 // foundation APIs via the VideoDecodeAccelerator interface.
30 // This class lives on a single thread and DCHECKs that it is never accessed
32 class CONTENT_EXPORT DXVAVideoDecodeAccelerator
33 : public VideoDecodeAcceleratorImpl
,
34 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
37 kUninitialized
, // un-initialized.
38 kNormal
, // normal playing state.
39 kResetting
, // upon received Reset(), before ResetDone()
40 kStopped
, // upon output EOS received.
41 kFlushing
, // upon flush request received.
44 // Does not take ownership of |client| which must outlive |*this|.
45 explicit DXVAVideoDecodeAccelerator(
46 media::VideoDecodeAccelerator::Client
* client
,
47 const base::Callback
<bool(void)>& make_context_current
);
48 virtual ~DXVAVideoDecodeAccelerator();
50 // media::VideoDecodeAccelerator implementation.
51 virtual bool Initialize(media::VideoCodecProfile profile
) OVERRIDE
;
52 virtual void Decode(const media::BitstreamBuffer
& bitstream_buffer
) OVERRIDE
;
53 virtual void AssignPictureBuffers(
54 const std::vector
<media::PictureBuffer
>& buffers
) OVERRIDE
;
55 virtual void ReusePictureBuffer(int32 picture_buffer_id
) OVERRIDE
;
56 virtual void Flush() OVERRIDE
;
57 virtual void Reset() OVERRIDE
;
58 virtual void Destroy() OVERRIDE
;
60 // Initialization work needed before the process is sandboxed.
62 // 1. Loads the dlls like mf/mfplat/d3d9, etc required for decoding.
63 // 2. Setting up the device manager instance which is shared between all
65 static void PreSandboxInitialization();
68 typedef void* EGLConfig
;
69 typedef void* EGLSurface
;
70 // Creates and initializes an instance of the D3D device and the
71 // corresponding device manager. The device manager instance is eventually
72 // passed to the IMFTransform interface implemented by the h.264 decoder.
73 static bool CreateD3DDevManager();
75 // Creates, initializes and sets the media types for the h.264 decoder.
78 // Validates whether the h.264 decoder supports hardware video acceleration.
79 bool CheckDecoderDxvaSupport();
81 // Returns information about the input and output streams. This includes
82 // alignment information, decoder support flags, minimum sample size, etc.
83 bool GetStreamsInfoAndBufferReqs();
85 // Registers the input and output media types on the h.264 decoder. This
86 // includes the expected input and output formats.
87 bool SetDecoderMediaTypes();
89 // Registers the input media type for the h.264 decoder.
90 bool SetDecoderInputMediaType();
92 // Registers the output media type for the h.264 decoder.
93 bool SetDecoderOutputMediaType(const GUID
& subtype
);
95 // Passes a command message to the decoder. This includes commands like
96 // start of stream, end of stream, flush, drain the decoder, etc.
97 bool SendMFTMessage(MFT_MESSAGE_TYPE msg
, int32 param
);
99 // The bulk of the decoding happens here. This function handles errors,
100 // format changes and processes decoded output.
103 // Invoked when we have a valid decoded output sample. Retrieves the D3D
104 // surface and maintains a copy of it which is passed eventually to the
105 // client when we have a picture buffer to copy the surface contents to.
106 bool ProcessOutputSample(IMFSample
* sample
);
108 // Processes pending output samples by copying them to available picture
110 void ProcessPendingSamples();
112 // Helper function to notify the accelerator client about the error.
113 void StopOnError(media::VideoDecodeAccelerator::Error error
);
115 // Transitions the decoder to the uninitialized state. The decoder will stop
116 // accepting requests in this state.
119 // Notifies the client that the input buffer identifed by input_buffer_id has
121 void NotifyInputBufferRead(int input_buffer_id
);
123 // Notifies the client that initialize was completed.
124 void NotifyInitializeDone();
126 // Notifies the client that the decoder was flushed.
127 void NotifyFlushDone();
129 // Notifies the client that the decoder was reset.
130 void NotifyResetDone();
132 // Requests picture buffers from the client.
133 void RequestPictureBuffers(int width
, int height
);
135 // Notifies the client about the availability of a picture.
136 void NotifyPictureReady(const media::Picture
& picture
);
138 // Sends pending input buffer processed acks to the client if we don't have
139 // output samples waiting to be processed.
140 void NotifyInputBuffersDropped();
142 // Decodes pending input buffers.
143 void DecodePendingInputBuffers();
145 // Helper for handling the Flush operation.
146 void FlushInternal();
148 // Helper for handling the Decode operation.
149 void DecodeInternal(const base::win::ScopedComPtr
<IMFSample
>& input_sample
);
151 // Handles mid stream resolution changes.
152 void HandleResolutionChanged(int width
, int height
);
154 struct DXVAPictureBuffer
;
155 typedef std::map
<int32
, linked_ptr
<DXVAPictureBuffer
> > OutputBuffers
;
157 // Tells the client to dismiss the stale picture buffers passed in.
158 void DismissStaleBuffers(const OutputBuffers
& picture_buffers
);
160 // To expose client callbacks from VideoDecodeAccelerator.
161 media::VideoDecodeAccelerator::Client
* client_
;
163 base::win::ScopedComPtr
<IMFTransform
> decoder_
;
165 // These interface pointers are initialized before the process is sandboxed.
166 // They are not released when the GPU process exits. This is ok for now
167 // because the GPU process does not exit normally on Windows. It is always
168 // terminated. The device manager instance is shared among all decoder
169 // instances. This is OK because there is internal locking performed by the
171 static IDirect3DDeviceManager9
* device_manager_
;
172 static IDirect3DDevice9Ex
* device_
;
173 static IDirect3DQuery9
* query_
;
174 static IDirect3D9Ex
* d3d9_
;
176 // The EGL config to use for decoded frames.
177 EGLConfig egl_config_
;
179 // Current state of the decoder.
182 MFT_INPUT_STREAM_INFO input_stream_info_
;
183 MFT_OUTPUT_STREAM_INFO output_stream_info_
;
185 // Contains information about a decoded sample.
186 struct PendingSampleInfo
{
187 PendingSampleInfo(int32 buffer_id
, IMFSample
* sample
);
188 ~PendingSampleInfo();
190 int32 input_buffer_id
;
191 base::win::ScopedComPtr
<IMFSample
> output_sample
;
194 typedef std::list
<PendingSampleInfo
> PendingOutputSamples
;
196 // List of decoded output samples.
197 PendingOutputSamples pending_output_samples_
;
199 // This map maintains the picture buffers passed the client for decoding.
200 // The key is the picture buffer id.
201 OutputBuffers output_picture_buffers_
;
203 // Set to true if we requested picture slots from the client.
204 bool pictures_requested_
;
206 // Ideally the reset token would be a stack variable which is used while
207 // creating the device manager. However it seems that the device manager
208 // holds onto the token and attempts to access it if the underlying device
210 // TODO(ananta): This needs to be verified.
211 static uint32 dev_manager_reset_token_
;
213 // Counter which holds the number of input packets before a successful
215 int inputs_before_decode_
;
217 // Set to true if all necessary initialization needed before the GPU process
218 // is sandboxed is done.
219 // This includes the following:
220 // 1. All required decoder dlls were successfully loaded.
221 // 2. The device manager initialization completed.
222 static bool pre_sandbox_init_done_
;
224 // List of input samples waiting to be processed.
225 typedef std::list
<base::win::ScopedComPtr
<IMFSample
>> PendingInputs
;
226 PendingInputs pending_input_buffers_
;
228 // Callback to set the correct gl context.
229 base::Callback
<bool(void)> make_context_current_
;
232 } // namespace content
234 #endif // CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_