Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit be059ce

Browse files
committed
Add adatper check for initializing d3d11 device. Also add D3D11 flag for MfxSession creation
1 parent d288e13 commit be059ce

File tree

8 files changed

+143
-69
lines changed

8 files changed

+143
-69
lines changed

talk/owt/sdk/base/win/msdkvideobase.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static bool isValidPluginUID(const mfxPluginUID* uid) {
2929
return (AreGuidsEqual(uid, &MFX_PLUGINID_HEVCD_HW) ||
3030
AreGuidsEqual(uid, &MFX_PLUGINID_HEVCE_HW) ||
3131
AreGuidsEqual(uid, &MFX_PLUGINID_VP8D_HW) ||
32+
AreGuidsEqual(uid, &MFX_PLUGINID_VP9D_HW) ||
3233
AreGuidsEqual(uid, &MFX_PLUGINID_HEVCE_GACC));
3334
}
3435

@@ -68,9 +69,11 @@ MSDKFactory* MSDKFactory::Get() {
6869
return singleton;
6970
}
7071

71-
MFXVideoSession* MSDKFactory::InternalCreateSession() {
72+
MFXVideoSession* MSDKFactory::InternalCreateSession(bool use_d3d11) {
7273
mfxStatus sts = MFX_ERR_NONE;
7374
mfxIMPL impl = MFX_IMPL_HARDWARE_ANY;
75+
if (use_d3d11)
76+
impl |= MFX_IMPL_VIA_D3D11;
7477
mfxVersion version = {{3, 1}};
7578

7679
MFXVideoSession* session = new MFXVideoSession();
@@ -87,11 +90,11 @@ MFXVideoSession* MSDKFactory::InternalCreateSession() {
8790
return session;
8891
}
8992

90-
MFXVideoSession* MSDKFactory::CreateSession() {
93+
MFXVideoSession* MSDKFactory::CreateSession(bool use_d3d11) {
9194
mfxStatus sts = MFX_ERR_NONE;
9295
MFXVideoSession* session = nullptr;
9396

94-
session = InternalCreateSession();
97+
session = InternalCreateSession(use_d3d11);
9598

9699
if (!session) {
97100
return nullptr;

talk/owt/sdk/base/win/msdkvideobase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MSDKFactory {
3131

3232
static MSDKFactory* Get();
3333

34-
MFXVideoSession* CreateSession();
34+
MFXVideoSession* CreateSession(bool use_d3d11 = true);
3535

3636
void DestroySession(MFXVideoSession* session);
3737

@@ -99,7 +99,7 @@ class MSDKFactory {
9999
protected:
100100
MSDKFactory();
101101
bool Init();
102-
MFXVideoSession* InternalCreateSession();
102+
MFXVideoSession* InternalCreateSession(bool use_d3d11 = true);
103103

104104
private:
105105
static MSDKFactory* singleton;

talk/owt/sdk/base/win/msdkvideodecoder.cc

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
#include "msdkvideobase.h"
6+
#include "mfxadapter.h"
67
#include "talk/owt/sdk/base/nativehandlebuffer.h"
78
#include "talk/owt/sdk/base/win/d3d11_allocator.h"
89
#include "talk/owt/sdk/base/win/d3dnativeframe.h"
@@ -67,15 +68,53 @@ void MSDKVideoDecoder::CheckOnCodecThread() {
6768

6869
bool MSDKVideoDecoder::CreateD3D11Device() {
6970
HRESULT hr = S_OK;
70-
UINT create_flag = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
7171

7272
static D3D_FEATURE_LEVEL feature_levels[] = {
7373
D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1,
7474
D3D_FEATURE_LEVEL_10_1};
7575
D3D_FEATURE_LEVEL feature_levels_out;
7676

77+
mfxU8 headers[] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xE0, 0x0A, 0x96,
78+
0x52, 0x85, 0x89, 0xC8, 0x00, 0x00, 0x00, 0x01, 0x68,
79+
0xC9, 0x23, 0xC8, 0x00, 0x00, 0x00, 0x01, 0x09, 0x10};
80+
mfxBitstream bs = {};
81+
bs.Data = headers;
82+
bs.DataLength = bs.MaxLength = sizeof(headers);
83+
84+
mfxStatus sts = MFX_ERR_NONE;
85+
mfxU32 num_adapters;
86+
sts = MFXQueryAdaptersNumber(&num_adapters);
87+
88+
if (sts != MFX_ERR_NONE)
89+
return false;
90+
91+
std::vector<mfxAdapterInfo> display_data(num_adapters);
92+
mfxAdaptersInfo adapters = {display_data.data(), mfxU32(display_data.size()),
93+
0u};
94+
sts = MFXQueryAdaptersDecode(&bs, MFX_CODEC_AVC, &adapters);
95+
if (sts != MFX_ERR_NONE) {
96+
RTC_LOG(LS_ERROR) << "Failed to query adapter with hardware acceleration";
97+
return false;
98+
}
99+
mfxU32 adapter_idx = adapters.Adapters[0].Number;
100+
101+
hr = CreateDXGIFactory(__uuidof(IDXGIFactory2), (void**)(&m_pDXGIFactory));
102+
if (FAILED(hr)) {
103+
RTC_LOG(LS_ERROR)
104+
<< "Failed to create dxgi factory for adatper enumeration.";
105+
return false;
106+
}
107+
108+
hr = m_pDXGIFactory->EnumAdapters(adapter_idx, &m_pAdapter);
109+
if (FAILED(hr)) {
110+
RTC_LOG(LS_ERROR) << "Failed to enum adapter for specified adapter index.";
111+
return false;
112+
}
113+
114+
// On DG1 this setting driver type to hardware will result-in device
115+
// creation failure.
77116
hr = D3D11CreateDevice(
78-
nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, create_flag, feature_levels,
117+
m_pAdapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, feature_levels,
79118
sizeof(feature_levels) / sizeof(feature_levels[0]), D3D11_SDK_VERSION,
80119
&d3d11_device, &feature_levels_out, &d3d11_device_context);
81120
if (FAILED(hr)) {
@@ -173,9 +212,9 @@ int32_t MSDKVideoDecoder::InitDecodeOnCodecThread() {
173212
codec_id = MFX_CODEC_AV1;
174213
}
175214

176-
if (!factory->LoadDecoderPlugin(codec_id, m_mfxSession, &m_pluginID)) {
177-
return WEBRTC_VIDEO_CODEC_ERROR;
178-
}
215+
//if (!factory->LoadDecoderPlugin(codec_id, m_mfxSession, &m_pluginID)) {
216+
// return WEBRTC_VIDEO_CODEC_ERROR;
217+
//}
179218

180219
if (!CreateD3D11Device()) {
181220
return WEBRTC_VIDEO_CODEC_ERROR;
@@ -256,6 +295,8 @@ int32_t MSDKVideoDecoder::Decode(
256295
return WEBRTC_VIDEO_CODEC_ERROR;
257296
}
258297
nSurfNum = MSDK_MAX(request.NumFrameSuggested, 1);
298+
299+
request.Type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
259300
sts = m_pMFXAllocator->Alloc(m_pMFXAllocator->pthis, &request,
260301
&m_mfxResponse);
261302
if (MFX_ERR_NONE != sts) {
@@ -275,6 +316,19 @@ int32_t MSDKVideoDecoder::Decode(
275316
MSDK_MEMCPY_VAR(m_pInputSurfaces[i].Info, &(request.Info),
276317
sizeof(mfxFrameInfo));
277318
m_pInputSurfaces[i].Data.MemId = m_mfxResponse.mids[i];
319+
m_pInputSurfaces[i].Data.MemType = request.Type;
320+
}
321+
322+
if (!m_mfxVideoParams.mfx.FrameInfo.FrameRateExtN ||
323+
m_mfxVideoParams.mfx.FrameInfo.FrameRateExtD) {
324+
m_mfxVideoParams.mfx.FrameInfo.FrameRateExtN = 30;
325+
m_mfxVideoParams.mfx.FrameInfo.FrameRateExtD = 1;
326+
}
327+
328+
if (!m_mfxVideoParams.mfx.FrameInfo.AspectRatioH ||
329+
!m_mfxVideoParams.mfx.FrameInfo.AspectRatioW) {
330+
m_mfxVideoParams.mfx.FrameInfo.AspectRatioH = 1;
331+
m_mfxVideoParams.mfx.FrameInfo.AspectRatioW = 1;
278332
}
279333
// Finally we're done with all configurations and we're OK to init the
280334
// decoder.
@@ -316,12 +370,15 @@ int32_t MSDKVideoDecoder::Decode(
316370
if (sts == MFX_ERR_NONE && syncp != nullptr) {
317371
sts = m_mfxSession->SyncOperation(syncp, MSDK_DEC_WAIT_INTERVAL);
318372
if (sts >= MFX_ERR_NONE) {
319-
mfxHDLPair* dxMemId = (mfxHDLPair*)pOutputSurface->Data.MemId;
320-
373+
mfxMemId dxMemId = pOutputSurface->Data.MemId;
374+
mfxHDLPair pair = {nullptr};
375+
// Maybe we should also send the allocator as part of the frame
376+
// handle for locking/unlocking purpose.
377+
m_pMFXAllocator->GetFrameHDL(dxMemId, (mfxHDL*)&pair);
321378
if (callback_) {
322379
surface_handle->d3d11_device = d3d11_device.p;
323380
surface_handle->texture =
324-
reinterpret_cast<ID3D11Texture2D*>(dxMemId->first);
381+
reinterpret_cast<ID3D11Texture2D*>(pair.first);
325382
// Texture_array_index not used when decoding with MSDK.
326383
surface_handle->texture_array_index = 0;
327384
D3D11_TEXTURE2D_DESC texture_desc;

talk/owt/sdk/base/win/msdkvideodecoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <codecapi.h>
88
#include <combaseapi.h>
99
#include <d3d11.h>
10+
#include <dxgi1_2.h>
1011
#include <dxva2api.h>
1112
#include <memory>
1213
#include <utility>
@@ -93,6 +94,8 @@ class MSDKVideoDecoder : public webrtc::VideoDecoder {
9394
CComPtr<ID3D11DeviceContext> d3d11_device_context;
9495
CComPtr<ID3D11VideoDevice> d3d11_video_device;
9596
CComPtr<ID3D11VideoContext> d3d11_video_context;
97+
CComQIPtr<IDXGIAdapter> m_pAdapter;
98+
CComPtr<IDXGIFactory2> m_pDXGIFactory;
9699
// Store current decoded frame.
97100
std::unique_ptr<D3D11ImageHandle> surface_handle;
98101

talk/owt/sdk/base/win/msdkvideodecoderfactory.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ std::unique_ptr<webrtc::VideoDecoder> MSDKVideoDecoderFactory::CreateVideoDecode
8080
else if (codec == webrtc::kVideoCodecVP9)
8181
vp9_hw = true;
8282
#ifndef DISABLE_H265
83-
else if (codec == webrtc::kVideoCodecH265)
83+
else if (codec == webrtc::kVideoCodecH265) {
8484
h265_hw = true;
85+
RTC_LOG(LS_ERROR) << "Enabling hardware hevc.";
86+
}
8587
#endif
8688
}
8789
if (absl::EqualsIgnoreCase(format.name, cricket::kVp9CodecName) && !vp9_hw) {
@@ -98,7 +100,8 @@ std::unique_ptr<webrtc::VideoDecoder> MSDKVideoDecoderFactory::CreateVideoDecode
98100
#ifndef DISABLE_H265
99101
// This should not happen.
100102
else if (absl::EqualsIgnoreCase(format.name, cricket::kH265CodecName) && !h265_hw) {
101-
return nullptr;
103+
RTC_LOG(LS_ERROR) << "Returning null hevc encoder.";
104+
//return nullptr;
102105
}
103106
#endif
104107

@@ -108,7 +111,7 @@ std::unique_ptr<webrtc::VideoDecoder> MSDKVideoDecoderFactory::CreateVideoDecode
108111
std::vector<webrtc::SdpVideoFormat> MSDKVideoDecoderFactory::GetSupportedFormats()
109112
const {
110113
std::vector<webrtc::SdpVideoFormat> supported_codecs;
111-
//supported_codecs.push_back(webrtc::SdpVideoFormat(cricket::kVp8CodecName));
114+
supported_codecs.push_back(webrtc::SdpVideoFormat(cricket::kVp8CodecName));
112115
for (const webrtc::SdpVideoFormat& format : webrtc::SupportedVP9Codecs())
113116
supported_codecs.push_back(format);
114117
for (const webrtc::SdpVideoFormat& format : owt::base::CodecUtils::SupportedH264Codecs())

0 commit comments

Comments
 (0)