Skip to content

Commit 4085cf5

Browse files
committed
win-mf: Add Intel NPU AI Features
1 parent ba1f334 commit 4085cf5

1 file changed

Lines changed: 146 additions & 1 deletion

File tree

plugins/win-mf/win-mf.cpp

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ using namespace std;
7171
#define TEXT_RANGE_FULL obs_module_text("ColorRange.Full")
7272
#define TEXT_DWNS obs_module_text("DeactivateWhenNotShowing")
7373

74+
#define INTELNPU_BLUR_TYPE "intelnpu_blur_type"
75+
#define TEXT_INTELNPU_BLUR_TYPE obs_module_text("IntelNPU.BackgroundBlur")
76+
#define TEXT_INTELNPU_BLUR_NONE obs_module_text("IntelNPU.BlurNone")
77+
#define TEXT_INTELNPU_BLUR_STANDARD obs_module_text("IntelNPU.BlurStandard")
78+
#define TEXT_INTELNPU_BLUR_PORTRAIT obs_module_text("IntelNPU.Portrait")
79+
80+
#define INTELNPU_BACKGROUND_REMOVAL "intelnpu_background_removal"
81+
#define TEXT_INTELNPU_BACKGROUND_REMOVAL \
82+
obs_module_text("IntelNPU.BackgroundRemoval")
83+
84+
#define INTELNPU_AUTO_FRAMING "intelnpu_auto_framing"
85+
#define TEXT_INTELNPU_AUTO_FRAMING obs_module_text("IntelNPU.AutoFraming")
86+
87+
#define INTELNPU_EYEGAZE_CORRECTION "intelnpu_eyegaze_correction"
88+
#define TEXT_INTELNPU_EYEGAZE_CORRECTION \
89+
obs_module_text("IntelNPU.EyeGazeCorrection")
90+
91+
enum BlurType {
92+
NpuBlurType_None,
93+
NpuBlurType_Standard,
94+
NpuBlurType_Portrait,
95+
};
96+
7497
enum ResType {
7598
ResTypePreferred,
7699
ResTypeCustom,
@@ -114,7 +137,8 @@ enum class Action {
114137
Deactivate,
115138
Shutdown,
116139
SaveSettings,
117-
RestoreSettings
140+
RestoreSettings,
141+
NpuControl
118142
};
119143

120144
struct MediaFoundationVideoInfo {
@@ -492,6 +516,66 @@ void MediaFoundationSourceInput::MediaFoundationSourceLoop()
492516
MF_RestoreDefaultSettings(_mfcaptureDevice);
493517
}
494518
break;
519+
case Action::NpuControl:
520+
if (_mfcaptureDevice) {
521+
522+
obs_data_t *settings;
523+
settings = obs_source_get_settings(source);
524+
bool blur, shallowfocus, mask;
525+
526+
int val = (int)obs_data_get_int(
527+
settings, INTELNPU_BLUR_TYPE);
528+
switch (val) {
529+
case NpuBlurType_None:
530+
blur = false;
531+
shallowfocus = false;
532+
break;
533+
case NpuBlurType_Standard:
534+
blur = true;
535+
shallowfocus = false;
536+
break;
537+
case NpuBlurType_Portrait:
538+
blur = true;
539+
shallowfocus = true;
540+
break;
541+
}
542+
mask = obs_data_get_bool(
543+
settings, INTELNPU_BACKGROUND_REMOVAL);
544+
545+
bool _blur, _shallowfocus, _mask, _transparent;
546+
MF_GetBlur(_mfcaptureDevice, _blur,
547+
_shallowfocus, _mask);
548+
MF_GetTransparent(_mfcaptureDevice,
549+
_transparent);
550+
if (_blur != blur ||
551+
_shallowfocus != shallowfocus ||
552+
_transparent != mask) {
553+
MF_SetBlur(_mfcaptureDevice, blur,
554+
shallowfocus, mask);
555+
}
556+
557+
bool autoframing = obs_data_get_bool(
558+
settings, INTELNPU_AUTO_FRAMING);
559+
bool _autoframing;
560+
MF_GetAutoFraming(_mfcaptureDevice,
561+
_autoframing);
562+
if (_autoframing != autoframing) {
563+
MF_SetAutoFraming(_mfcaptureDevice,
564+
autoframing);
565+
}
566+
567+
bool eyegaze = obs_data_get_bool(
568+
settings, INTELNPU_EYEGAZE_CORRECTION);
569+
bool _eyegaze;
570+
MF_GetEyeGazeCorrection(_mfcaptureDevice,
571+
_eyegaze);
572+
if (_eyegaze != eyegaze) {
573+
MF_SetEyeGazeCorrection(
574+
_mfcaptureDevice, eyegaze);
575+
}
576+
obs_data_release(settings);
577+
}
578+
break;
495579
case Action::None:;
496580
}
497581
}
@@ -1485,6 +1569,42 @@ static bool ActivateClicked(obs_properties_t *, obs_property_t *p, void *data)
14851569
return true;
14861570
}
14871571

