Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(388)

Side by Side Diff: content/common/gpu/media/dxva_video_decode_accelerator.cc

Issue 8510039: Initial implementation of the DXVA 2.0 H.264 hardware decoder for pepper for Windows. The decodin... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Assert this file is only built on OS_WIN: #if !def
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
5 #include "content/common/gpu/media/dxva_video_decode_accelerator.h"
6
7 #include <ks.h>
8 #include <codecapi.h>
9 #include <d3dx9tex.h>
10 #include <mfapi.h>
11 #include <mferror.h>
12 #include <wmcodecdsp.h>
13
14 #include "base/lazy_instance.h"
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 remove
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
15 #include "base/logging.h"
16 #include "base/memory/scoped_handle.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop.h"
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 can remove if you take my NonThreadSafe suggestion
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
19 #include "base/shared_memory.h"
20 #include "base/time.h"
21 #include "media/video/video_decode_accelerator.h"
22 #include "third_party/angle/include/GLES2/gl2.h"
23 #include "third_party/angle/include/GLES2/gl2ext.h"
24
25 namespace {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Since almost all contents of this are static and a
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
26
27 static const int kNumPictureBuffers = 5;
28 static const int kNumInputs = 100;
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Are these magic DXVA constants? Where do they com
29
30 IMFSample* CreateEmptySample() {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 static
ananta 2011/12/13 23:29:15 Done.
31 HRESULT hr = E_FAIL;
32 base::win::ScopedComPtr<IMFSample> sample;
33 hr = MFCreateSample(sample.Receive());
34 if (FAILED(hr)) {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 This seems like a pretty common pattern (similar t
ananta 2011/12/13 23:29:15 Done.
35 NOTREACHED() << "Unable to create an empty sample";
36 return NULL;
37 }
38 return sample.Detach();
39 }
40
41 // Creates a Media Foundation sample with one buffer of length |buffer_length|
42 // on a |align|-byte boundary. Alignment must be a perfect power of 2 or 0.
43 // If |align| is 0, then no alignment is specified.
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 drop this line?
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
44 IMFSample* CreateEmptySampleWithBuffer(int buffer_length, int align) {
45 CHECK_GT(buffer_length, 0);
46 base::win::ScopedComPtr<IMFSample> sample;
47 sample.Attach(CreateEmptySample());
48 if (!sample.get())
49 return NULL;
50 base::win::ScopedComPtr<IMFMediaBuffer> buffer;
51 HRESULT hr = E_FAIL;
52 if (align == 0) {
53 // Note that MFCreateMemoryBuffer is same as MFCreateAlignedMemoryBuffer
54 // with the align argument being 0.
55 hr = MFCreateMemoryBuffer(buffer_length, buffer.Receive());
56 } else {
57 hr = MFCreateAlignedMemoryBuffer(buffer_length,
58 align - 1,
59 buffer.Receive());
60 }
61 if (FAILED(hr)) {
62 NOTREACHED() << "Unable to create an empty buffer";
63 return NULL;
64 }
65 hr = sample->AddBuffer(buffer.get());
66 if (FAILED(hr)) {
67 NOTREACHED() << "Failed to add empty buffer to sample";
68 return NULL;
69 }
70 return sample.Detach();
71 }
72
73 // Creates a Media Foundation sample with one buffer containing a copy of the
74 // given Annex B stream data.
75 // If duration and sample time are not known, provide 0.
76 // |min_size| specifies the minimum size of the buffer (might be required by
77 // the decoder for input). The times here should be given in 100ns units.
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Units of 100ns is pretty unchrome-y. Is it not a
ananta 2011/12/13 23:29:15 Unnecessary now. Removed these parameters as we do
ananta 2011/12/13 23:29:15 Removed these parameters as they are not needed.
78 // |alignment| specifies the buffer in the sample to be aligned. If no
79 // alignment is required, provide 0 or 1.
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 "0 or 1"? Any reason to allow/suggest both?
ananta 2011/12/13 23:29:15 Bad comment. Fixed.
ananta 2011/12/13 23:29:15 Fixed
80 static IMFSample* CreateInputSample(const uint8* stream, int size,
81 int64 timestamp, int64 duration,
82 int min_size, int alignment) {
83 CHECK(stream);
84 CHECK_GT(size, 0);
85 base::win::ScopedComPtr<IMFSample> sample;
86 sample.Attach(CreateEmptySampleWithBuffer(std::max(min_size, size),
87 alignment));
88 if (!sample.get()) {
89 NOTREACHED() << "Failed to create empty buffer for input";
90 return NULL;
91 }
92 HRESULT hr = E_FAIL;
93 if (duration > 0) {
94 hr = sample->SetSampleDuration(duration);
95 if (FAILED(hr)) {
96 NOTREACHED() << "Failed to set sample duration";
97 return NULL;
98 }
99 }
100 if (timestamp > 0) {
101 hr = sample->SetSampleTime(timestamp);
102 if (FAILED(hr)) {
103 NOTREACHED() << "Failed to set sample time";
104 return NULL;
105 }
106 }
107 base::win::ScopedComPtr<IMFMediaBuffer> buffer;
108 hr = sample->GetBufferByIndex(0, buffer.Receive());
109 if (FAILED(hr)) {
110 NOTREACHED() << "Failed to get buffer in sample";
111 return NULL;
112 }
113 DWORD max_length = 0, current_length = 0;
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 comma is unchrome-y.
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
114 uint8* destination = NULL;
115 hr = buffer->Lock(&destination, &max_length, &current_length);
116 if (FAILED(hr)) {
117 NOTREACHED() << "Failed to lock buffer";
118 return NULL;
119 }
120 CHECK_EQ(current_length, 0u);
121 CHECK_GE(static_cast<int>(max_length), size);
122 memcpy(destination, stream, size);
123 CHECK(SUCCEEDED(buffer->Unlock()));
124 hr = buffer->SetCurrentLength(size);
125 if (FAILED(hr)) {
126 NOTREACHED() << "Failed to set current length to " << size;
127 return NULL;
128 }
129 hr = sample->SetUINT32(MFSampleExtension_CleanPoint, TRUE);
130 if (FAILED(hr)) {
131 NOTREACHED() << "Failed to mark sample as key sample";
132 return NULL;
133 }
134 return sample.Detach();
135 }
136
137 } // namespace
138
139 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator(
140 media::VideoDecodeAccelerator::Client* client,
141 base::ProcessHandle renderer_process)
142 : client_(client),
143 message_loop_(MessageLoop::current()->message_loop_proxy()),
144 surface_width_(0),
145 surface_height_(0),
146 state_(kUninitialized),
147 input_stream_info_(),
148 output_stream_info_(),
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 This and the previous line are unnecessary, I thin
ananta 2011/12/13 23:29:15 Done.
ananta 2011/12/13 23:29:15 Done.
149 pictures_requested_(false),
150 renderer_process_(renderer_process),
151 dev_manager_reset_token_(0) {
152 input_buffer_frame_times_.reserve(kNumInputs);
153
154 #if !defined(NDEBUG)
155 decode_start_time_ = 0;
156 inputs_before_decode_ = 0;
157 #endif // !defined(NDEBUG)
158 }
159
160 DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() {
161 client_ = NULL;
162 message_loop_ = NULL;
163 }
164
165 bool DXVAVideoDecodeAccelerator::Initialize(Profile profile) {
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 profile is unused in this file. Does the MF decod
ananta 2011/12/13 23:29:15 We don't need to pass it specifically.
166 DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
167
168 if (state_ != kUninitialized) {
169 NOTREACHED() << "Initialize: invalid state: "
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 One line.
ananta 2011/12/13 23:29:15 Done.
170 << state_;
171 return false;
172 }
173
174 HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL);
175 if (FAILED(hr)) {
176 NOTREACHED() << "MFStartup failed. Error:"
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Single line (here and below; I'm going to stop com
ananta 2011/12/13 23:29:15 Done.
177 << std::hex << std::showbase << hr;
cpu_(ooo_6.6-7.5) 2011/12/13 19:28:57 is the object behind NOTREACHED an actual iostream
Ami GONE FROM CHROMIUM 2011/12/13 19:30:23 Yes; NOTREACHED is (always) a DCHECK(false) under
178 return false;
179 }
180 if (!CreateD3DDevManager())
181 return false;
182 if (!InitDecoder())
183 return false;
184 if (!GetStreamsInfoAndBufferReqs())
185 return false;
186 if (SendMFTMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)) {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 If you reverse the test (!) you get to: - drop the
ananta 2011/12/13 23:29:15 Done.
187 state_ = DXVAVideoDecodeAccelerator::kNormal;
188 client_->NotifyInitializeDone();
189 return true;
190 }
191 return false;
192 }
193
194 void DXVAVideoDecodeAccelerator::Decode(
195 const media::BitstreamBuffer& bitstream_buffer) {
196 DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
197
198 if (state_ == DXVAVideoDecodeAccelerator::kUninitialized) {
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 What about the other states (resetting/flushing)?
ananta 2011/12/13 23:29:15 Will do that once this change clears up. Fixed thi
199 NOTREACHED() << "ConsumeVideoSample: invalid state";
200 return;
201 }
202
203 HANDLE shared_memory_handle = NULL;
204 if (!::DuplicateHandle(renderer_process_,
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Is this really necessary? I expected the fact tha
apatrick_chromium 2011/12/13 19:51:01 Last time I checked, the IPC system didn't know ho
ananta 2011/12/13 23:29:15 The shared memory handle is not duplicated into th
205 bitstream_buffer.handle(),
206 ::GetCurrentProcess(),
207 &shared_memory_handle,
208 0,
209 FALSE,
210 DUPLICATE_SAME_ACCESS)) {
211 NOTREACHED() << "Failed to open duplicate shared mem handle";
212 return;
213 }
214
215 base::SharedMemory shm(shared_memory_handle, true);
216 if (!shm.Map(bitstream_buffer.size())) {
217 NOTREACHED() << "Failed in SharedMemory::Map()";
218 return;
219 }
220
221 base::Time::Exploded exploded;
222 base::Time::Now().LocalExplode(&exploded);
223 base::win::ScopedComPtr<IMFSample> sample;
224 sample.Attach(CreateInputSample(reinterpret_cast<const uint8*>(shm.memory()),
225 bitstream_buffer.size(),
226 exploded.second * 10000000,
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Wait, what? This is putting Now() into the decode
ananta 2011/12/13 23:29:15 Removed. I had put that only for debugging i.e. to
227 400000,
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 Where does 400k come from?
ananta 2011/12/13 23:29:15 From thin air. :) Removed this parameter.
228 input_stream_info_.cbSize,
229 input_stream_info_.cbAlignment));
230 if (!sample.get()) {
231 NOTREACHED() << "Failed to create an input sample";
232 return;
233 } else {
234 #if !defined(NDEBUG)
235 inputs_before_decode_++;
236 if (!decode_start_time_)
237 decode_start_time_ = ::GetTickCount();
238 #endif // !defined(NDEBUG)
239 SendMFTMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 No error-checking necessary?
ananta 2011/12/13 23:29:15 Done.
240
241 if (FAILED(decoder_->ProcessInput(0, sample.get(), 0))) {
242 NOTREACHED() << "Failed to process input";
243 return;
244 }
245 }
246 if (state_ != DXVAVideoDecodeAccelerator::kEosDrain) {
247 if (!SendMFTMessage(MFT_MESSAGE_NOTIFY_END_OF_STREAM, 0)) {
248 NOTREACHED() << "Failed to send eos message to MFT";
249 } else {
250 state_ = DXVAVideoDecodeAccelerator::kEosDrain;
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 You're draining the decoder after each sample? Th
ananta 2011/12/13 23:29:15 I am not draining the decoder. The state is marked
251 }
252 }
253 input_buffer_frame_times_.push_back(bitstream_buffer.id());
254 DoDecode();
255 client_->NotifyEndOfBitstreamBuffer(bitstream_buffer.id());
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 This is supposed to be called when the buffer has
ananta 2011/12/13 23:29:15 We already handed the decoder the buffer to be dec
256 }
257
258 void DXVAVideoDecodeAccelerator::AssignPictureBuffers(
259 const std::vector<media::PictureBuffer>& buffers) {
260 DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
261 // Copy the picture buffers provided by the client to the available list,
262 // and mark these buffers as available for use.
263 for (size_t buffer_index = 0; buffer_index < buffers.size(); ++buffer_index) {
264 DXVAPictureBuffer picture_buffer;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Use the ctor instead (but see below and just drop
ananta 2011/12/13 23:29:15 Done.
265 picture_buffer.available = true;
266 picture_buffer.picture_buffer = buffers[buffer_index];
267
268 DCHECK(available_pictures_.find(buffers[buffer_index].id()) ==
269 available_pictures_.end());
270 available_pictures_[buffers[buffer_index].id()] = picture_buffer;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 A more efficient way is to replace the above 3 lin
ananta 2011/12/13 23:29:15 The find above is just checking whether the pictur
271 }
272 int buffer_index = 0;
273 PendingOutputSamples::iterator sample_index =
274 pending_output_samples_.begin();
275 HRESULT hr = E_FAIL;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 None of the above 4 lines are used. I would have e
ananta 2011/12/13 23:29:15 Dunno why. These statements were left out by mista
276
277 ProcessPendingSamples();
278 }
279
280 void DXVAVideoDecodeAccelerator::ReusePictureBuffer(
281 int32 picture_buffer_id) {
282 DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
283
284 DCHECK(available_pictures_.find(picture_buffer_id) !=
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 FWIW, instead of find() != end(), it's shorter to
ananta 2011/12/13 23:29:15 Done.
285 available_pictures_.end());
286 available_pictures_[picture_buffer_id].available = true;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Since you end up needing this several more times i
ananta 2011/12/13 23:29:15 Done.
287
288 PendingOutputSamples::iterator sample_index =
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This would be clearer as if (pending_output_sampl
ananta 2011/12/13 23:29:15 Done.
289 pending_output_samples_.begin();
290
291 if (sample_index != pending_output_samples_.end()) {
292 const PendingSampleInfo& sample_info = *sample_index;
293 CopyOutputSampleDataToPictureBuffer(
294 sample_info.surface,
295 available_pictures_[picture_buffer_id].picture_buffer,
296 sample_info.input_buffer_id);
297 available_pictures_[picture_buffer_id].available = false;
298 pending_output_samples_.erase(sample_index);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 pop_front if you take my suggestion above (and I'd
ananta 2011/12/13 23:29:15 Ignoring this as I took your above suggestion to c
299 }
300 }
301
302 void DXVAVideoDecodeAccelerator::Flush() {
303 DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
304
305 VLOG(1) << "DXVAVideoDecodeAccelerator::Flush";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 FWIW s/VLOG/DVLOG/g everywhere in this CL (see chr
306 if (state_ == DXVAVideoDecodeAccelerator::kUninitialized) {
307 NOTREACHED() << "ConsumeVideoSample: invalid state";
308 client_->NotifyFlushDone();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 It is generally not OK to call PPP_* methods from
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Shouldn't this be a NotifyError(ILLEGAL_STATE)? In
ananta 2011/12/13 23:29:15 Done.
309 return;
310 }
311
312 if (state_ == DXVAVideoDecodeAccelerator::kStopped) {
313 VLOG(1) << "No data available from the decoder";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This doesn't seem to be worthy of a VLOG to me.
ananta 2011/12/13 23:29:15 Rewrote this function. PTAL.
314 client_->NotifyFlushDone();
315 return;
316 }
317
318 state_ = DXVAVideoDecodeAccelerator::kEosDrain;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 qualification unnecessary (here and elsewhere belo
ananta 2011/12/13 23:29:15 Done.
319 if (!SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0)) {
320 LOG(WARNING) << "Failed to send drain message";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Consistency with other MF failures.
ananta 2011/12/13 23:29:15 This function has been rewritten to make it look b
321 state_ = DXVAVideoDecodeAccelerator::kStopped;
322 client_->NotifyFlushDone();
323 return;
324 }
325
326 // As per MSDN docs after the client sends this message, it calls
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 s/this/the above MFT_MESSAGE_COMMAND_DRAIN/ ?
ananta 2011/12/13 23:29:15 Yes.
327 // IMFTransform::ProcessOutput in a loop, until ProcessOutput returns the
328 // error code MF_E_TRANSFORM_NEED_MORE_INPUT. The DoDecode function sets the
329 // state to DXVAVideoDecodeAccelerator::kStopped when the decoder returns
330 // MF_E_TRANSFORM_NEED_MORE_INPUT.
331 while (state_ != DXVAVideoDecodeAccelerator::kStopped) {
332 DoDecode();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 I guess DoDecode() really is synchronous. Are you
ananta 2011/12/13 23:29:15 Worried a little yes. However the MFT decoder uses
333 }
334 client_->NotifyFlushDone();
335 }
336
337 void DXVAVideoDecodeAccelerator::Reset() {
338 DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
339
340 VLOG(1) << "DXVAVideoDecodeAccelerator::Reset";
341 if (state_ != kNormal && state_ != kStopped) {
342 NOTREACHED() << "Reset: invalid state";
343 client_->NotifyResetDone();
344 return;
345 }
346
347 state_ = DXVAVideoDecodeAccelerator::kResetting;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Whenever you have code like this that checks that
348 if (!SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0)) {
349 LOG(WARNING) << "DXVAVideoDecodeAccelerator::Flush failed to send message";
350 client_->NotifyResetDone();
351 return;
352 }
353 state_ = DXVAVideoDecodeAccelerator::kNormal;
354 client_->NotifyResetDone();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This should be the last call in the method.
ananta 2011/12/13 23:29:15 Done.
355 input_buffer_frame_times_.clear();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 What about the rest of the pending state?
ananta 2011/12/13 23:29:15 Done.
356 }
357
358 void DXVAVideoDecodeAccelerator::Destroy() {
359 DCHECK_EQ(message_loop_, MessageLoop::current()->message_loop_proxy());
360
361 OutputBuffers::iterator index;
362 for (index = available_pictures_.begin(); index != available_pictures_.end();
363 ++index) {
364 client_->DismissPictureBuffer(index->second.picture_buffer.id());
365 }
366 available_pictures_.clear();
367 pending_output_samples_.clear();
368 input_buffer_frame_times_.clear();
369 }
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Does nothing need to be done to tear down the deco
370
371 bool DXVAVideoDecodeAccelerator::CreateD3DDevManager() {
372 base::win::ScopedComPtr<IDirect3D9Ex> d3d9;
373
374 Direct3DCreate9Ex(D3D_SDK_VERSION, d3d9.Receive());
apatrick_chromium 2011/12/13 19:51:01 If the delay loading of d3d9.dll doesn't work for
375 if (d3d9.get() == NULL) {
376 NOTREACHED() << "Failed to create D3D9";
377 return false;
378 }
379
380 D3DPRESENT_PARAMETERS present_params = {0};
381 present_params.BackBufferWidth = 1;
382 present_params.BackBufferHeight = 1;
383 present_params.BackBufferFormat = D3DFMT_UNKNOWN;
384 present_params.BackBufferCount = 1;
385 present_params.SwapEffect = D3DSWAPEFFECT_DISCARD;
386 present_params.hDeviceWindow = GetShellWindow();
apatrick_chromium 2011/12/13 19:51:01 Question for cpu: what will GetShellWindow() do in
387 present_params.Windowed = TRUE;
388 present_params.Flags = D3DPRESENTFLAG_VIDEO;
389 present_params.FullScreen_RefreshRateInHz = 0;
390 present_params.PresentationInterval = 0;
391
392 HRESULT hr = d3d9->CreateDeviceEx(D3DADAPTER_DEFAULT,
393 D3DDEVTYPE_HAL,
394 GetShellWindow(),
395 D3DCREATE_MIXED_VERTEXPROCESSING,
apatrick_chromium 2011/12/13 19:51:01 I think MIXED won't work on cards that do not supp
396 &present_params,
397 NULL,
398 device_.Receive());
399 if (FAILED(hr)) {
400 NOTREACHED() << "Failed to create D3D Device";
401 return false;
402 }
403
404 hr = DXVA2CreateDirect3DDeviceManager9(&dev_manager_reset_token_,
405 device_manager_.Receive());
406 if (FAILED(hr)) {
407 NOTREACHED() << "Couldn't create D3D Device manager";
408 return false;
409 }
410
411 hr = device_manager_->ResetDevice(device_.get(),
412 dev_manager_reset_token_);
413 if (FAILED(hr)) {
414 NOTREACHED() << "Failed to set device to device manager";
415 return false;
416 }
417 return true;
418 }
419
420
421 bool DXVAVideoDecodeAccelerator::InitDecoder() {
422 HRESULT hr = CoCreateInstance(__uuidof(CMSH264DecoderMFT),
423 NULL,
424 CLSCTX_INPROC_SERVER,
425 __uuidof(IMFTransform),
426 reinterpret_cast<void**>(decoder_.Receive()));
427 if (FAILED(hr) || !decoder_.get()) {
428 NOTREACHED() << "CoCreateInstance failed "
429 << std::hex << std::showbase << hr;
430 return false;
431 }
432
433 if (!CheckDecoderDxvaSupport())
434 return false;
435 hr = decoder_->ProcessMessage(
436 MFT_MESSAGE_SET_D3D_MANAGER,
437 reinterpret_cast<ULONG_PTR>(device_manager_.get()));
438 if (FAILED(hr)) {
439 NOTREACHED() << "Failed to pass D3D9 device to decoder "
440 << std::hex << hr;
441 return false;
442 }
443 return SetDecoderMediaTypes();
444 }
445
446 bool DXVAVideoDecodeAccelerator::CheckDecoderDxvaSupport() {
447 base::win::ScopedComPtr<IMFAttributes> attributes;
448 HRESULT hr = decoder_->GetAttributes(attributes.Receive());
449 if (FAILED(hr)) {
450 NOTREACHED() << "Unlock: Failed to get attributes, hr = "
451 << std::hex << std::showbase << hr;
452 return false;
453 }
454 UINT32 dxva = 0;
455 hr = attributes->GetUINT32(MF_SA_D3D_AWARE, &dxva);
456 if (FAILED(hr) || !dxva) {
457 NOTREACHED() << "Failed to get DXVA attr. Error:"
458 << std::hex << std::showbase << hr
459 << " .This might not be the right decoder.";
460 return false;
461 }
462
463 hr = attributes->SetUINT32(CODECAPI_AVDecVideoAcceleration_H264, TRUE);
464 DCHECK(SUCCEEDED(hr));
465 return true;
466 }
467
468 bool DXVAVideoDecodeAccelerator::SetDecoderMediaTypes() {
469 MFT_REGISTER_TYPE_INFO* input_types = NULL;
470 MFT_REGISTER_TYPE_INFO* output_types = NULL;
471 uint32 num_inputs = 0;
472 uint32 num_outputs = 0;
473 HRESULT hr = MFTGetInfo(__uuidof(CMSH264DecoderMFT), NULL, &input_types,
474 &num_inputs, &output_types, &num_outputs, NULL);
475 if (!SetDecoderInputMediaType())
476 return false;
477 return SetDecoderOutputMediaType(MFVideoFormat_NV12);
478 }
479
480 bool DXVAVideoDecodeAccelerator::SetDecoderInputMediaType() {
481 base::win::ScopedComPtr<IMFMediaType> media_type;
482 HRESULT hr = MFCreateMediaType(media_type.Receive());
483 if (FAILED(hr)) {
484 NOTREACHED() << "Failed to create empty media type object";
485 return false;
486 }
487
488 hr = media_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
489 if (FAILED(hr)) {
490 NOTREACHED() << "SetGUID for major type failed";
491 return false;
492 }
493
494 hr = media_type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_H264);
495 if (FAILED(hr)) {
496 NOTREACHED() << "SetGUID for subtype failed";
497 return false;
498 }
499
500 hr = decoder_->SetInputType(0, media_type.get(), 0); // No flags
501 if (FAILED(hr)) {
502 NOTREACHED() << "Failed to set decoder's input type";
503 return false;
504 }
505 return true;
506 }
507
508 bool DXVAVideoDecodeAccelerator::SetDecoderOutputMediaType(
509 const GUID& subtype) {
510 DWORD i = 0;
511 base::win::ScopedComPtr<IMFMediaType> out_media_type;
512 bool found = false;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 unused
ananta 2011/12/13 23:29:15 Done.
513 while (SUCCEEDED(decoder_->GetOutputAvailableType(
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 ISTM this would be more natural as a for-loop for
ananta 2011/12/13 23:29:15 Done.
514 0, i, out_media_type.Receive()))) {
515 GUID out_subtype = {0};
516 HRESULT hr = out_media_type->GetGUID(MF_MT_SUBTYPE, &out_subtype);
517 if (FAILED(hr)) {
518 NOTREACHED() << "Failed to GetGUID() on GetOutputAvailableType() "
519 << i;
520 out_media_type.Release();
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Unnecessary? (here and elsewhere)
ananta 2011/12/13 23:29:15 We have to release the COM interface here inorder
521 continue;
522 }
523 if (out_subtype == subtype) {
524 hr = decoder_->SetOutputType(0, out_media_type, 0); // No flags
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 No checking for failures?
ananta 2011/12/13 23:29:15 Done.
525 hr = MFGetAttributeSize(out_media_type, MF_MT_FRAME_SIZE,
526 reinterpret_cast<UINT32*>(&surface_width_),
527 reinterpret_cast<UINT32*>(&surface_height_));
Ami GONE FROM CHROMIUM 2011/12/13 07:24:56 surface_{width,height}_ don't seem to be used anyw
ananta 2011/12/13 23:29:15 Done. PTAL at this function. Rewritten to abort on
528 if (FAILED(hr)) {
529 NOTREACHED() << "Failed to SetOutputType to |subtype| or obtain "
530 << "width/height " << std::hex << hr;
531 }
532 out_media_type.Release();
533 return true;
534 }
535 i++;
536 out_media_type.Release();
537 }
538 return false;
539 }
540
541 bool DXVAVideoDecodeAccelerator::SendMFTMessage(MFT_MESSAGE_TYPE msg,
542 int32 param) {
543 HRESULT hr = decoder_->ProcessMessage(msg, param);
544 return SUCCEEDED(hr);
545 }
546
547 // Gets the minimum buffer sizes for input and output samples.
548 // The MFT will not allocate buffer for input nor output, so we have
549 // to do it ourselves and make sure they're the correct size.
550 // Exception is when dxva is enabled, the decoder will allocate output.
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 What's the alternative? Can dxva be "disabled" an
ananta 2011/12/13 23:29:15 Unfortunate cut paste. Rephrased the comment.
551 bool DXVAVideoDecodeAccelerator::GetStreamsInfoAndBufferReqs() {
552 HRESULT hr = decoder_->GetInputStreamInfo(0, &input_stream_info_);
553 if (FAILED(hr)) {
554 LOG(ERROR) << "Failed to get input stream info";
555 return false;
556 }
557 VLOG(1) << "Input stream info: ";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Between the VLOGs and the error-handling it's real
ananta 2011/12/13 23:29:15 Fixed this function to get the input and output st
558 VLOG(1) << "Max latency: " << input_stream_info_.hnsMaxLatency;
559 // There should be three flags, one for requiring a whole frame be in a
560 // single sample, one for requiring there be one buffer only in a single
561 // sample, and one that specifies a fixed sample size. (as in cbSize)
562 VLOG(1) << "Flags: "
563 << std::hex << std::showbase << input_stream_info_.dwFlags;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Unnecessary since the CHECK_EQ in the next line wi
564 CHECK_EQ(input_stream_info_.dwFlags, 0x7u);
565 VLOG(1) << "Min buffer size: " << input_stream_info_.cbSize;
566 VLOG(1) << "Max lookahead: " << input_stream_info_.cbMaxLookahead;
567 VLOG(1) << "Alignment: " << input_stream_info_.cbAlignment;
568
569 hr = decoder_->GetOutputStreamInfo(0, &output_stream_info_);
570 if (FAILED(hr)) {
571 LOG(ERROR) << "Failed to get output stream info";
572 return false;
573 }
574 VLOG(1) << "Output stream info: ";
575 // The flags here should be the same and mean the same thing, except when
576 // DXVA is enabled, there is an extra 0x100 flag meaning decoder will
577 // allocate its own sample.
578 VLOG(1) << "Flags: "
579 << std::hex << std::showbase << output_stream_info_.dwFlags;
580 CHECK_EQ(output_stream_info_.dwFlags, 0x107u);
581 VLOG(1) << "Min buffer size: " << output_stream_info_.cbSize;
582 VLOG(1) << "Alignment: " << output_stream_info_.cbAlignment;
583 return true;
584 }
585
586 bool DXVAVideoDecodeAccelerator::DoDecode() {
587 if (state_ != kNormal && state_ != kEosDrain) {
588 NOTREACHED() << "DoDecode: not in normal or drain state";
589 return false;
590 }
591 base::win::ScopedComPtr<IMFSample> output_sample;
592 MFT_OUTPUT_DATA_BUFFER output_data_buffer;
593 DWORD status = 0;
594
595 memset(&output_data_buffer, 0, sizeof(output_data_buffer));
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This is incorrect in general; prefer the = {0}; st
ananta 2011/12/13 23:29:15 Done.
596 output_data_buffer.pSample = output_sample;
597 status = 0;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 redundant.
ananta 2011/12/13 23:29:15 Done.
598
599 HRESULT hr = decoder_->ProcessOutput(0, // No flags
600 1, // # of out streams to pull from
601 &output_data_buffer,
602 &status);
603 IMFCollection* events = output_data_buffer.pEvents;
604 if (events != NULL) {
605 VLOG(1) << "Got events from ProcessOuput, but discarding";
606 events->Release();
607 }
608 if (FAILED(hr)) {
609 if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
610 hr = SetDecoderOutputMediaType(MFVideoFormat_NV12);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 I don't understand this - the stream changed nothi
ananta 2011/12/13 23:29:15 This is how MSDN mentions that we should handle th
611 if (SUCCEEDED(hr)) {
612 return true;
613 } else {
614 NOTREACHED() << "Failed to set decoder output media type";
615 return false;
616 }
617 } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
618 // No more output from the decoder. Notify EOS and stop playback.
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 DCHECK that we were waiting for this?
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This doesn't actually Notify or stop, it just sign
ananta 2011/12/13 23:29:15 Could you clarify this?.
619 state_ = DXVAVideoDecodeAccelerator::kStopped;
620 return false;
621 } else {
622 NOTREACHED() << "Unhandled error in DoDecode()";
623 state_ = DXVAVideoDecodeAccelerator::kStopped;
624 return false;
625 }
626 }
627
628 #if !defined(NDEBUG)
629 VLOG(1) << "Number of input packets before successful decode: "
630 << inputs_before_decode_;
631 inputs_before_decode_ = 0;
632 uint32 end_decode = GetTickCount();
633 VLOG(1) << "Total time for decode: "
634 << end_decode - decode_start_time_;
635 decode_start_time_ = 0;
636 #endif // !defined(NDEBUG)
637 if (!ProcessOutputSample(output_data_buffer.pSample)) {
638 state_ = DXVAVideoDecodeAccelerator::kStopped;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 More like error than stopped. NotifyError?
ananta 2011/12/13 23:29:15 Done.
639 return false;
640 }
641
642 input_buffer_frame_times_.clear();
643 state_ = DXVAVideoDecodeAccelerator::kNormal;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Why overwrite the state if we were stopping?
ananta 2011/12/13 23:29:15 When we arrive here we are not stopping as we have
644 return true;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Return value is ignored at all call-sites.
ananta 2011/12/13 23:29:15 Done.
645 }
646
647 bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) {
648 if (!sample) {
649 NOTREACHED() << "ProcessOutput succeeded, but did not get a sample back";
650 return false;
651 }
652 base::win::ScopedComPtr<IMFSample> output_sample;
653 output_sample.Attach(sample);
654
655 if (!input_buffer_frame_times_.size()) {
656 VLOG(1) << "In input buffers left to process output";
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 what?
ananta 2011/12/13 23:29:15 Replaced with No. :(
657 return false;
658 }
659 int32 input_buffer_id =
660 input_buffer_frame_times_[input_buffer_frame_times_.size() - 1];
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 I extra don't understand input_buffer_frame_times_
661 input_buffer_frame_times_.clear();
662
663 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer;
664 HRESULT hr = sample->GetBufferByIndex(0, output_buffer.Receive());
665 if (FAILED(hr)) {
666 NOTREACHED() << "Failed to get buffer from sample";
667 return false;
668 }
669 base::win::ScopedComPtr<IDirect3DSurface9> surface;
670 hr = MFGetService(output_buffer, MR_BUFFER_SERVICE,
671 IID_PPV_ARGS(surface.Receive()));
672 if (FAILED(hr)) {
673 NOTREACHED() << "Failed to get surface from buffer";
674 return false;
675 }
676
677 D3DSURFACE_DESC surface_desc;
678 hr = surface->GetDesc(&surface_desc);
679 if (FAILED(hr)) {
680 NOTREACHED() << "Failed to get surface description";
681 return false;
682 }
683
684 #if !defined(NDEBUG)
685 DWORD start_surface_time = GetTickCount();
686 #endif // !defined(NDEBUG)
687
688 // TODO(ananta)
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 TODOs are usually marked w/ an email address. s/a
Ami GONE FROM CHROMIUM 2011/12/13 20:24:38 oops, I see that's your @chromium address. Disreg
689 // The code below may not be necessary once we have an ANGLE extension which
690 // allows us to pass the Direct 3D surface directly for rendering.
691
692 // The decoded bits in the source direct 3d surface are in the YUV
693 // format. Angle does not support that. As a workaround we create an
694 // offscreen surface in the RGB format and copy the source surface
695 // to this surface.
696 base::win::ScopedComPtr<IDirect3DSurface9> dest_surface;
697 hr = device_->CreateOffscreenPlainSurface(surface_desc.Width,
698 surface_desc.Height,
699 D3DFMT_A8R8G8B8,
700 D3DPOOL_DEFAULT,
701 dest_surface.Receive(),
702 NULL);
703 if (FAILED(hr)) {
704 NOTREACHED() << "Failed to create offscreen surface";
705 return false;
706 }
707
708 hr = D3DXLoadSurfaceFromSurface(dest_surface, NULL, NULL, surface, NULL,
709 NULL, 0, 0);
710 if (FAILED(hr)) {
711 NOTREACHED() << "Failed to copy source surface to dest.";
712 return false;
713 }
714
715 #if !defined(NDEBUG)
716 DWORD end_surface_time = GetTickCount();
717 VLOG(1) << "Time to create and copy new surface is "
718 << end_surface_time - start_surface_time;
719 #endif // !defined(NDEBUG)
720
721 PendingSampleInfo sample_info;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Give PSI a ctor and use it here? In fact can repla
ananta 2011/12/13 23:29:15 Done.
722
723 sample_info.input_buffer_id = input_buffer_id;
724 sample_info.surface = dest_surface;
725 pending_output_samples_.push_back(sample_info);
726
727 // If we have available picture buffers to copy the output data then use the
728 // first one and then flag it as not being available for use.
729 if (available_pictures_.size()) {
730 ProcessPendingSamples();
731 return true;
732 }
733
734 if (pictures_requested_) {
735 VLOG(1) << "Waiting for picture slots from the client.";
736 return true;
737 }
738 // Go ahead and request picture buffers.
739 client_->ProvidePictureBuffers(
740 kNumPictureBuffers, gfx::Size(surface_desc.Width, surface_desc.Height));
741 pictures_requested_ = true;
cpu_(ooo_6.6-7.5) 2011/12/13 19:28:57 so pictures_requested_ does not become false again
ananta 2011/12/13 23:29:15 We only do this upfront. After that we just rotate
742 return true;
743 }
744
745 bool DXVAVideoDecodeAccelerator::CopyOutputSampleDataToPictureBuffer(
746 IDirect3DSurface9* dest_surface, media::PictureBuffer picture_buffer,
747 int32 input_buffer_id) {
748 DCHECK(dest_surface);
749
750 // Get the currently loaded bitmap from the DC.
751 HDC hdc = NULL;
752
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 drop \n
ananta 2011/12/13 23:29:15 Done.
753 HRESULT hr = dest_surface->GetDC(&hdc);
754 if (FAILED(hr)) {
755 NOTREACHED() << "Failed to get HDC for dest offscreen surface";
756 return false;
757 }
758
759 HBITMAP bitmap =
760 reinterpret_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
761 if (!bitmap) {
762 NOTREACHED() << "Failed to get bitmap from DC";
763 return false;
764 }
765 // TODO(ananta)
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 ditto
ananta 2011/12/13 23:29:15 Done.
766 // The code below may not be necessary once we have an ANGLE extension which
767 // allows us to pass the Direct 3D surface directly for rendering.
768
769 // The Device dependent bitmap is upside down for OpenGL. We convert the
770 // bitmap to a DIB and render it on the texture instead.
771 BITMAP bitmap_basic_info = {0};
772 GetObject(bitmap, sizeof(BITMAP), &bitmap_basic_info);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 failure handling
ananta 2011/12/13 23:29:15 Done.
773
774 BITMAPINFO bitmap_info = {0};
775 bitmap_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
776 bitmap_info.bmiHeader.biWidth = bitmap_basic_info.bmWidth;
777 bitmap_info.bmiHeader.biHeight = bitmap_basic_info.bmHeight;
778 bitmap_info.bmiHeader.biPlanes = 1;
779 bitmap_info.bmiHeader.biBitCount = bitmap_basic_info.bmBitsPixel;
780 bitmap_info.bmiHeader.biCompression = BI_RGB;
781 bitmap_info.bmiHeader.biSizeImage = 0;
782 bitmap_info.bmiHeader.biClrUsed = 0;
783
784 int ret = GetDIBits(hdc, bitmap, 0, 0, NULL, &bitmap_info, DIB_RGB_COLORS);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 failure handling
ananta 2011/12/13 23:29:15 Done.
785 if (bitmap_info.bmiHeader.biSizeImage <= 0) {
786 NOTREACHED() << "Failed to read bitmap size";
787 return false;
788 }
789 scoped_ptr<char> bits(new char[bitmap_info.bmiHeader.biSizeImage]);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 scoped_array
ananta 2011/12/13 23:29:15 Done.
790 ret = GetDIBits(hdc, bitmap, 0, bitmap_basic_info.bmHeight, bits.get(),
791 &bitmap_info, DIB_RGB_COLORS);
792 DCHECK_GT(ret, 0);
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 You seem to have given up on error handling from h
ananta 2011/12/13 23:29:15 Done.
793
794 D3DSURFACE_DESC surface_desc;
795 hr = dest_surface->GetDesc(&surface_desc);
796 DCHECK(SUCCEEDED(hr));
797
798 glBindTexture(GL_TEXTURE_2D, picture_buffer.texture_id());
799 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, surface_desc.Width,
800 surface_desc.Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
801 reinterpret_cast<GLvoid*>(bits.get()));
802 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
803 glBindTexture(GL_TEXTURE_2D, 0);
804 dest_surface->ReleaseDC(hdc);
805
806 media::Picture output_picture(picture_buffer.id(), input_buffer_id);
807 client_->PictureReady(output_picture);
808 return true;
809 }
810
811 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() {
812 PendingOutputSamples::iterator sample_index =
813 pending_output_samples_.begin();
814 HRESULT hr = E_FAIL;
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 Unused.
ananta 2011/12/13 23:29:15 Done.
815
816 while (sample_index != pending_output_samples_.end()) {
Ami GONE FROM CHROMIUM 2011/12/13 16:44:06 This while smells like a for, but more than that t
ananta 2011/12/13 23:29:15 Done.
817 const PendingSampleInfo& sample_info = *sample_index;
818 OutputBuffers::iterator index;
819 for (index = available_pictures_.begin();
820 index != available_pictures_.end();
821 ++index) {
822 if (index->second.available) {
823 CopyOutputSampleDataToPictureBuffer(sample_info.surface,
824 index->second.picture_buffer,
825 sample_info.input_buffer_id);
826 index->second.available = false;
827 sample_index = pending_output_samples_.erase(sample_index);
828 break;
829 }
830 }
831 if (index == available_pictures_.end()) {
832 VLOG(1) << "No available picture slots for output";
833 break;
834 }
835 }
836 }
837
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698