• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/memory/weak_ptr.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "base/win/scoped_comptr.h"
20 #include "content/common/content_export.h"
21 #include "media/video/video_decode_accelerator.h"
22 
23 interface IMFSample;
24 interface IDirect3DSurface9;
25 
26 namespace content {
27 
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
31 // from any other.
32 class CONTENT_EXPORT DXVAVideoDecodeAccelerator
33     : public media::VideoDecodeAccelerator,
34       NON_EXPORTED_BASE(public base::NonThreadSafe) {
35  public:
36   enum State {
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.
42   };
43 
44   // Does not take ownership of |client| which must outlive |*this|.
45   explicit DXVAVideoDecodeAccelerator(
46       const base::Callback<bool(void)>& make_context_current);
47   virtual ~DXVAVideoDecodeAccelerator();
48 
49   // media::VideoDecodeAccelerator implementation.
50   virtual bool Initialize(media::VideoCodecProfile profile,
51                           Client* client) 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;
59   virtual bool CanDecodeOnIOThread() OVERRIDE;
60 
61  private:
62   typedef void* EGLConfig;
63   typedef void* EGLSurface;
64   // Creates and initializes an instance of the D3D device and the
65   // corresponding device manager. The device manager instance is eventually
66   // passed to the IMFTransform interface implemented by the h.264 decoder.
67   bool CreateD3DDevManager();
68 
69   // Creates, initializes and sets the media types for the h.264 decoder.
70   bool InitDecoder(media::VideoCodecProfile profile);
71 
72   // Validates whether the h.264 decoder supports hardware video acceleration.
73   bool CheckDecoderDxvaSupport();
74 
75   // Returns information about the input and output streams. This includes
76   // alignment information, decoder support flags, minimum sample size, etc.
77   bool GetStreamsInfoAndBufferReqs();
78 
79   // Registers the input and output media types on the h.264 decoder. This
80   // includes the expected input and output formats.
81   bool SetDecoderMediaTypes();
82 
83   // Registers the input media type for the h.264 decoder.
84   bool SetDecoderInputMediaType();
85 
86   // Registers the output media type for the h.264 decoder.
87   bool SetDecoderOutputMediaType(const GUID& subtype);
88 
89   // Passes a command message to the decoder. This includes commands like
90   // start of stream, end of stream, flush, drain the decoder, etc.
91   bool SendMFTMessage(MFT_MESSAGE_TYPE msg, int32 param);
92 
93   // The bulk of the decoding happens here. This function handles errors,
94   // format changes and processes decoded output.
95   void DoDecode();
96 
97   // Invoked when we have a valid decoded output sample. Retrieves the D3D
98   // surface and maintains a copy of it which is passed eventually to the
99   // client when we have a picture buffer to copy the surface contents to.
100   bool ProcessOutputSample(IMFSample* sample);
101 
102   // Processes pending output samples by copying them to available picture
103   // slots.
104   void ProcessPendingSamples();
105 
106   // Helper function to notify the accelerator client about the error.
107   void StopOnError(media::VideoDecodeAccelerator::Error error);
108 
109   // Transitions the decoder to the uninitialized state. The decoder will stop
110   // accepting requests in this state.
111   void Invalidate();
112 
113   // Notifies the client that the input buffer identifed by input_buffer_id has
114   // been processed.
115   void NotifyInputBufferRead(int input_buffer_id);
116 
117   // Notifies the client that the decoder was flushed.
118   void NotifyFlushDone();
119 
120   // Notifies the client that the decoder was reset.
121   void NotifyResetDone();
122 
123   // Requests picture buffers from the client.
124   void RequestPictureBuffers(int width, int height);
125 
126   // Notifies the client about the availability of a picture.
127   void NotifyPictureReady(const media::Picture& picture);
128 
129   // Sends pending input buffer processed acks to the client if we don't have
130   // output samples waiting to be processed.
131   void NotifyInputBuffersDropped();
132 
133   // Decodes pending input buffers.
134   void DecodePendingInputBuffers();
135 
136   // Helper for handling the Flush operation.
137   void FlushInternal();
138 
139   // Helper for handling the Decode operation.
140   void DecodeInternal(const base::win::ScopedComPtr<IMFSample>& input_sample);
141 
142   // Handles mid stream resolution changes.
143   void HandleResolutionChanged(int width, int height);
144 
145   struct DXVAPictureBuffer;
146   typedef std::map<int32, linked_ptr<DXVAPictureBuffer> > OutputBuffers;
147 
148   // Tells the client to dismiss the stale picture buffers passed in.
149   void DismissStaleBuffers(const OutputBuffers& picture_buffers);
150 
151   // To expose client callbacks from VideoDecodeAccelerator.
152   media::VideoDecodeAccelerator::Client* client_;
153 
154   base::win::ScopedComPtr<IMFTransform> decoder_;
155 
156   base::win::ScopedComPtr<IDirect3D9Ex> d3d9_;
157   base::win::ScopedComPtr<IDirect3DDevice9Ex> device_;
158   base::win::ScopedComPtr<IDirect3DDeviceManager9> device_manager_;
159   base::win::ScopedComPtr<IDirect3DQuery9> query_;
160   // Ideally the reset token would be a stack variable which is used while
161   // creating the device manager. However it seems that the device manager
162   // holds onto the token and attempts to access it if the underlying device
163   // changes.
164   // TODO(ananta): This needs to be verified.
165   uint32 dev_manager_reset_token_;
166 
167   // The EGL config to use for decoded frames.
168   EGLConfig egl_config_;
169 
170   // Current state of the decoder.
171   State state_;
172 
173   MFT_INPUT_STREAM_INFO input_stream_info_;
174   MFT_OUTPUT_STREAM_INFO output_stream_info_;
175 
176   // Contains information about a decoded sample.
177   struct PendingSampleInfo {
178     PendingSampleInfo(int32 buffer_id, IMFSample* sample);
179     ~PendingSampleInfo();
180 
181     int32 input_buffer_id;
182     base::win::ScopedComPtr<IMFSample> output_sample;
183   };
184 
185   typedef std::list<PendingSampleInfo> PendingOutputSamples;
186 
187   // List of decoded output samples.
188   PendingOutputSamples pending_output_samples_;
189 
190   // This map maintains the picture buffers passed the client for decoding.
191   // The key is the picture buffer id.
192   OutputBuffers output_picture_buffers_;
193 
194   // Set to true if we requested picture slots from the client.
195   bool pictures_requested_;
196 
197   // Counter which holds the number of input packets before a successful
198   // decode.
199   int inputs_before_decode_;
200 
201   // List of input samples waiting to be processed.
202   typedef std::list<base::win::ScopedComPtr<IMFSample>> PendingInputs;
203   PendingInputs pending_input_buffers_;
204 
205   // Callback to set the correct gl context.
206   base::Callback<bool(void)> make_context_current_;
207 
208   // WeakPtrFactory for posting tasks back to |this|.
209   base::WeakPtrFactory<DXVAVideoDecodeAccelerator> weak_this_factory_;
210 };
211 
212 }  // namespace content
213 
214 #endif  // CONTENT_COMMON_GPU_MEDIA_DXVA_VIDEO_DECODE_ACCELERATOR_H_
215