1572+
bool NPUDetection()
1573+
{
1574+
// You begin DXCore adapter enumeration by creating an adapter factory.
1575+
winrt::com_ptr<IDXCoreAdapterFactory> adapterFactory;
1576+
winrt::check_hresult(
1577+
::DXCoreCreateAdapterFactory(adapterFactory.put()));
1578+
1579+
// From the factory, retrieve a list of all the Direct3D 12 Core Compute adapters.
1580+
winrt::com_ptr<IDXCoreAdapterList> d3D12CoreComputeAdapters;
1581+
GUID attributes[]{DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE};
1582+
winrt::check_hresult(adapterFactory->CreateAdapterList(
1583+
_countof(attributes), attributes,
1584+
d3D12CoreComputeAdapters.put()));
1585+
1586+
const uint32_t count{d3D12CoreComputeAdapters->GetAdapterCount()};
1587+
1588+
bool npuDetected = false;
1589+
1590+
for (uint32_t i = 0; i < count; ++i) {
1591+
winrt::com_ptr<IDXCoreAdapter> candidateAdapter;
1592+
winrt::check_hresult(d3D12CoreComputeAdapters->GetAdapter(
1593+
i, candidateAdapter.put()));
1594+
1595+
char description[256];
1596+
winrt::check_hresult(candidateAdapter->GetProperty(
1597+
DXCoreAdapterProperty::DriverDescription,
1598+
&description));
1599+
1600+
char npu[256] = "Intel(R) AI Boost";
1601+
if (strcmp(description, npu) == 0)
1602+
npuDetected = true;
1603+
}
1604+
1605+
return npuDetected;
1606+
}
1607+
14881608
static obs_properties_t *GetMediaFoundationSourceProperties(void *obj)
14891609
{
14901610
MediaFoundationSourceInput *input =
@@ -1520,6 +1640,31 @@ static obs_properties_t *GetMediaFoundationSourceProperties(void *obj)
15201640

15211641
obs_properties_add_bool(ppts, DEACTIVATE_WNS, TEXT_DWNS);
15221642

1643+
/* ------------------------------------- */
1644+
/* Intel NPU AI effect settings */
1645+
1646+
if (NPUDetection()) {
1647+
1648+
p = obs_properties_add_list(ppts, INTELNPU_BLUR_TYPE,
1649+
TEXT_INTELNPU_BLUR_TYPE,
1650+
OBS_COMBO_TYPE_LIST,
1651+
OBS_COMBO_FORMAT_INT);
1652+
1653+
obs_property_list_add_int(p, TEXT_INTELNPU_BLUR_NONE,
1654+
NpuBlurType_None);
1655+
obs_property_list_add_int(p, TEXT_INTELNPU_BLUR_STANDARD,
1656+
NpuBlurType_Standard);
1657+
obs_property_list_add_int(p, TEXT_INTELNPU_BLUR_PORTRAIT,
1658+
NpuBlurType_Portrait);
1659+
1660+
obs_properties_add_bool(ppts, INTELNPU_BACKGROUND_REMOVAL,
1661+
TEXT_INTELNPU_BACKGROUND_REMOVAL);
1662+
obs_properties_add_bool(ppts, INTELNPU_AUTO_FRAMING,
1663+
TEXT_INTELNPU_AUTO_FRAMING);
1664+
obs_properties_add_bool(ppts, INTELNPU_EYEGAZE_CORRECTION,
1665+
TEXT_INTELNPU_EYEGAZE_CORRECTION);
1666+
}
1667+
15231668
/* ------------------------------------- */
15241669
/* video settings */
15251670

0 commit comments

Comments
 (0)