OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/threading/non_thread_safe.h" |
| 16 #include "base/win/scoped_comptr.h" |
| 17 #include "media/video/video_decode_accelerator.h" |
| 18 #include "third_party/angle/include/EGL/egl.h" |
| 19 #include "third_party/angle/include/EGL/eglext.h" |
| 20 |
| 21 interface IMFSample; |
| 22 interface IDirect3DSurface9; |
| 23 |
| 24 // Class to provide a DXVA 2.0 based accelerator using the Microsoft Media |
| 25 // foundation APIs via the VideoDecodeAccelerator interface. |
| 26 // This class lives on a single thread and DCHECKs that it is never accessed |
| 27 // from any other. |
| 28 class DXVAVideoDecodeAccelerator : public media::VideoDecodeAccelerator, |
| 29 public base::NonThreadSafe { |
| 30 public: |
| 31 enum State { |
| 32 kUninitialized, // un-initialized. |
| 33 kNormal, // normal playing state. |
| 34 kResetting, // upon received Reset(), before ResetDone() |
| 35 kEosDrain, // upon input EOS received. |
| 36 kStopped, // upon output EOS received. |
| 37 }; |
| 38 |
| 39 // Does not take ownership of |client| which must outlive |*this|. |
| 40 DXVAVideoDecodeAccelerator( |
| 41 media::VideoDecodeAccelerator::Client* client, |
| 42 base::ProcessHandle renderer_process); |
| 43 virtual ~DXVAVideoDecodeAccelerator(); |
| 44 |
| 45 // media::VideoDecodeAccelerator implementation. |
| 46 virtual bool Initialize(Profile) OVERRIDE; |
| 47 virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE; |
| 48 virtual void AssignPictureBuffers( |
| 49 const std::vector<media::PictureBuffer>& buffers) OVERRIDE; |
| 50 virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE; |
| 51 virtual void Flush() OVERRIDE; |
| 52 virtual void Reset() OVERRIDE; |
| 53 virtual void Destroy() OVERRIDE; |
| 54 |
| 55 // Initialization work needed before the process is sandboxed. |
| 56 // This includes:- |
| 57 // 1. Loads the dlls like mf/mfplat/d3d9, etc required for decoding. |
| 58 // 2. Setting up the device manager instance which is shared between all |
| 59 // decoder instances. |
| 60 static void PreSandboxInitialization(); |
| 61 |
| 62 private: |
| 63 // Creates and initializes an instance of the D3D device and the |
| 64 // corresponding device manager. The device manager instance is eventually |
| 65 // passed to the IMFTransform interface implemented by the h.264 decoder. |
| 66 static bool CreateD3DDevManager(); |
| 67 |
| 68 // Creates, initializes and sets the media types for the h.264 decoder. |
| 69 bool InitDecoder(); |
| 70 |
| 71 // Validates whether the h.264 decoder supports hardware video acceleration. |
| 72 bool CheckDecoderDxvaSupport(); |
| 73 |
| 74 // Returns information about the input and output streams. This includes |
| 75 // alignment information, decoder support flags, minimum sample size, etc. |
| 76 bool GetStreamsInfoAndBufferReqs(); |
| 77 |
| 78 // Registers the input and output media types on the h.264 decoder. This |
| 79 // includes the expected input and output formats. |
| 80 bool SetDecoderMediaTypes(); |
| 81 |
| 82 // Registers the input media type for the h.264 decoder. |
| 83 bool SetDecoderInputMediaType(); |
| 84 |
| 85 // Registers the output media type for the h.264 decoder. |
| 86 bool SetDecoderOutputMediaType(const GUID& subtype); |
| 87 |
| 88 // Passes a command message to the decoder. This includes commands like |
| 89 // start of stream, end of stream, flush, drain the decoder, etc. |
| 90 bool SendMFTMessage(MFT_MESSAGE_TYPE msg, int32 param); |
| 91 |
| 92 // The bulk of the decoding happens here. This function handles errors, |
| 93 // format changes and processes decoded output. |
| 94 void DoDecode(); |
| 95 |
| 96 // Invoked when we have a valid decoded output sample. Retrieves the D3D |
| 97 // surface and maintains a copy of it which is passed eventually to the |
| 98 // client when we have a picture buffer to copy the surface contents to. |
| 99 bool ProcessOutputSample(IMFSample* sample); |
| 100 |
| 101 // Copies the output sample data to the picture buffer provided by the |
| 102 // client. |
| 103 bool CopyOutputSampleDataToPictureBuffer(IDirect3DSurface9* dest_surface, |
| 104 media::PictureBuffer picture_buffer, |
| 105 int32 input_buffer_id); |
| 106 |
| 107 // Processes pending output samples by copying them to available picture |
| 108 // slots. |
| 109 void ProcessPendingSamples(); |
| 110 |
| 111 // Clears local state maintained by the decoder. |
| 112 void ClearState(); |
| 113 |
| 114 // Helper function to notify the accelerator client about the error. |
| 115 void StopOnError(media::VideoDecodeAccelerator::Error error); |
| 116 |
| 117 // Transitions the decoder to the uninitialized state. The decoder will stop |
| 118 // accepting requests in this state. |
| 119 void Invalidate(); |
| 120 |
| 121 // Helper function to read the bitmap from the D3D surface passed in. |
| 122 bool GetBitmapFromSurface(IDirect3DSurface9* surface, |
| 123 scoped_array<char>* bits); |
| 124 |
| 125 // Notifies the client that the input buffer identifed by input_buffer_id has |
| 126 // been processed. |
| 127 void NotifyInputBufferRead(int input_buffer_id); |
| 128 |
| 129 // Notifies the client that initialize was completed. |
| 130 void NotifyInitializeDone(); |
| 131 |
| 132 // Notifies the client that the decoder was flushed. |
| 133 void NotifyFlushDone(); |
| 134 |
| 135 // Notifies the client that the decoder was reset. |
| 136 void NotifyResetDone(); |
| 137 |
| 138 // Requests picture buffers from the client. |
| 139 void RequestPictureBuffers(int width, int height); |
| 140 |
| 141 // Notifies the client about the availability of a picture. |
| 142 void NotifyPictureReady(const media::Picture& picture); |
| 143 |
| 144 // To expose client callbacks from VideoDecodeAccelerator. |
| 145 media::VideoDecodeAccelerator::Client* client_; |
| 146 |
| 147 base::win::ScopedComPtr<IMFTransform> decoder_; |
| 148 |
| 149 // These interface pointers are initialized before the process is sandboxed. |
| 150 // They are not released when the GPU process exits. This is ok for now |
| 151 // because the GPU process does not exit normally on Windows. It is always |
| 152 // terminated. The device manager instance is shared among all decoder |
| 153 // instances. This is OK because there is internal locking performed by the |
| 154 // device manager. |
| 155 static IDirect3DDeviceManager9* device_manager_; |
| 156 static IDirect3DDevice9Ex* device_; |
| 157 |
| 158 // Current state of the decoder. |
| 159 State state_; |
| 160 |
| 161 MFT_INPUT_STREAM_INFO input_stream_info_; |
| 162 MFT_OUTPUT_STREAM_INFO output_stream_info_; |
| 163 |
| 164 // Contains information about a decoded sample. |
| 165 struct PendingSampleInfo { |
| 166 PendingSampleInfo(int32 buffer_id, IDirect3DSurface9* surface); |
| 167 ~PendingSampleInfo(); |
| 168 |
| 169 int32 input_buffer_id; |
| 170 base::win::ScopedComPtr<IDirect3DSurface9> dest_surface; |
| 171 }; |
| 172 |
| 173 typedef std::list<PendingSampleInfo> PendingOutputSamples; |
| 174 |
| 175 // List of decoded output samples. |
| 176 PendingOutputSamples pending_output_samples_; |
| 177 |
| 178 // Maintains information about a DXVA picture buffer, i.e. whether it is |
| 179 // available for rendering, the texture information, etc. |
| 180 struct DXVAPictureBuffer { |
| 181 explicit DXVAPictureBuffer(const media::PictureBuffer& buffer); |
| 182 |
| 183 bool available; |
| 184 media::PictureBuffer picture_buffer; |
| 185 }; |
| 186 |
| 187 // This map maintains the picture buffers passed the client for decoding. |
| 188 // The key is the picture buffer id. |
| 189 typedef std::map<int32, DXVAPictureBuffer> OutputBuffers; |
| 190 OutputBuffers output_picture_buffers_; |
| 191 |
| 192 // Set to true if we requested picture slots from the client. |
| 193 bool pictures_requested_; |
| 194 |
| 195 // Contains the id of the last input buffer received from the client. |
| 196 int32 last_input_buffer_id_; |
| 197 |
| 198 // Handle to the renderer process. |
| 199 base::ProcessHandle renderer_process_; |
| 200 |
| 201 // Ideally the reset token would be a stack variable which is used while |
| 202 // creating the device manager. However it seems that the device manager |
| 203 // holds onto the token and attempts to access it if the underlying device |
| 204 // changes. |
| 205 // TODO(ananta): This needs to be verified. |
| 206 static uint32 dev_manager_reset_token_; |
| 207 |
| 208 // Counter which holds the number of input packets before a successful |
| 209 // decode. |
| 210 int inputs_before_decode_; |
| 211 |
| 212 // Set to true if all necessary initialization needed before the GPU process |
| 213 // is sandboxed is done. |
| 214 // This includes the following: |
| 215 // 1. All required decoder dlls were successfully loaded. |
| 216 // 2. The device manager initialization completed. |
| 217 static bool pre_sandbox_init_done_; |
| 218 }; |
| 219 |
| 220 #endif // CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_ |
| 221 |
OLD | NEW |