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

Side by Side Diff: media/gpu/media_foundation_video_encode_accelerator_win.cc

Issue 2058413003: H264 HW encode using MediaFoundation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: grt@ comments. Created 4 years, 5 months 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
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "media/gpu/media_foundation_video_encode_accelerator_win.h"
6
7 #if defined(OS_WIN)
ananta 2016/07/18 23:26:07 Remove if defined() This file should only be compi
emircan 2016/07/19 03:09:20 Done.
8 #pragma warning(push)
9 #pragma warning(disable : 4800) // Disable warning for added padding.
10 #endif // !defined(OS_WIN)
11
12 #include <codecapi.h>
13 #include <mferror.h>
14 #include <mftransform.h>
15
16 #include <utility>
17 #include <vector>
18
19 #include "base/threading/sequenced_task_runner_handle.h"
20 #include "base/win/scoped_co_mem.h"
21 #include "base/win/scoped_variant.h"
22 #include "base/win/windows_version.h"
23 #include "media/base/win/mf_helpers.h"
24 #include "media/base/win/mf_initializer.h"
25 #include "third_party/libyuv/include/libyuv.h"
26
27 using base::win::ScopedComPtr;
28 using media::mf::MediaBufferScopedPointer;
29
30 namespace media {
31
32 namespace {
33
34 const size_t kMaxFrameRateNumerator = 30;
35 const size_t kMaxFrameRateDenominator = 1;
36 const size_t kMaxResolutionWidth = 4096;
37 const size_t kMaxResolutionHeight = 2160;
38 const size_t kNumInputBuffers = 3;
39 const size_t kOneSecondInMicroseconds = 1000000;
40 const size_t kOutputSampleBufferSizeRatio = 4;
41
42 constexpr const wchar_t* const kMediaFoundationVideoEncoderDLLs[] = {
43 L"mf.dll", L"mfplat.dll",
44 };
45
46 } // namespace
47
48 class MediaFoundationVideoEncodeAccelerator::EncodeOutput {
49 public:
50 EncodeOutput(uint32_t size, bool key_frame, base::TimeDelta timestamp)
51 : keyframe(key_frame), capture_timestamp(timestamp), data_(size) {}
52
53 uint8_t* memory() { return data_.data(); }
54 int size() const { return static_cast<int>(data_.size()); }
ananta 2016/07/18 23:26:08 newline between methods.
emircan 2016/07/19 03:09:20 Done.
55 const bool keyframe;
56 const base::TimeDelta capture_timestamp;
57
58 private:
59 std::vector<uint8_t> data_;
60 DISALLOW_COPY_AND_ASSIGN(EncodeOutput);
ananta 2016/07/18 23:26:08 newline here.
emircan 2016/07/19 03:09:20 Done.
61 };
62
63 struct MediaFoundationVideoEncodeAccelerator::BitstreamBufferRef {
64 BitstreamBufferRef(int32_t id,
65 std::unique_ptr<base::SharedMemory> shm,
66 size_t size)
67 : id(id), shm(std::move(shm)), size(size) {}
68 const int32_t id;
69 const std::unique_ptr<base::SharedMemory> shm;
70 const size_t size;
71
72 private:
73 DISALLOW_IMPLICIT_CONSTRUCTORS(BitstreamBufferRef);
74 };
75
76 MediaFoundationVideoEncodeAccelerator::MediaFoundationVideoEncodeAccelerator()
77 : client_task_runner_(base::SequencedTaskRunnerHandle::Get()),
78 encoder_thread_("MFEncoderThread"),
79 encoder_task_weak_factory_(this) {}
80
81 MediaFoundationVideoEncodeAccelerator::
82 ~MediaFoundationVideoEncodeAccelerator() {
83 DVLOG(3) << __FUNCTION__;
84 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
85
86 DCHECK(!encoder_thread_.IsRunning());
87 DCHECK(!encoder_task_weak_factory_.HasWeakPtrs());
88 }
89
90 VideoEncodeAccelerator::SupportedProfiles
91 MediaFoundationVideoEncodeAccelerator::GetSupportedProfiles() {
92 DVLOG(3) << __FUNCTION__;
93 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
94
95 SupportedProfiles profiles;
96 if (base::win::GetVersion() < base::win::VERSION_WIN8) {
97 DLOG(ERROR) << "Windows versions earlier than 8 are not supported.";
98 return profiles;
99 }
100
101 SupportedProfile profile;
102 // More profiles can be supported here, but they should be available in SW
103 // fallback as well.
104 profile.profile = H264PROFILE_BASELINE;
105 profile.max_framerate_numerator = kMaxFrameRateNumerator;
106 profile.max_framerate_denominator = kMaxFrameRateDenominator;
107 profile.max_resolution = gfx::Size(kMaxResolutionWidth, kMaxResolutionHeight);
108 profiles.push_back(profile);
109 return profiles;
110 }
111
112 bool MediaFoundationVideoEncodeAccelerator::Initialize(
113 VideoPixelFormat format,
114 const gfx::Size& input_visible_size,
115 VideoCodecProfile output_profile,
116 uint32_t initial_bitrate,
117 Client* client) {
118 DVLOG(3) << __FUNCTION__
119 << ": input_format=" << VideoPixelFormatToString(format)
120 << ", input_visible_size=" << input_visible_size.ToString()
121 << ", output_profile=" << output_profile
122 << ", initial_bitrate=" << initial_bitrate;
123 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
124
125 if (PIXEL_FORMAT_I420 != format) {
126 DLOG(ERROR) << "Input format not supported= "
127 << VideoPixelFormatToString(format);
128 return false;
129 }
130
131 if (H264PROFILE_BASELINE != output_profile) {
132 DLOG(ERROR) << "Output profile not supported= " << output_profile;
133 return false;
134 }
135
136 for (const wchar_t* mfdll : kMediaFoundationVideoEncoderDLLs) {
137 HMODULE dll = ::GetModuleHandle(mfdll);
138 if (!dll) {
ananta 2016/07/18 23:26:08 nuke dll and just use if (!::GetModuleHandle(mfdll
emircan 2016/07/19 03:09:20 Done.
139 DLOG(ERROR) << mfdll << " is required for encoding";
140 return false;
141 }
142 }
143
144 encoder_thread_.init_com_with_mta(false);
145 if (!encoder_thread_.Start()) {
146 DLOG(ERROR) << "Failed spawning encoder thread.";
147 return false;
148 }
149 encoder_thread_task_runner_ = encoder_thread_.task_runner();
150
151 InitializeMediaFoundation();
152
153 uint32_t flags = MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_SORTANDFILTER;
154 MFT_REGISTER_TYPE_INFO input_info;
155 input_info.guidMajorType = MFMediaType_Video;
156 input_info.guidSubtype = MFVideoFormat_NV12;
157 MFT_REGISTER_TYPE_INFO output_info;
158 output_info.guidMajorType = MFMediaType_Video;
159 output_info.guidSubtype = MFVideoFormat_H264;
160
161 base::win::ScopedCoMem<CLSID> CLSIDs;
162 uint32_t count = 0;
163 HRESULT hr = MFTEnum(MFT_CATEGORY_VIDEO_ENCODER, flags, NULL, &output_info,
164 NULL, &CLSIDs, &count);
165 RETURN_ON_HR_FAILURE(hr, "Couldn't enumerate hardware encoder", false);
166 RETURN_ON_FAILURE((count > 0), "No HW encoder found", false);
167 DVLOG(3) << "HW encoder(s) found: " << count;
168 hr = encoder_.CreateInstance(CLSIDs[0]);
169 RETURN_ON_HR_FAILURE(hr, "Couldn't activate hardware encoder", false);
170
171 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
172 client_ = client_ptr_factory_->GetWeakPtr();
173 input_visible_size_ = input_visible_size;
174 frame_rate_ = kMaxFrameRateNumerator / kMaxFrameRateDenominator;
175 target_bitrate_ = initial_bitrate;
176 bitstream_buffer_size_ = input_visible_size.GetArea();
177
178 u_plane_offset_ =
179 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, VideoFrame::kYPlane,
180 input_visible_size_)
181 .GetArea();
182 v_plane_offset_ =
183 u_plane_offset_ +
184 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, VideoFrame::kUPlane,
185 input_visible_size_)
186 .GetArea();
187
188 if (!InitializeInputOutputSamples()) {
189 DLOG(ERROR) << "Failed initializing input-output samples.";
190 return false;
191 }
192
193 if (!SetEncoderModes()) {
194 DLOG(ERROR) << "Failed setting encoder parameters.";
195 return false;
196 }
197
198 hr = encoder_->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, NULL);
199 RETURN_ON_HR_FAILURE(hr, "Couldn't set ProcessMessage", false);
200
201 client_task_runner_->PostTask(
202 FROM_HERE,
203 base::Bind(&Client::RequireBitstreamBuffers, client_, kNumInputBuffers,
204 input_visible_size_, bitstream_buffer_size_));
205 return SUCCEEDED(hr);
206 }
207
208 void MediaFoundationVideoEncodeAccelerator::Encode(
209 const scoped_refptr<VideoFrame>& frame,
210 bool force_keyframe) {
211 DVLOG(3) << __FUNCTION__;
212 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
213
214 encoder_thread_task_runner_->PostTask(
215 FROM_HERE, base::Bind(&MediaFoundationVideoEncodeAccelerator::EncodeTask,
216 encoder_task_weak_factory_.GetWeakPtr(), frame,
217 force_keyframe));
218 }
219
220 void MediaFoundationVideoEncodeAccelerator::UseOutputBitstreamBuffer(
221 const BitstreamBuffer& buffer) {
222 DVLOG(3) << __FUNCTION__ << ": buffer size=" << buffer.size();
223 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
224
225 if (buffer.size() < bitstream_buffer_size_) {
226 DLOG(ERROR) << "Output BitstreamBuffer isn't big enough: " << buffer.size()
227 << " vs. " << bitstream_buffer_size_;
228 client_->NotifyError(kInvalidArgumentError);
229 return;
230 }
231
232 std::unique_ptr<base::SharedMemory> shm(
233 new base::SharedMemory(buffer.handle(), false));
234 if (!shm->Map(buffer.size())) {
235 DLOG(ERROR) << "Failed mapping shared memory.";
236 client_->NotifyError(kPlatformFailureError);
237 return;
238 }
239
240 std::unique_ptr<BitstreamBufferRef> buffer_ref(
241 new BitstreamBufferRef(buffer.id(), std::move(shm), buffer.size()));
242 encoder_thread_task_runner_->PostTask(
243 FROM_HERE,
244 base::Bind(
245 &MediaFoundationVideoEncodeAccelerator::UseOutputBitstreamBufferTask,
246 encoder_task_weak_factory_.GetWeakPtr(), base::Passed(&buffer_ref)));
247 }
248
249 void MediaFoundationVideoEncodeAccelerator::RequestEncodingParametersChange(
250 uint32_t bitrate,
251 uint32_t framerate) {
252 DVLOG(3) << __FUNCTION__ << ": bitrate=" << bitrate
253 << ": framerate=" << framerate;
254 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
255
256 encoder_thread_task_runner_->PostTask(
257 FROM_HERE,
258 base::Bind(&MediaFoundationVideoEncodeAccelerator::
259 RequestEncodingParametersChangeTask,
260 encoder_task_weak_factory_.GetWeakPtr(), bitrate, framerate));
261 }
262
263 void MediaFoundationVideoEncodeAccelerator::Destroy() {
264 DVLOG(3) << __FUNCTION__;
265 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
266
267 // Cancel all callbacks.
268 client_ptr_factory_.reset();
269
270 if (encoder_thread_.IsRunning()) {
271 encoder_thread_task_runner_->PostTask(
272 FROM_HERE,
273 base::Bind(&MediaFoundationVideoEncodeAccelerator::DestroyTask,
274 encoder_task_weak_factory_.GetWeakPtr()));
275 encoder_thread_.Stop();
276 }
277
278 delete this;
279 }
280
281 // static
282 void MediaFoundationVideoEncodeAccelerator::PreSandboxInitialization() {
283 for (const wchar_t* mfdll : kMediaFoundationVideoEncoderDLLs)
284 ::LoadLibrary(mfdll);
285 }
286
287 bool MediaFoundationVideoEncodeAccelerator::InitializeInputOutputSamples() {
288 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
289
290 HRESULT hr = encoder_->GetStreamLimits(
291 &input_stream_count_min_, &input_stream_count_max_,
292 &output_stream_count_min_, &output_stream_count_max_);
293 RETURN_ON_HR_FAILURE(hr, "Couldn't query stream limits", false);
294 DVLOG(3) << "Stream limits: " << input_stream_count_min_ << ","
295 << input_stream_count_max_ << "," << output_stream_count_min_ << ","
296 << output_stream_count_max_;
297
298 // Initialize output parameters.
299 base::win::ScopedComPtr<IMFMediaType> imf_output_media_type;
300 hr = MFCreateMediaType(imf_output_media_type.Receive());
301 RETURN_ON_HR_FAILURE(hr, "Couldn't create media type", false);
302 hr = imf_output_media_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
303 RETURN_ON_HR_FAILURE(hr, "Couldn't set media type", false);
304 hr = imf_output_media_type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_H264);
305 RETURN_ON_HR_FAILURE(hr, "Couldn't set video format", false);
306 hr = imf_output_media_type->SetUINT32(MF_MT_AVG_BITRATE, target_bitrate_);
307 RETURN_ON_HR_FAILURE(hr, "Couldn't set bitrate", false);
308 hr = MFSetAttributeRatio(imf_output_media_type.get(), MF_MT_FRAME_RATE,
309 frame_rate_, kMaxFrameRateDenominator);
310 RETURN_ON_HR_FAILURE(hr, "Couldn't set frame rate", false);
311 hr = MFSetAttributeSize(imf_output_media_type.get(), MF_MT_FRAME_SIZE,
312 input_visible_size_.width(),
313 input_visible_size_.height());
314 RETURN_ON_HR_FAILURE(hr, "Couldn't set frame size", false);
315 hr = imf_output_media_type->SetUINT32(MF_MT_INTERLACE_MODE,
316 MFVideoInterlace_Progressive);
317 RETURN_ON_HR_FAILURE(hr, "Couldn't set interlace mode", false);
318 hr = imf_output_media_type->SetUINT32(MF_MT_MPEG2_PROFILE,
319 eAVEncH264VProfile_Base);
320 RETURN_ON_HR_FAILURE(hr, "Couldn't set codec profile", false);
321 hr = encoder_->SetOutputType(0, imf_output_media_type.get(), 0);
322 RETURN_ON_HR_FAILURE(hr, "Couldn't set output media type", false);
323
324 // Initialize input parameters.
325 base::win::ScopedComPtr<IMFMediaType> imf_input_media_type;
326 hr = MFCreateMediaType(imf_input_media_type.Receive());
327 RETURN_ON_HR_FAILURE(hr, "Couldn't create media type", false);
328 hr = imf_input_media_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
329 RETURN_ON_HR_FAILURE(hr, "Couldn't set media type", false);
330 hr = imf_input_media_type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_YV12);
331 RETURN_ON_HR_FAILURE(hr, "Couldn't set video format", false);
332 hr = MFSetAttributeRatio(imf_input_media_type.get(), MF_MT_FRAME_RATE,
333 frame_rate_, kMaxFrameRateDenominator);
334 RETURN_ON_HR_FAILURE(hr, "Couldn't set frame rate", false);
335 hr = MFSetAttributeSize(imf_input_media_type.get(), MF_MT_FRAME_SIZE,
336 input_visible_size_.width(),
337 input_visible_size_.height());
338 RETURN_ON_HR_FAILURE(hr, "Couldn't set frame size", false);
339 hr = imf_input_media_type->SetUINT32(MF_MT_INTERLACE_MODE,
340 MFVideoInterlace_Progressive);
341 RETURN_ON_HR_FAILURE(hr, "Couldn't set interlace mode", false);
342 hr = encoder_->SetInputType(0, imf_input_media_type.get(), 0);
343 RETURN_ON_HR_FAILURE(hr, "Couldn't set input media type", false);
344
345 input_sample_.Attach(mf::CreateEmptySampleWithBuffer(
346 VideoFrame::AllocationSize(PIXEL_FORMAT_I420, input_visible_size_), 2));
347 output_sample_.Attach(mf::CreateEmptySampleWithBuffer(
348 bitstream_buffer_size_ * kOutputSampleBufferSizeRatio, 2));
349
350 return SUCCEEDED(hr);
351 }
352
353 bool MediaFoundationVideoEncodeAccelerator::SetEncoderModes() {
354 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
355
356 HRESULT hr = encoder_.QueryInterface(IID_ICodecAPI, codec_api_.ReceiveVoid());
357 RETURN_ON_HR_FAILURE(hr, "Couldn't get ICodecAPI", false);
358 VARIANT var;
359 var.vt = VT_UI4;
360 var.ulVal = eAVEncCommonRateControlMode_CBR;
361 hr = codec_api_->SetValue(&CODECAPI_AVEncCommonRateControlMode, &var);
362 RETURN_ON_HR_FAILURE(hr, "Couldn't set CommonRateControlMode", false);
363 var.ulVal = target_bitrate_;
364 hr = codec_api_->SetValue(&CODECAPI_AVEncCommonMeanBitRate, &var);
365 RETURN_ON_HR_FAILURE(hr, "Couldn't set bitrate", false);
366 var.ulVal = eAVEncAdaptiveMode_FrameRate;
367 hr = codec_api_->SetValue(&CODECAPI_AVEncAdaptiveMode, &var);
368 RETURN_ON_HR_FAILURE(hr, "Couldn't set FrameRate", false);
369 var.vt = VT_BOOL;
370 var.boolVal = VARIANT_TRUE;
371 hr = codec_api_->SetValue(&CODECAPI_AVLowLatencyMode, &var);
372 RETURN_ON_HR_FAILURE(hr, "Couldn't set LowLatencyMode", false);
373 return SUCCEEDED(hr);
374 }
375
376 void MediaFoundationVideoEncodeAccelerator::EncodeTask(
377 const scoped_refptr<VideoFrame>& frame,
378 bool force_keyframe) {
379 DVLOG(3) << __FUNCTION__;
380 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
381
382 if (!encoder_)
ananta 2016/07/18 23:26:08 Should this just be a DCHECK?. From the code it se
emircan 2016/07/19 03:09:20 This is for the case when EncodeTask() is schedule
383 return;
384
385 base::win::ScopedComPtr<IMFMediaBuffer> input_buffer;
386 input_sample_->GetBufferByIndex(0, input_buffer.Receive());
387
388 {
389 MediaBufferScopedPointer scoped_buffer(input_buffer.get());
390 DCHECK(scoped_buffer.get());
391 libyuv::I420Copy(frame->visible_data(VideoFrame::kYPlane),
392 frame->stride(VideoFrame::kYPlane),
393 frame->visible_data(VideoFrame::kVPlane),
394 frame->stride(VideoFrame::kVPlane),
395 frame->visible_data(VideoFrame::kUPlane),
396 frame->stride(VideoFrame::kUPlane), scoped_buffer.get(),
397 frame->stride(VideoFrame::kYPlane),
398 scoped_buffer.get() + u_plane_offset_,
399 frame->stride(VideoFrame::kUPlane),
400 scoped_buffer.get() + v_plane_offset_,
401 frame->stride(VideoFrame::kVPlane),
402 input_visible_size_.width(), input_visible_size_.height());
403 }
404
405 input_sample_->SetSampleTime(frame->timestamp().InMicroseconds() * 10);
406 input_sample_->SetSampleDuration(kOneSecondInMicroseconds / frame_rate_);
407 HRESULT hr = encoder_->ProcessInput(0, input_sample_.get(), 0);
408 // According to MSDN, if encoder returns MF_E_NOTACCEPTING, we need to try
409 // processing the output. This error indicates that encoder does not accept
410 // any more input data.
411 if (hr == MF_E_NOTACCEPTING) {
412 DVLOG(3) << "MF_E_NOTACCEPTING";
413 ProcessOutput();
414 hr = encoder_->ProcessInput(0, input_sample_.get(), 0);
415 if (hr == MF_E_NOTACCEPTING) {
416 encoder_thread_task_runner_->PostTask(
417 FROM_HERE,
418 base::Bind(&MediaFoundationVideoEncodeAccelerator::EncodeTask,
ananta 2016/07/18 23:26:08 Do we need a PostTask here?. Just calling ProcessO
emircan 2016/07/19 03:09:20 We already do call ProcessOutput() and retry encod
ananta 2016/07/20 01:57:22 That code in dxva appears to be wrong. If we get M
emircan 2016/07/20 19:01:19 Done. I will notify of an error if we fail twice a
419 encoder_task_weak_factory_.GetWeakPtr(), frame,
420 force_keyframe));
421 } else {
422 RETURN_ON_HR_FAILURE(hr, "Couldn't encode", );
423 }
424 } else {
425 RETURN_ON_HR_FAILURE(hr, "Couldn't encode", );
426 }
427 DVLOG(3) << "Sent for encode " << hr;
428
429 ProcessOutput();
430 }
431
432 void MediaFoundationVideoEncodeAccelerator::ProcessOutput() {
433 DVLOG(3) << __FUNCTION__;
434 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
435
436 MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0};
437 output_data_buffer.dwStreamID = 0;
438 output_data_buffer.dwStatus = 0;
439 output_data_buffer.pEvents = NULL;
440 output_data_buffer.pSample = output_sample_.get();
441 DWORD status = 0;
442 HRESULT hr = encoder_->ProcessOutput(0, 1, &output_data_buffer, &status);
443 if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
444 DVLOG(3) << "MF_E_TRANSFORM_NEED_MORE_INPUT";
445 return;
446 }
447 RETURN_ON_HR_FAILURE(hr, "Couldn't get encoded data", );
448 DVLOG(3) << "Got encoded data " << hr;
449
450 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer;
451 hr = output_sample_->GetBufferByIndex(0, output_buffer.Receive());
452 RETURN_ON_HR_FAILURE(hr, "Couldn't get buffer by index", );
453 DWORD size = 0;
454 hr = output_buffer->GetCurrentLength(&size);
455 RETURN_ON_HR_FAILURE(hr, "Couldn't get buffer length", );
456
457 const bool keyframe = MFGetAttributeUINT32(
458 output_sample_.get(), MFSampleExtension_CleanPoint, false);
459 DVLOG(3) << "We HAVE encoded data with size:" << size << " keyframe "
460 << keyframe;
461
462 if (bitstream_buffer_queue_.empty()) {
463 DVLOG(3) << "No bitstream buffers.";
464 // We need to copy the output so that encoding can continue.
465 std::unique_ptr<EncodeOutput> encode_output(
466 new EncodeOutput(size, keyframe, base::Time::Now() - base::Time()));
467 {
468 MediaBufferScopedPointer scoped_buffer(output_buffer.get());
469 memcpy(encode_output->memory(), scoped_buffer.get(), size);
470 }
471 encoder_output_queue_.push_back(std::move(encode_output));
472 return;
473 }
474
475 std::unique_ptr<MediaFoundationVideoEncodeAccelerator::BitstreamBufferRef>
476 buffer_ref = std::move(bitstream_buffer_queue_.front());
477 bitstream_buffer_queue_.pop_front();
478
479 {
480 MediaBufferScopedPointer scoped_buffer(output_buffer.get());
481 memcpy(buffer_ref->shm->memory(), scoped_buffer.get(), size);
482 }
483
484 client_task_runner_->PostTask(
485 FROM_HERE,
486 base::Bind(&Client::BitstreamBufferReady, client_, buffer_ref->id, size,
487 keyframe, base::Time::Now() - base::Time()));
488
489 // Keep calling ProcessOutput recursively until MF_E_TRANSFORM_NEED_MORE_INPUT
490 // is returned to flush out all the output.
491 ProcessOutput();
492 }
493
494 void MediaFoundationVideoEncodeAccelerator::UseOutputBitstreamBufferTask(
495 std::unique_ptr<BitstreamBufferRef> buffer_ref) {
496 DVLOG(3) << __FUNCTION__;
497 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
498
499 // If there is already EncodeOutput waiting, copy its output first.
500 if (!encoder_output_queue_.empty()) {
501 std::unique_ptr<MediaFoundationVideoEncodeAccelerator::EncodeOutput>
502 encode_output = std::move(encoder_output_queue_.front());
503 encoder_output_queue_.pop_front();
504 ReturnBitstreamBuffer(std::move(encode_output), std::move(buffer_ref));
505 return;
506 }
507
508 bitstream_buffer_queue_.push_back(std::move(buffer_ref));
509 }
510
511 void MediaFoundationVideoEncodeAccelerator::ReturnBitstreamBuffer(
512 std::unique_ptr<EncodeOutput> encode_output,
513 std::unique_ptr<MediaFoundationVideoEncodeAccelerator::BitstreamBufferRef>
514 buffer_ref) {
515 DVLOG(3) << __FUNCTION__;
516 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
517
518 memcpy(buffer_ref->shm->memory(), encode_output->memory(),
519 encode_output->size());
520 client_task_runner_->PostTask(
521 FROM_HERE,
522 base::Bind(&Client::BitstreamBufferReady, client_, buffer_ref->id,
523 encode_output->size(), encode_output->keyframe,
524 encode_output->capture_timestamp));
525 }
526
527 void MediaFoundationVideoEncodeAccelerator::RequestEncodingParametersChangeTask(
528 uint32_t bitrate,
529 uint32_t framerate) {
530 DVLOG(3) << __FUNCTION__;
531 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
532
533 frame_rate_ = framerate ? framerate : 1;
534 target_bitrate_ = bitrate ? bitrate : 1;
535
536 VARIANT var;
537 var.vt = VT_UI4;
538 var.ulVal = target_bitrate_;
539 HRESULT hr = codec_api_->SetValue(&CODECAPI_AVEncCommonMeanBitRate, &var);
540 RETURN_ON_HR_FAILURE(hr, "Couldn't set bitrate", );
541
542 base::win::ScopedComPtr<IMFMediaType> imf_output_media_type;
543 hr = MFCreateMediaType(imf_output_media_type.Receive());
544 RETURN_ON_HR_FAILURE(hr, "Couldn't create output media type", );
545 hr = imf_output_media_type->SetUINT32(MF_MT_AVG_BITRATE, target_bitrate_);
546 RETURN_ON_HR_FAILURE(hr, "Couldn't set bitrate", );
547 hr = MFSetAttributeRatio(imf_output_media_type.get(), MF_MT_FRAME_RATE,
548 frame_rate_, kMaxFrameRateDenominator);
549 RETURN_ON_HR_FAILURE(hr, "Couldn't set output type params", );
550 }
551
552 void MediaFoundationVideoEncodeAccelerator::DestroyTask() {
553 DVLOG(3) << __FUNCTION__;
554 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
555
556 // Cancel all encoder thread callbacks.
557 encoder_task_weak_factory_.InvalidateWeakPtrs();
558
559 encoder_.Release();
560 }
561
562 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698