| OLD | NEW |
| (Empty) |
| 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 <d3d11.h> | |
| 9 #include <d3d9.h> | |
| 10 // Work around bug in this header by disabling the relevant warning for it. | |
| 11 // https://connect.microsoft.com/VisualStudio/feedback/details/911260/dxva2api-h
-in-win8-sdk-triggers-c4201-with-w4 | |
| 12 #pragma warning(push) | |
| 13 #pragma warning(disable:4201) | |
| 14 #include <dxva2api.h> | |
| 15 #pragma warning(pop) | |
| 16 #include <list> | |
| 17 #include <map> | |
| 18 #include <mfidl.h> | |
| 19 #include <vector> | |
| 20 | |
| 21 #include "base/compiler_specific.h" | |
| 22 #include "base/macros.h" | |
| 23 #include "base/memory/linked_ptr.h" | |
| 24 #include "base/memory/weak_ptr.h" | |
| 25 #include "base/synchronization/lock.h" | |
| 26 #include "base/threading/non_thread_safe.h" | |
| 27 #include "base/threading/thread.h" | |
| 28 #include "base/win/scoped_comptr.h" | |
| 29 #include "content/common/content_export.h" | |
| 30 #include "media/video/video_decode_accelerator.h" | |
| 31 | |
| 32 interface IMFSample; | |
| 33 interface IDirect3DSurface9; | |
| 34 | |
| 35 namespace gfx { | |
| 36 class GLContext; | |
| 37 } | |
| 38 | |
| 39 typedef HRESULT (WINAPI* CreateDXGIDeviceManager)( | |
| 40 UINT* reset_token, | |
| 41 IMFDXGIDeviceManager** device_manager); | |
| 42 | |
| 43 namespace content { | |
| 44 | |
| 45 // Class to provide a DXVA 2.0 based accelerator using the Microsoft Media | |
| 46 // foundation APIs via the VideoDecodeAccelerator interface. | |
| 47 // This class lives on a single thread and DCHECKs that it is never accessed | |
| 48 // from any other. | |
| 49 class CONTENT_EXPORT DXVAVideoDecodeAccelerator | |
| 50 : public media::VideoDecodeAccelerator { | |
| 51 public: | |
| 52 enum State { | |
| 53 kUninitialized, // un-initialized. | |
| 54 kNormal, // normal playing state. | |
| 55 kResetting, // upon received Reset(), before ResetDone() | |
| 56 kStopped, // upon output EOS received. | |
| 57 kFlushing, // upon flush request received. | |
| 58 }; | |
| 59 | |
| 60 // Does not take ownership of |client| which must outlive |*this|. | |
| 61 explicit DXVAVideoDecodeAccelerator( | |
| 62 const base::Callback<bool(void)>& make_context_current, | |
| 63 gfx::GLContext* gl_context); | |
| 64 ~DXVAVideoDecodeAccelerator() override; | |
| 65 | |
| 66 // media::VideoDecodeAccelerator implementation. | |
| 67 bool Initialize(media::VideoCodecProfile profile, | |
| 68 Client* client) override; | |
| 69 void Decode(const media::BitstreamBuffer& bitstream_buffer) override; | |
| 70 void AssignPictureBuffers( | |
| 71 const std::vector<media::PictureBuffer>& buffers) override; | |
| 72 void ReusePictureBuffer(int32 picture_buffer_id) override; | |
| 73 void Flush() override; | |
| 74 void Reset() override; | |
| 75 void Destroy() override; | |
| 76 bool CanDecodeOnIOThread() override; | |
| 77 GLenum GetSurfaceInternalFormat() const override; | |
| 78 | |
| 79 static media::VideoDecodeAccelerator::SupportedProfiles | |
| 80 GetSupportedProfiles(); | |
| 81 | |
| 82 // Preload dlls required for decoding. | |
| 83 static void PreSandboxInitialization(); | |
| 84 | |
| 85 private: | |
| 86 typedef void* EGLConfig; | |
| 87 typedef void* EGLSurface; | |
| 88 | |
| 89 // Creates and initializes an instance of the D3D device and the | |
| 90 // corresponding device manager. The device manager instance is eventually | |
| 91 // passed to the IMFTransform interface implemented by the decoder. | |
| 92 bool CreateD3DDevManager(); | |
| 93 | |
| 94 // Creates and initializes an instance of the DX11 device and the | |
| 95 // corresponding device manager. The device manager instance is eventually | |
| 96 // passed to the IMFTransform interface implemented by the decoder. | |
| 97 bool CreateDX11DevManager(); | |
| 98 | |
| 99 // Creates, initializes and sets the media codec types for the decoder. | |
| 100 bool InitDecoder(media::VideoCodecProfile profile); | |
| 101 | |
| 102 // Validates whether the decoder supports hardware video acceleration. | |
| 103 bool CheckDecoderDxvaSupport(); | |
| 104 | |
| 105 // Returns information about the input and output streams. This includes | |
| 106 // alignment information, decoder support flags, minimum sample size, etc. | |
| 107 bool GetStreamsInfoAndBufferReqs(); | |
| 108 | |
| 109 // Registers the input and output media types on the decoder. This includes | |
| 110 // the expected input and output formats. | |
| 111 bool SetDecoderMediaTypes(); | |
| 112 | |
| 113 // Registers the input media type for the decoder. | |
| 114 bool SetDecoderInputMediaType(); | |
| 115 | |
| 116 // Registers the output media type for the decoder. | |
| 117 bool SetDecoderOutputMediaType(const GUID& subtype); | |
| 118 | |
| 119 // Passes a command message to the decoder. This includes commands like | |
| 120 // start of stream, end of stream, flush, drain the decoder, etc. | |
| 121 bool SendMFTMessage(MFT_MESSAGE_TYPE msg, int32 param); | |
| 122 | |
| 123 // The bulk of the decoding happens here. This function handles errors, | |
| 124 // format changes and processes decoded output. | |
| 125 void DoDecode(); | |
| 126 | |
| 127 // Invoked when we have a valid decoded output sample. Retrieves the D3D | |
| 128 // surface and maintains a copy of it which is passed eventually to the | |
| 129 // client when we have a picture buffer to copy the surface contents to. | |
| 130 bool ProcessOutputSample(IMFSample* sample); | |
| 131 | |
| 132 // Processes pending output samples by copying them to available picture | |
| 133 // slots. | |
| 134 void ProcessPendingSamples(); | |
| 135 | |
| 136 // Helper function to notify the accelerator client about the error. | |
| 137 void StopOnError(media::VideoDecodeAccelerator::Error error); | |
| 138 | |
| 139 // Transitions the decoder to the uninitialized state. The decoder will stop | |
| 140 // accepting requests in this state. | |
| 141 void Invalidate(); | |
| 142 | |
| 143 // Notifies the client that the input buffer identifed by input_buffer_id has | |
| 144 // been processed. | |
| 145 void NotifyInputBufferRead(int input_buffer_id); | |
| 146 | |
| 147 // Notifies the client that the decoder was flushed. | |
| 148 void NotifyFlushDone(); | |
| 149 | |
| 150 // Notifies the client that the decoder was reset. | |
| 151 void NotifyResetDone(); | |
| 152 | |
| 153 // Requests picture buffers from the client. | |
| 154 void RequestPictureBuffers(int width, int height); | |
| 155 | |
| 156 // Notifies the client about the availability of a picture. | |
| 157 void NotifyPictureReady(int picture_buffer_id, | |
| 158 int input_buffer_id); | |
| 159 | |
| 160 // Sends pending input buffer processed acks to the client if we don't have | |
| 161 // output samples waiting to be processed. | |
| 162 void NotifyInputBuffersDropped(); | |
| 163 | |
| 164 // Decodes pending input buffers. | |
| 165 void DecodePendingInputBuffers(); | |
| 166 | |
| 167 // Helper for handling the Flush operation. | |
| 168 void FlushInternal(); | |
| 169 | |
| 170 // Helper for handling the Decode operation. | |
| 171 void DecodeInternal(const base::win::ScopedComPtr<IMFSample>& input_sample); | |
| 172 | |
| 173 // Handles mid stream resolution changes. | |
| 174 void HandleResolutionChanged(int width, int height); | |
| 175 | |
| 176 struct DXVAPictureBuffer; | |
| 177 typedef std::map<int32, linked_ptr<DXVAPictureBuffer> > OutputBuffers; | |
| 178 | |
| 179 // Tells the client to dismiss the stale picture buffers passed in. | |
| 180 void DismissStaleBuffers(); | |
| 181 | |
| 182 // Called after the client indicates we can recycle a stale picture buffer. | |
| 183 void DeferredDismissStaleBuffer(int32 picture_buffer_id); | |
| 184 | |
| 185 // Sets the state of the decoder. Called from the main thread and the decoder | |
| 186 // thread. The state is changed on the main thread. | |
| 187 void SetState(State state); | |
| 188 | |
| 189 // Gets the state of the decoder. Can be called from the main thread and | |
| 190 // the decoder thread. Thread safe. | |
| 191 State GetState(); | |
| 192 | |
| 193 // Worker function for the Decoder Reset functionality. Executes on the | |
| 194 // decoder thread and queues tasks on the main thread as needed. | |
| 195 void ResetHelper(); | |
| 196 | |
| 197 // Starts the thread used for decoding. | |
| 198 void StartDecoderThread(); | |
| 199 | |
| 200 // Returns if we have output samples waiting to be processed. We only | |
| 201 // allow one output sample to be present in the output queue at any given | |
| 202 // time. | |
| 203 bool OutputSamplesPresent(); | |
| 204 | |
| 205 // Copies the source surface |src_surface| to the destination |dest_surface|. | |
| 206 // The copying is done on the decoder thread. | |
| 207 void CopySurface(IDirect3DSurface9* src_surface, | |
| 208 IDirect3DSurface9* dest_surface, | |
| 209 int picture_buffer_id, | |
| 210 int input_buffer_id); | |
| 211 | |
| 212 // This is a notification that the source surface |src_surface| was copied to | |
| 213 // the destination |dest_surface|. Received on the main thread. | |
| 214 void CopySurfaceComplete(IDirect3DSurface9* src_surface, | |
| 215 IDirect3DSurface9* dest_surface, | |
| 216 int picture_buffer_id, | |
| 217 int input_buffer_id); | |
| 218 | |
| 219 // Copies the source texture |src_texture| to the destination |dest_texture|. | |
| 220 // The copying is done on the decoder thread. The |video_frame| parameter | |
| 221 // is the sample containing the frame to be copied. | |
| 222 void CopyTexture(ID3D11Texture2D* src_texture, | |
| 223 ID3D11Texture2D* dest_texture, | |
| 224 IMFSample* video_frame, | |
| 225 int picture_buffer_id, | |
| 226 int input_buffer_id); | |
| 227 | |
| 228 // Flushes the decoder device to ensure that the decoded surface is copied | |
| 229 // to the target surface. |iterations| helps to maintain an upper limit on | |
| 230 // the number of times we try to complete the flush operation. | |
| 231 void FlushDecoder(int iterations, | |
| 232 IDirect3DSurface9* src_surface, | |
| 233 IDirect3DSurface9* dest_surface, | |
| 234 int picture_buffer_id, | |
| 235 int input_buffer_id); | |
| 236 | |
| 237 // Initializes the DX11 Video format converter media types. | |
| 238 // Returns true on success. | |
| 239 bool InitializeDX11VideoFormatConverterMediaType(int width, int height); | |
| 240 | |
| 241 // Returns the output video frame dimensions (width, height). | |
| 242 // |sample| :- This is the output sample containing the video frame. | |
| 243 // |width| :- The width is returned here. | |
| 244 // |height| :- The height is returned here. | |
| 245 // Returns true on success. | |
| 246 bool GetVideoFrameDimensions(IMFSample* sample, int* width, int* height); | |
| 247 | |
| 248 // To expose client callbacks from VideoDecodeAccelerator. | |
| 249 media::VideoDecodeAccelerator::Client* client_; | |
| 250 | |
| 251 base::win::ScopedComPtr<IMFTransform> decoder_; | |
| 252 base::win::ScopedComPtr<IMFTransform> video_format_converter_mft_; | |
| 253 | |
| 254 base::win::ScopedComPtr<IDirect3D9Ex> d3d9_; | |
| 255 base::win::ScopedComPtr<IDirect3DDevice9Ex> d3d9_device_ex_; | |
| 256 base::win::ScopedComPtr<IDirect3DDeviceManager9> device_manager_; | |
| 257 base::win::ScopedComPtr<IDirect3DQuery9> query_; | |
| 258 | |
| 259 base::win::ScopedComPtr<ID3D11DeviceContext> d3d11_device_context_; | |
| 260 base::win::ScopedComPtr<ID3D11Device > d3d11_device_; | |
| 261 base::win::ScopedComPtr<IMFDXGIDeviceManager> d3d11_device_manager_; | |
| 262 base::win::ScopedComPtr<ID3D11Query> d3d11_query_; | |
| 263 base::win::ScopedComPtr<ID3D10Multithread> multi_threaded_; | |
| 264 | |
| 265 // Ideally the reset token would be a stack variable which is used while | |
| 266 // creating the device manager. However it seems that the device manager | |
| 267 // holds onto the token and attempts to access it if the underlying device | |
| 268 // changes. | |
| 269 // TODO(ananta): This needs to be verified. | |
| 270 uint32 dev_manager_reset_token_; | |
| 271 | |
| 272 // Reset token for the DX11 device manager. | |
| 273 uint32 dx11_dev_manager_reset_token_; | |
| 274 | |
| 275 uint32 dx11_dev_manager_reset_token_format_conversion_; | |
| 276 | |
| 277 // The EGL config to use for decoded frames. | |
| 278 EGLConfig egl_config_; | |
| 279 | |
| 280 // Current state of the decoder. | |
| 281 volatile State state_; | |
| 282 | |
| 283 MFT_INPUT_STREAM_INFO input_stream_info_; | |
| 284 MFT_OUTPUT_STREAM_INFO output_stream_info_; | |
| 285 | |
| 286 // Contains information about a decoded sample. | |
| 287 struct PendingSampleInfo { | |
| 288 PendingSampleInfo(int32 buffer_id, IMFSample* sample); | |
| 289 ~PendingSampleInfo(); | |
| 290 | |
| 291 int32 input_buffer_id; | |
| 292 | |
| 293 // The target picture buffer id where the frame would be copied to. | |
| 294 // Defaults to -1. | |
| 295 int picture_buffer_id; | |
| 296 | |
| 297 base::win::ScopedComPtr<IMFSample> output_sample; | |
| 298 }; | |
| 299 | |
| 300 typedef std::list<PendingSampleInfo> PendingOutputSamples; | |
| 301 | |
| 302 // List of decoded output samples. Protected by |decoder_lock_|. | |
| 303 PendingOutputSamples pending_output_samples_; | |
| 304 | |
| 305 // This map maintains the picture buffers passed the client for decoding. | |
| 306 // The key is the picture buffer id. | |
| 307 OutputBuffers output_picture_buffers_; | |
| 308 | |
| 309 // After a resolution change there may be a few output buffers which have yet | |
| 310 // to be displayed so they cannot be dismissed immediately. We move them from | |
| 311 // |output_picture_buffers_| to this map so they may be dismissed once they | |
| 312 // become available. | |
| 313 OutputBuffers stale_output_picture_buffers_; | |
| 314 | |
| 315 // Set to true if we requested picture slots from the client. | |
| 316 bool pictures_requested_; | |
| 317 | |
| 318 // Counter which holds the number of input packets before a successful | |
| 319 // decode. | |
| 320 int inputs_before_decode_; | |
| 321 | |
| 322 // Set to true when the drain message is sent to the decoder during a flush | |
| 323 // operation. Used to ensure the message is only sent once after | |
| 324 // |pending_input_buffers_| is drained. Protected by |decoder_lock_|. | |
| 325 bool sent_drain_message_; | |
| 326 | |
| 327 // List of input samples waiting to be processed. | |
| 328 typedef std::list<base::win::ScopedComPtr<IMFSample>> PendingInputs; | |
| 329 PendingInputs pending_input_buffers_; | |
| 330 | |
| 331 // Callback to set the correct gl context. | |
| 332 base::Callback<bool(void)> make_context_current_; | |
| 333 | |
| 334 // Which codec we are decoding with hardware acceleration. | |
| 335 media::VideoCodec codec_; | |
| 336 // Thread on which the decoder operations like passing input frames, | |
| 337 // getting output frames are performed. One instance of this thread | |
| 338 // is created per decoder instance. | |
| 339 base::Thread decoder_thread_; | |
| 340 | |
| 341 // Task runner to be used for posting tasks to the decoder thread. | |
| 342 scoped_refptr<base::SingleThreadTaskRunner> decoder_thread_task_runner_; | |
| 343 | |
| 344 // Task runner to be used for posting tasks to the main thread. | |
| 345 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | |
| 346 | |
| 347 // Used to synchronize access between the decoder thread and the main thread. | |
| 348 base::Lock decoder_lock_; | |
| 349 | |
| 350 // Disallow rebinding WeakReference ownership to a different thread by | |
| 351 // keeping a persistent reference. This avoids problems with the | |
| 352 // thread safety of reaching into this class from multiple threads to | |
| 353 // attain a WeakPtr. | |
| 354 base::WeakPtr<DXVAVideoDecodeAccelerator> weak_ptr_; | |
| 355 | |
| 356 // Set to true if we are in the context of a Flush operation. Used to prevent | |
| 357 // multiple flush done notifications being sent out. | |
| 358 bool pending_flush_; | |
| 359 | |
| 360 // Defaults to false. Indicates if we should use D3D or DX11 interfaces for | |
| 361 // H/W decoding. | |
| 362 bool use_dx11_; | |
| 363 | |
| 364 // Set to true if the DX11 video format converter input media types need to | |
| 365 // be initialized. Defaults to true. | |
| 366 bool dx11_video_format_converter_media_type_needs_init_; | |
| 367 | |
| 368 // The GLContext to be used by the decoder. | |
| 369 scoped_refptr<gfx::GLContext> gl_context_; | |
| 370 | |
| 371 // Set to true if we are sharing ANGLE's device. | |
| 372 bool using_angle_device_; | |
| 373 | |
| 374 // WeakPtrFactory for posting tasks back to |this|. | |
| 375 base::WeakPtrFactory<DXVAVideoDecodeAccelerator> weak_this_factory_; | |
| 376 | |
| 377 // Function pointer for the MFCreateDXGIDeviceManager API. | |
| 378 static CreateDXGIDeviceManager create_dxgi_device_manager_; | |
| 379 | |
| 380 DISALLOW_COPY_AND_ASSIGN(DXVAVideoDecodeAccelerator); | |
| 381 }; | |
| 382 | |
| 383 } // namespace content | |
| 384 | |
| 385 #endif // CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_ | |
| OLD | NEW |