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 #ifndef CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_ 6 #define CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_ 7 8 #include <d3d9.h> 9 #include <dxva2api.h> 10 #include <list> 11 #include <map> 12 #include <mfidl.h> 13 #include <vector> 14 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 "media/video/video_decode_accelerator.h" 21 22 interface IMFSample; 23 interface IDirect3DSurface9; 24 25 namespace content { 26 27 // Class to provide a DXVA 2.0 based accelerator using the Microsoft Media 28 // foundation APIs via the VideoDecodeAccelerator interface. 29 // This class lives on a single thread and DCHECKs that it is never accessed 30 // from any other. 31 class CONTENT_EXPORT DXVAVideoDecodeAccelerator 32 : public media::VideoDecodeAccelerator, 33 NON_EXPORTED_BASE(public base::NonThreadSafe) { 34 public: 35 enum State { 36 kUninitialized, // un-initialized. 37 kNormal, // normal playing state. 38 kResetting, // upon received Reset(), before ResetDone() 39 kStopped, // upon output EOS received. 40 kFlushing, // upon flush request received. 41 }; 42 43 // Does not take ownership of |client| which must outlive |*this|. 44 explicit DXVAVideoDecodeAccelerator( 45 media::VideoDecodeAccelerator::Client* client, 46 const base::Callback<bool(void)>& make_context_current); 47 virtual ~DXVAVideoDecodeAccelerator(); 48 49 // media::VideoDecodeAccelerator implementation. 50 virtual bool Initialize(media::VideoCodecProfile profile) OVERRIDE; 51 virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE; 52 virtual void AssignPictureBuffers( 53 const std::vector<media::PictureBuffer>& buffers) OVERRIDE; 54 virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE; 55 virtual void Flush() OVERRIDE; 56 virtual void Reset() OVERRIDE; 57 virtual void Destroy() OVERRIDE; 58 59 // Initialization work needed before the process is sandboxed. 60 // This includes:- 61 // 1. Loads the dlls like mf/mfplat/d3d9, etc required for decoding. 62 // 2. Setting up the device manager instance which is shared between all 63 // decoder instances. 64 static void PreSandboxInitialization(); 65 66 private: 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 static bool CreateD3DDevManager(); 73 74 // Creates, initializes and sets the media types for the h.264 decoder. 75 bool InitDecoder(); 76 77 // Validates whether the h.264 decoder supports hardware video acceleration. 78 bool CheckDecoderDxvaSupport(); 79 80 // Returns information about the input and output streams. This includes 81 // alignment information, decoder support flags, minimum sample size, etc. 82 bool GetStreamsInfoAndBufferReqs(); 83 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(); 87 88 // Registers the input media type for the h.264 decoder. 89 bool SetDecoderInputMediaType(); 90 91 // Registers the output media type for the h.264 decoder. 92 bool SetDecoderOutputMediaType(const GUID& subtype); 93 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); 97 98 // The bulk of the decoding happens here. This function handles errors, 99 // format changes and processes decoded output. 100 void DoDecode(); 101 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); 106 107 // Processes pending output samples by copying them to available picture 108 // slots. 109 void ProcessPendingSamples(); 110 111 // Helper function to notify the accelerator client about the error. 112 void StopOnError(media::VideoDecodeAccelerator::Error error); 113 114 // Transitions the decoder to the uninitialized state. The decoder will stop 115 // accepting requests in this state. 116 void Invalidate(); 117 118 // Notifies the client that the input buffer identifed by input_buffer_id has 119 // been processed. 120 void NotifyInputBufferRead(int input_buffer_id); 121 122 // Notifies the client that initialize was completed. 123 void NotifyInitializeDone(); 124 125 // Notifies the client that the decoder was flushed. 126 void NotifyFlushDone(); 127 128 // Notifies the client that the decoder was reset. 129 void NotifyResetDone(); 130 131 // Requests picture buffers from the client. 132 void RequestPictureBuffers(int width, int height); 133 134 // Notifies the client about the availability of a picture. 135 void NotifyPictureReady(const media::Picture& picture); 136 137 // Sends pending input buffer processed acks to the client if we don't have 138 // output samples waiting to be processed. 139 void NotifyInputBuffersDropped(); 140 141 // Decodes pending input buffers. 142 void DecodePendingInputBuffers(); 143 144 // Helper for handling the Flush operation. 145 void FlushInternal(); 146 147 // Helper for handling the Decode operation. 148 void DecodeInternal(const base::win::ScopedComPtr<IMFSample>& input_sample); 149 150 // To expose client callbacks from VideoDecodeAccelerator. 151 media::VideoDecodeAccelerator::Client* client_; 152 153 base::win::ScopedComPtr<IMFTransform> decoder_; 154 155 // These interface pointers are initialized before the process is sandboxed. 156 // They are not released when the GPU process exits. This is ok for now 157 // because the GPU process does not exit normally on Windows. It is always 158 // terminated. The device manager instance is shared among all decoder 159 // instances. This is OK because there is internal locking performed by the 160 // device manager. 161 static IDirect3DDeviceManager9* device_manager_; 162 static IDirect3DDevice9Ex* device_; 163 static IDirect3DQuery9* query_; 164 static IDirect3D9Ex* d3d9_; 165 166 // The EGL config to use for decoded frames. 167 EGLConfig egl_config_; 168 169 // Current state of the decoder. 170 State state_; 171 172 MFT_INPUT_STREAM_INFO input_stream_info_; 173 MFT_OUTPUT_STREAM_INFO output_stream_info_; 174 175 // Contains information about a decoded sample. 176 struct PendingSampleInfo { 177 PendingSampleInfo(int32 buffer_id, IMFSample* sample); 178 ~PendingSampleInfo(); 179 180 int32 input_buffer_id; 181 base::win::ScopedComPtr<IMFSample> output_sample; 182 }; 183 184 typedef std::list<PendingSampleInfo> PendingOutputSamples; 185 186 // List of decoded output samples. 187 PendingOutputSamples pending_output_samples_; 188 189 struct DXVAPictureBuffer; 190 191 // This map maintains the picture buffers passed the client for decoding. 192 // The key is the picture buffer id. 193 typedef std::map<int32, linked_ptr<DXVAPictureBuffer> > OutputBuffers; 194 OutputBuffers output_picture_buffers_; 195 196 // Set to true if we requested picture slots from the client. 197 bool pictures_requested_; 198 199 // Ideally the reset token would be a stack variable which is used while 200 // creating the device manager. However it seems that the device manager 201 // holds onto the token and attempts to access it if the underlying device 202 // changes. 203 // TODO(ananta): This needs to be verified. 204 static uint32 dev_manager_reset_token_; 205 206 // Counter which holds the number of input packets before a successful 207 // decode. 208 int inputs_before_decode_; 209 210 // Set to true if all necessary initialization needed before the GPU process 211 // is sandboxed is done. 212 // This includes the following: 213 // 1. All required decoder dlls were successfully loaded. 214 // 2. The device manager initialization completed. 215 static bool pre_sandbox_init_done_; 216 217 // List of input samples waiting to be processed. 218 typedef std::list<base::win::ScopedComPtr<IMFSample>> PendingInputs; 219 PendingInputs pending_input_buffers_; 220 221 // Callback to set the correct gl context. 222 base::Callback<bool(void)> make_context_current_; 223 }; 224 225 } // namespace content 226 227 #endif // CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_ 228