Commit aa60bafc authored by emircan's avatar emircan Committed by Commit bot

Reland: H264 HW encode using MediaFoundation

Got reverted due to breaking MSVC build:
https://bugs.chromium.org/p/chromium/issues/detail?id=630335#c2

This CL moves GTEST_FAIL() lines so that they fail at the end of the
function.

Original description ----------------------------------------------

H264 HW encode using MediaFoundation

This CL adds MediaFoundationVideoEncodeAccelerator which enables H264 encode support
using MediaFoundation on Windows 8.1+. Also, it includes a refactor of common
MediaFoundation classes under mf_helpers.*.

Note that, this is the first CL and H264 codec is still behind a flag.

Design Doc(with perf measurements): http://goo.gl/UCnwyA

BUG=590060
TEST= Tested AppRTC loopback with Chrome flag "--enable-webrtc-hw-h264-encoding" and
"--enable-mf-h264-encoding" on https://apprtc.appspot.com/?debug=loopback&vsc=h264
Also, added WIN specific sections at vea_unittests.

TBR=ananta@chromium.org,avi@chromium.org,grt@chromium.org,sandersd@chromium.org,wuchengli@chromium.org
The incremented change is trivial.

Review-Url: https://codereview.chromium.org/2167333002
Cr-Commit-Position: refs/heads/master@{#407049}
parent af71dd08
......@@ -120,6 +120,7 @@ static const char* const kSwitchNames[] = {
#endif
#if defined(OS_WIN)
switches::kEnableAcceleratedVpxDecode,
switches::kEnableMFH264Encoding,
#endif
switches::kEnableHeapProfiling,
switches::kEnableLogging,
......
......@@ -59,6 +59,7 @@
#include "base/win/scoped_com_initializer.h"
#include "base/win/windows_version.h"
#include "media/gpu/dxva_video_decode_accelerator_win.h"
#include "media/gpu/media_foundation_video_encode_accelerator_win.h"
#include "sandbox/win/src/sandbox.h"
#endif
......@@ -489,6 +490,7 @@ bool WarmUpSandbox(const base::CommandLine& command_line) {
#if defined(OS_WIN)
media::DXVAVideoDecodeAccelerator::PreSandboxInitialization();
media::MediaFoundationVideoEncodeAccelerator::PreSandboxInitialization();
#endif
return true;
}
......
......@@ -55,6 +55,9 @@ const char kUseGpuMemoryBuffersForCapture[] =
// for details.
const char kEnableExclusiveAudio[] = "enable-exclusive-audio";
// Enables H264 HW encode acceleration using Media Foundation for Windows.
const char kEnableMFH264Encoding[] = "enable-mf-h264-encoding";
// Force the use of MediaFoundation for video capture. This is only supported in
// Windows 7 and above. Used, like |kForceDirectShowVideoCapture|, to
// troubleshoot problems in Windows platforms.
......
......@@ -37,6 +37,7 @@ MEDIA_EXPORT extern const char kUseGpuMemoryBuffersForCapture[];
#if defined(OS_WIN)
MEDIA_EXPORT extern const char kEnableExclusiveAudio[];
MEDIA_EXPORT extern const char kEnableMFH264Encoding[];
MEDIA_EXPORT extern const char kForceMediaFoundationVideoCapture[];
MEDIA_EXPORT extern const char kForceWaveAudio[];
MEDIA_EXPORT extern const char kTrySupportedChannelLayouts[];
......
......@@ -8,6 +8,8 @@ component("win") {
defines = [ "MF_INITIALIZER_IMPLEMENTATION" ]
set_sources_assignment_filter([])
sources = [
"mf_helpers.cc",
"mf_helpers.h",
"mf_initializer.cc",
"mf_initializer.h",
"mf_initializer_export.h",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/win/mf_helpers.h"
namespace media {
namespace mf {
void LogDXVAError(int line) {
LOG(ERROR) << "Error in dxva_video_decode_accelerator_win.cc on line "
<< line;
}
IMFSample* CreateEmptySampleWithBuffer(uint32_t buffer_length, int align) {
CHECK_GT(buffer_length, 0U);
base::win::ScopedComPtr<IMFSample> sample;
HRESULT hr = MFCreateSample(sample.Receive());
RETURN_ON_HR_FAILURE(hr, "MFCreateSample failed", NULL);
base::win::ScopedComPtr<IMFMediaBuffer> buffer;
if (align == 0) {
// Note that MFCreateMemoryBuffer is same as MFCreateAlignedMemoryBuffer
// with the align argument being 0.
hr = MFCreateMemoryBuffer(buffer_length, buffer.Receive());
} else {
hr =
MFCreateAlignedMemoryBuffer(buffer_length, align - 1, buffer.Receive());
}
RETURN_ON_HR_FAILURE(hr, "Failed to create memory buffer for sample", NULL);
hr = sample->AddBuffer(buffer.get());
RETURN_ON_HR_FAILURE(hr, "Failed to add buffer to sample", NULL);
buffer->SetCurrentLength(0);
return sample.Detach();
}
MediaBufferScopedPointer::MediaBufferScopedPointer(IMFMediaBuffer* media_buffer)
: media_buffer_(media_buffer),
buffer_(nullptr),
max_length_(0),
current_length_(0) {
HRESULT hr = media_buffer_->Lock(&buffer_, &max_length_, &current_length_);
CHECK(SUCCEEDED(hr));
}
MediaBufferScopedPointer::~MediaBufferScopedPointer() {
HRESULT hr = media_buffer_->Unlock();
CHECK(SUCCEEDED(hr));
}
} // namespace mf
} // namespace media
\ No newline at end of file
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_BASE_WIN_MF_HELPERS_H_
#define MEDIA_BASE_WIN_MF_HELPERS_H_
#include <mfapi.h>
#include <stdint.h>
#include "base/win/scoped_comptr.h"
#include "media/base/win/mf_initializer_export.h"
namespace media {
namespace mf {
#define RETURN_ON_FAILURE(result, log, ret) \
do { \
if (!(result)) { \
DLOG(ERROR) << log; \
mf::LogDXVAError(__LINE__); \
return ret; \
} \
} while (0)
#define RETURN_ON_HR_FAILURE(result, log, ret) \
RETURN_ON_FAILURE(SUCCEEDED(result), \
log << ", HRESULT: 0x" << std::hex << result, ret);
#define RETURN_AND_NOTIFY_ON_FAILURE(result, log, error_code, ret) \
do { \
if (!(result)) { \
DVLOG(1) << log; \
mf::LogDXVAError(__LINE__); \
StopOnError(error_code); \
return ret; \
} \
} while (0)
#define RETURN_AND_NOTIFY_ON_HR_FAILURE(result, log, error_code, ret) \
RETURN_AND_NOTIFY_ON_FAILURE(SUCCEEDED(result), \
log << ", HRESULT: 0x" << std::hex << result, \
error_code, ret);
MF_INITIALIZER_EXPORT void LogDXVAError(int line);
// Creates a Media Foundation sample with one buffer of length |buffer_length|
// on a |align|-byte boundary. Alignment must be a perfect power of 2 or 0.
MF_INITIALIZER_EXPORT IMFSample* CreateEmptySampleWithBuffer(
uint32_t buffer_length,
int align);
// Provides scoped access to the underlying buffer in an IMFMediaBuffer
// instance.
class MF_INITIALIZER_EXPORT MediaBufferScopedPointer {
public:
MediaBufferScopedPointer(IMFMediaBuffer* media_buffer);
~MediaBufferScopedPointer();
uint8_t* get() { return buffer_; }
DWORD current_length() const { return current_length_; }
private:
base::win::ScopedComPtr<IMFMediaBuffer> media_buffer_;
uint8_t* buffer_;
DWORD max_length_;
DWORD current_length_;
DISALLOW_COPY_AND_ASSIGN(MediaBufferScopedPointer);
};
} // namespace mf
} // namespace media
#endif // MEDIA_BASE_WIN_MF_HELPERS_H_
\ No newline at end of file
......@@ -337,6 +337,8 @@ component("gpu") {
"dxva_picture_buffer_win.h",
"dxva_video_decode_accelerator_win.cc",
"dxva_video_decode_accelerator_win.h",
"media_foundation_video_encode_accelerator_win.cc",
"media_foundation_video_encode_accelerator_win.h",
]
configs += [
# TODO(jschuh): crbug.com/167187 fix size_t to int truncations.
......@@ -344,7 +346,10 @@ component("gpu") {
"//third_party/khronos:khronos_headers",
]
public_deps += [ "//media/base/win" ]
deps += [ "//third_party/angle:includes" ]
deps += [
"//third_party/angle:includes",
"//third_party/libyuv",
]
libs += [
"d3d9.lib",
"d3d11.lib",
......@@ -434,7 +439,7 @@ if (is_win || is_android || is_chromeos) {
}
}
if (is_chromeos || is_mac) {
if (is_chromeos || is_mac || is_win) {
test("video_encode_accelerator_unittest") {
deps = [
"//base",
......
......@@ -39,6 +39,7 @@
#include "build/build_config.h"
#include "gpu/command_buffer/service/gpu_preferences.h"
#include "gpu/config/gpu_driver_bug_workarounds.h"
#include "media/base/win/mf_helpers.h"
#include "media/base/win/mf_initializer.h"
#include "media/gpu/dxva_picture_buffer_win.h"
#include "media/video/video_decode_accelerator.h"
......@@ -187,42 +188,6 @@ static const DWORD g_IntelLegacyGPUList[] = {
0x102, 0x106, 0x116, 0x126,
};
// Provides scoped access to the underlying buffer in an IMFMediaBuffer
// instance.
class MediaBufferScopedPointer {
public:
explicit MediaBufferScopedPointer(IMFMediaBuffer* media_buffer)
: media_buffer_(media_buffer),
buffer_(nullptr),
max_length_(0),
current_length_(0) {
HRESULT hr = media_buffer_->Lock(&buffer_, &max_length_, &current_length_);
CHECK(SUCCEEDED(hr));
}
~MediaBufferScopedPointer() {
HRESULT hr = media_buffer_->Unlock();
CHECK(SUCCEEDED(hr));
}
uint8_t* get() { return buffer_; }
DWORD current_length() const { return current_length_; }
private:
base::win::ScopedComPtr<IMFMediaBuffer> media_buffer_;
uint8_t* buffer_;
DWORD max_length_;
DWORD current_length_;
DISALLOW_COPY_AND_ASSIGN(MediaBufferScopedPointer);
};
void LogDXVAError(int line) {
LOG(ERROR) << "Error in dxva_video_decode_accelerator_win.cc on line "
<< line;
}
} // namespace
namespace media {
......@@ -235,34 +200,6 @@ static const VideoCodecProfile kSupportedProfiles[] = {
CreateDXGIDeviceManager
DXVAVideoDecodeAccelerator::create_dxgi_device_manager_ = NULL;
#define RETURN_ON_FAILURE(result, log, ret) \
do { \
if (!(result)) { \
DLOG(ERROR) << log; \
LogDXVAError(__LINE__); \
return ret; \
} \
} while (0)
#define RETURN_ON_HR_FAILURE(result, log, ret) \
RETURN_ON_FAILURE(SUCCEEDED(result), \
log << ", HRESULT: 0x" << std::hex << result, ret);
#define RETURN_AND_NOTIFY_ON_FAILURE(result, log, error_code, ret) \
do { \
if (!(result)) { \
DVLOG(1) << log; \
LogDXVAError(__LINE__); \
StopOnError(error_code); \
return ret; \
} \
} while (0)
#define RETURN_AND_NOTIFY_ON_HR_FAILURE(result, log, error_code, ret) \
RETURN_AND_NOTIFY_ON_FAILURE(SUCCEEDED(result), \
log << ", HRESULT: 0x" << std::hex << result, \
error_code, ret);
enum {
// Maximum number of iterations we allow before aborting the attempt to flush
// the batched queries to the driver and allow torn/corrupt frames to be
......@@ -282,41 +219,6 @@ enum {
kAcquireSyncWaitMs = 0,
};
static IMFSample* CreateEmptySample() {
base::win::ScopedComPtr<IMFSample> sample;
HRESULT hr = MFCreateSample(sample.Receive());
RETURN_ON_HR_FAILURE(hr, "MFCreateSample failed", NULL);
return sample.Detach();
}
// Creates a Media Foundation sample with one buffer of length |buffer_length|
// on a |align|-byte boundary. Alignment must be a perfect power of 2 or 0.
static IMFSample* CreateEmptySampleWithBuffer(uint32_t buffer_length,
int align) {
CHECK_GT(buffer_length, 0U);
base::win::ScopedComPtr<IMFSample> sample;
sample.Attach(CreateEmptySample());
base::win::ScopedComPtr<IMFMediaBuffer> buffer;
HRESULT hr = E_FAIL;
if (align == 0) {
// Note that MFCreateMemoryBuffer is same as MFCreateAlignedMemoryBuffer
// with the align argument being 0.
hr = MFCreateMemoryBuffer(buffer_length, buffer.Receive());
} else {
hr =
MFCreateAlignedMemoryBuffer(buffer_length, align - 1, buffer.Receive());
}
RETURN_ON_HR_FAILURE(hr, "Failed to create memory buffer for sample", NULL);
hr = sample->AddBuffer(buffer.get());
RETURN_ON_HR_FAILURE(hr, "Failed to add buffer to sample", NULL);
buffer->SetCurrentLength(0);
return sample.Detach();
}
// Creates a Media Foundation sample with one buffer containing a copy of the
// given Annex B stream data.
// If duration and sample time are not known, provide 0.
......@@ -330,7 +232,7 @@ static IMFSample* CreateInputSample(const uint8_t* stream,
CHECK_GT(size, 0U);
base::win::ScopedComPtr<IMFSample> sample;
sample.Attach(
CreateEmptySampleWithBuffer(std::max(min_size, size), alignment));
mf::CreateEmptySampleWithBuffer(std::max(min_size, size), alignment));
RETURN_ON_FAILURE(sample.get(), "Failed to create empty sample", NULL);
base::win::ScopedComPtr<IMFMediaBuffer> buffer;
......@@ -2751,7 +2653,7 @@ HRESULT DXVAVideoDecodeAccelerator::CheckConfigChanged(IMFSample* sample,
HRESULT hr = sample->GetBufferByIndex(0, buffer.Receive());
RETURN_ON_HR_FAILURE(hr, "Failed to get buffer from input sample", hr);
MediaBufferScopedPointer scoped_media_buffer(buffer.get());
mf::MediaBufferScopedPointer scoped_media_buffer(buffer.get());
if (!config_change_detector_->DetectConfig(
scoped_media_buffer.get(), scoped_media_buffer.current_length())) {
......
......@@ -35,6 +35,9 @@
#include "media/gpu/android_video_encode_accelerator.h"
#elif defined(OS_MACOSX)
#include "media/gpu/vt_video_encode_accelerator_mac.h"
#elif defined(OS_WIN)
#include "media/base/media_switches.h"
#include "media/gpu/media_foundation_video_encode_accelerator_win.h"
#endif
namespace media {
......@@ -207,6 +210,13 @@ GpuVideoEncodeAccelerator::CreateVEAFps(
#endif
#if defined(OS_MACOSX)
create_vea_fps.push_back(&GpuVideoEncodeAccelerator::CreateVTVEA);
#endif
#if defined(OS_WIN)
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableMFH264Encoding)) {
create_vea_fps.push_back(
&GpuVideoEncodeAccelerator::CreateMediaFoundationVEA);
}
#endif
return create_vea_fps;
}
......@@ -250,6 +260,15 @@ GpuVideoEncodeAccelerator::CreateVTVEA() {
}
#endif
#if defined(OS_WIN)
// static
std::unique_ptr<media::VideoEncodeAccelerator>
GpuVideoEncodeAccelerator::CreateMediaFoundationVEA() {
return base::WrapUnique<media::VideoEncodeAccelerator>(
new MediaFoundationVideoEncodeAccelerator());
}
#endif
void GpuVideoEncodeAccelerator::OnEncode(
const AcceleratedVideoEncoderMsg_Encode_Params& params) {
DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode: frame_id = "
......
......@@ -92,6 +92,10 @@ class GpuVideoEncodeAccelerator
#if defined(OS_MACOSX)
static std::unique_ptr<VideoEncodeAccelerator> CreateVTVEA();
#endif
#if defined(OS_WIN)
static std::unique_ptr<media::VideoEncodeAccelerator>
CreateMediaFoundationVEA();
#endif
// IPC handlers, proxying VideoEncodeAccelerator for the renderer
// process.
......
This diff is collapsed.
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_
#define MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_
#include <mfapi.h>
#include <mfidl.h>
#include <stdint.h>
#include <strmif.h>
#include <deque>
#include <memory>
#include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_checker.h"
#include "base/win/scoped_comptr.h"
#include "media/gpu/media_gpu_export.h"
#include "media/video/video_encode_accelerator.h"
namespace media {
// Media Foundation implementation of the VideoEncodeAccelerator interface for
// Windows.
// This class saves the task runner on which it is constructed and returns
// encoded data to the client using that same task runner. This class has
// DCHECKs to makes sure that methods are called in sequence. It starts an
// internal encoder thread on which VideoEncodeAccelerator implementation tasks
// are posted.
class MEDIA_GPU_EXPORT MediaFoundationVideoEncodeAccelerator
: public VideoEncodeAccelerator {
public:
MediaFoundationVideoEncodeAccelerator();
// VideoEncodeAccelerator implementation.
VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() override;
bool Initialize(VideoPixelFormat input_format,
const gfx::Size& input_visible_size,
VideoCodecProfile output_profile,
uint32_t initial_bitrate,
Client* client) override;
void Encode(const scoped_refptr<VideoFrame>& frame,
bool force_keyframe) override;
void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override;
void RequestEncodingParametersChange(uint32_t bitrate,
uint32_t framerate) override;
void Destroy() override;
// Preload dlls required for encoding.
static void PreSandboxInitialization();
protected:
~MediaFoundationVideoEncodeAccelerator() override;
private:
// Holds output buffers coming from the client ready to be filled.
struct BitstreamBufferRef;
// Holds output buffers coming from the encoder.
class EncodeOutput;
// Initializes and allocates memory for input and output samples.
bool InitializeInputOutputSamples();
// Initializes encoder parameters for real-time use.
bool SetEncoderModes();
// Helper function to notify the client of an error on |client_task_runner_|.
void NotifyError(VideoEncodeAccelerator::Error error);
// Encoding tasks to be run on |encoder_thread_|.
void EncodeTask(const scoped_refptr<VideoFrame>& frame, bool force_keyframe);
// Checks for and copies encoded output on |encoder_thread_|.
void ProcessOutput();
// Inserts the output buffers for reuse on |encoder_thread_|.
void UseOutputBitstreamBufferTask(
std::unique_ptr<BitstreamBufferRef> buffer_ref);
// Copies EncodeOutput into a BitstreamBuffer and returns it to the |client_|.
void ReturnBitstreamBuffer(
std::unique_ptr<EncodeOutput> encode_output,
std::unique_ptr<MediaFoundationVideoEncodeAccelerator::BitstreamBufferRef>
buffer_ref);
// Changes encode parameters on |encoder_thread_|.
void RequestEncodingParametersChangeTask(uint32_t bitrate,
uint32_t framerate);
// Destroys encode session on |encoder_thread_|.
void DestroyTask();
// Bitstream buffers ready to be used to return encoded output as a FIFO.
std::deque<std::unique_ptr<BitstreamBufferRef>> bitstream_buffer_queue_;
// EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO.
std::deque<std::unique_ptr<EncodeOutput>> encoder_output_queue_;
gfx::Size input_visible_size_;
size_t bitstream_buffer_size_;
int32_t frame_rate_;
int32_t target_bitrate_;
size_t u_plane_offset_;
size_t v_plane_offset_;
base::win::ScopedComPtr<IMFTransform> encoder_;
base::win::ScopedComPtr<ICodecAPI> codec_api_;
DWORD input_stream_count_min_;
DWORD input_stream_count_max_;
DWORD output_stream_count_min_;
DWORD output_stream_count_max_;
base::win::ScopedComPtr<IMFSample> input_sample_;
base::win::ScopedComPtr<IMFSample> output_sample_;
// To expose client callbacks from VideoEncodeAccelerator.
// NOTE: all calls to this object *MUST* be executed on |client_task_runner_|.
base::WeakPtr<Client> client_;
std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_;
// Our original calling task runner for the child thread.
const scoped_refptr<base::SequencedTaskRunner> client_task_runner_;
// Sequence checker to enforce that the methods of this object are called in
// sequence.
base::SequenceChecker sequence_checker_;
// This thread services tasks posted from the VEA API entry points by the
// GPU child thread and CompressionCallback() posted from device thread.
base::Thread encoder_thread_;
scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_;
// Declared last to ensure that all weak pointers are invalidated before
// other destructors run.
base::WeakPtrFactory<MediaFoundationVideoEncodeAccelerator>
encoder_task_weak_factory_;
DISALLOW_COPY_AND_ASSIGN(MediaFoundationVideoEncodeAccelerator);
};
} // namespace media
#endif // MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_
\ No newline at end of file
......@@ -1739,6 +1739,8 @@
'include_dirs': [ '..', ],
'defines': [ 'MF_INITIALIZER_IMPLEMENTATION', ],
'sources': [
'base/win/mf_helpers.cc',
'base/win/mf_helpers.h',
'base/win/mf_initializer_export.h',
'base/win/mf_initializer.cc',
'base/win/mf_initializer.h',
......@@ -2132,7 +2134,7 @@
}
]
}],
['chromeos==1 or OS=="mac"', {
['chromeos==1 or OS=="mac" or OS=="win"', {
'targets': [
{
'target_name': 'video_encode_accelerator_unittest',
......@@ -2164,6 +2166,11 @@
'../third_party/webrtc/common_video/common_video.gyp:common_video',
],
}],
['OS=="win"', {
'dependencies': [
'../media/media.gyp:mf_initializer',
],
}],
['use_x11==1', {
'dependencies': [
'../ui/gfx/x/gfx_x11.gyp:gfx_x11',
......
......@@ -328,6 +328,7 @@
'dependencies': [
'../media/media.gyp:media',
'../media/media.gyp:mf_initializer',
'../third_party/libyuv/libyuv.gyp:libyuv',
'../ui/gl/gl.gyp:gl',
'../ui/gl/init/gl_init.gyp:gl_init',
],
......@@ -358,6 +359,8 @@
'gpu/dxva_picture_buffer_win.h',
'gpu/dxva_video_decode_accelerator_win.cc',
'gpu/dxva_video_decode_accelerator_win.h',
'gpu/media_foundation_video_encode_accelerator_win.cc',
'gpu/media_foundation_video_encode_accelerator_win.h',
],
'include_dirs': [
'<(DEPTH)/third_party/khronos',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment