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