Skip to content

Conversation

@thirumalai-qcom
Copy link

@thirumalai-qcom thirumalai-qcom commented Mar 24, 2025

Description

This PR introduces the media foundation plugin to OBS Studio, enabling Media Foundation-based encoding for H264, HEVC and AV1. This plugin supports hardware-accelerated encoding on Windows on ARM (WoA) devices, specifically those utilizing Qualcomm hardware encoders. The Key changes include:

  • mediafoundation: Add base files for MFT encoders support on WoA
  • mediafoundation: Add MFT H264 encoder for WoA
  • mediafoundation: Add MFT HEVC encoder for WoA
  • mediafoundation: Add MFT AV1 encoder for WoA
  • cmake: Add mediafoundation plugin to OBS windows build

Motivation and Context

The primary motivation for these changes is to enable hardware-accelerated encoding on Windows on ARM (WoA) devices by reintroducing the win-mf plugin in OBS Studio. With the growing adoption of WoA devices, ensuring OBS Studio can leverage hardware encoders on this platform improves performance and efficiency, reducing CPU load while maintaining high-quality encoding.

By integrating the mediafoundation plugin into the build system, we enable native support for Media Foundation-based encoding on WoA, ensuring users on this platform can leverage hardware acceleration for streaming and recording. This change ensures better utilization of Qualcomm hardware encoders, providing a more efficient and optimized encoding experience.

How Has This Been Tested?

The changes have been tested by building OBS Studio on a Windows on ARM (WoA) device with Qualcomm hardware. The build completed successfully, and basic functionality tests confirmed that OBS Studio runs as expected with the media foundation plugin. Media Foundation-based encoding for H264, HEVC and AV1 was verified to function correctly, ensuring proper hardware acceleration on WoA devices utilizing Qualcomm hardware.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@WizardCM WizardCM added Seeking Testers Build artifacts on CI New Feature New feature or plugin labels Mar 24, 2025
@thirumalai-qcom thirumalai-qcom force-pushed the mft-support branch 2 times, most recently from 55aa652 to ea69f55 Compare March 25, 2025 11:32
@thirumalai-qcom thirumalai-qcom marked this pull request as ready for review March 25, 2025 11:55
Copy link
Member

@PatTheMav PatTheMav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wary about just reintroducing the old code as-is, particularly as we have progressed to C++17 and expect modern C++ coding practices, including but not limited to:

  • Use of anonymous namespaces instead of static functions is deprecated in C++ (static functions are only used for class methods in modern C++).
  • Use of constexpr as it's preferable over macros in almost all situations as they enable the compiler to optimise code better and sometimes resolve entire code paths at compile time
  • Abuse of macros instead of functions marked as constexpr (see above) which makes the code harder to debug

Just merging this would mean re-importing the code smell as well and I just don't see someone else volunteering to "refactor" it after it's been merged again.

@derrod
Copy link
Member

derrod commented Mar 25, 2025

I tested this on my Snapdragon X laptop and it does work as expected.

But Ideally this plugin should be either rewritten from the ground up, or replaced wholesale with just using the FFmpeg implemenation. At the very least, the AAC encoder could probably be removed. Of course since this is ancient it also doesn't support AV1 (which the X Elite chips do support).

Aside from what Pat mentioned, the naming also conflicts with #10471 right now.

@dd-han
Copy link

dd-han commented Apr 13, 2025

Will this work with my older Surface Pro X (SQ2)?

If it works I can help test it.

@thirumalai-qcom thirumalai-qcom force-pushed the mft-support branch 4 times, most recently from 13c8aac to 2aa95e6 Compare May 20, 2025 11:10
@thirumalai-qcom
Copy link
Author

Will this work with my older Surface Pro X (SQ2)?
If it works I can help test it.

@dd-han Thanks for offering to help.
I believe it should work with your Surface Pro X [SQ2] which is based on the Snapdragon 8cx, If you get a change to try it out, please let me know how it goes.

@SA-Root
Copy link

SA-Root commented May 31, 2025

Hi, I tried using version 31.1.0 beta 1 on my pc with 8cx Gen 3, and I got 2 problems with MF based encoding:

  1. When using h264_mf encoder, it seems to be using CPU to encode the video.
    image
    image

  2. When I tried to use hevc_mf encoder, OBS immediately crashed when I click the Start Recording button. The corresponding log files are attached below.
    image
    image

2025-05-31 21-12-18.txt
Crash 2025-05-31 21-14-41.txt

image

@RytoEX
Copy link
Member

RytoEX commented May 31, 2025

Hi, I tried using version 31.1.0 beta 1 on my pc with 8cx Gen 3, and I got 2 problems with MF based encoding:

  1. When using h264_mf encoder, it seems to be using CPU to encode the video.

You are using the Media Foundation encoder via FFmpeg. That is not what is in this PR, and this PR has not been merged, so it is not in OBS Studio 31.1.0 Beta 1. Please use our forums or Discord for assistance.

@driver1998
Copy link

@SA-Root When using h264_mf encoder, it seems to be using CPU to encode the video.

You need to pass additional flags to make hardware encoding work with the ffmpeg backend.
See https://driver1998.github.io/posts/ffmpeg-hardware-video-encoding-on-qualcomm-woa/ (Chinese only)

Copy link
Member

@PatTheMav PatTheMav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a first round of reviews, will still have to look at the HEVC and H264 encoder implementations.

Except for the missing newline issues I haven't commented on every repeat issue, so things like functions marked inline in compilation units, opaque variable names, "magic" global variables, should be fixed throughout and not just were I first encountered them in review.

@derrod
Copy link
Member

derrod commented Jul 10, 2025

Couple more notes:

  • You should remove the AAC encoder, it was removed due to glitches and we don't really have a reason to reintroduce it
    • If you insist on keeping it, add the .codecs property so it is correctly recognised by the encoder selectors, currently it's not selectable due to missing codec information
  • MediaFoundation does support DirectX textures as encoder input since Windows 8.1 and that should be preferred over copying data to system RAM

@thirumalai-qcom thirumalai-qcom changed the title win-mf: Reintroduce win-mf plugin for Media Foundation-based encoding on WoA WIP:: win-mf: Reintroduce win-mf plugin for Media Foundation-based encoding on WoA Aug 4, 2025
@thirumalai-qcom thirumalai-qcom force-pushed the mft-support branch 2 times, most recently from 88a1a72 to 8ba0168 Compare August 12, 2025 12:31
@PatTheMav
Copy link
Member

Couple more notes:

  • You should remove the AAC encoder, it was removed due to glitches and we don't really have a reason to reintroduce it

    • If you insist on keeping it, add the .codecs property so it is correctly recognised by the encoder selectors, currently it's not selectable due to missing codec information
  • MediaFoundation does support DirectX textures as encoder input since Windows 8.1 and that should be preferred over copying data to system RAM

@thirumalai-qcom did you have a chance to review these notes as well?

@thirumalai-qcom thirumalai-qcom force-pushed the mft-support branch 2 times, most recently from 06727f5 to 2df9b97 Compare August 22, 2025 10:48
@thirumalai-qcom
Copy link
Author

thirumalai-qcom commented Aug 22, 2025

I tested this on my Snapdragon X laptop and it does work as expected.

@derrod Thanks for Confirming.

At the very least, the AAC encoder could probably be removed.

Apologies for the delayed response, there were some internal discussions on this. The AAC related changes have been dropped from this PR.

Of course since this is ancient it also doesn't support AV1 (which the X Elite chips do support).

There are also plans to add AV1 support. I'll get back on this shortly.

  • MediaFoundation does support DirectX textures as encoder input since Windows 8.1 and that should be preferred over copying data to system RAM

@PatTheMav @derrod
Got this preference confirmed from @Fenrirthviti , will keep you posted on this. 👍

@thirumalai-qcom thirumalai-qcom changed the title WIP:: win-mf: Reintroduce win-mf plugin for Media Foundation-based encoding on WoA win-mf: Reintroduce win-mf plugin for Media Foundation-based encoding on WoA Aug 23, 2025
@thirumalai-qcom thirumalai-qcom force-pushed the mft-support branch 2 times, most recently from c299d3b to 69c00db Compare September 4, 2025 13:15
@PatTheMav
Copy link
Member

I tested this on my Snapdragon X laptop and it does work as expected.

@derrod Thanks for Confirming.

At the very least, the AAC encoder could probably be removed.

Apologies for the delayed response, there were some internal discussions on this. The AAC related changes have been dropped from this PR.

Of course since this is ancient it also doesn't support AV1 (which the X Elite chips do support).

There are also plans to add AV1 support. I'll get back on this shortly.

  • MediaFoundation does support DirectX textures as encoder input since Windows 8.1 and that should be preferred over copying data to system RAM

@PatTheMav @derrod Got this preference confirmed from @Fenrirthviti , will keep you posted on this. 👍

Just as a heads-up, because there's another PR that introduces support for Media Foundation-based capture devices, we have a potential conflict between both using the win-mf subdirectory.

I suggested a solution to this on the other PR that I'd recommend this PR leans into as well: #10471 (comment)

@thirumalai-qcom thirumalai-qcom changed the title win-mf: Reintroduce win-mf plugin for Media Foundation-based encoding on WoA mediafoundation: Introduce Media Foundation-based encoding support on WoA Sep 8, 2025
@thirumalai-qcom
Copy link
Author

@derrod
Of course since this is ancient it also doesn't support AV1 (which the X Elite chips do support).
There are also plans to add AV1 support. I'll get back on this shortly.

Happy to announce, AV1 support has now been successfully integrated into this PR. 🥳 The CI-generated binaries of this PR include AV1 functionality.

Just as a heads-up, because there's another PR that introduces support for Media Foundation-based capture devices, we have a potential conflict between both using the win-mf subdirectory.

I suggested a solution to this on the other PR that I'd recommend this PR leans into as well: #10471 (comment)

@PatTheMav, Thanks for the heads-up regarding the potential conflict with the win-mf subdirectory. I've reviewed your suggestion and applied the recommended changes. Please take a look and let me know if any further improvements are needed.

@PatTheMav
Copy link
Member

@PatTheMav, Thanks for the heads-up regarding the potential conflict with the win-mf subdirectory. I've reviewed your suggestion and applied the recommended changes. Please take a look and let me know if any further improvements are needed.

Sorry for the delayed response - I saw that, much appreciated.

I'll have to look into both PRs a bit more closely as both initially wanted to "be" win-mf, but that's not possible as they would clobber each other. So I need to figure out what the correct way is for this scenario and come up with a naming scheme you could follow.

This commit re-introduces the base files required for Media Foundation
Transform (MFT) support in the mediafoundation plugin, specifically
targeting Windows on ARM devices with Qualcomm hardware. It includes
core implementation files, language configuration file for localization,
and the initial CMakeLists.txt for integration.
This commit introduces H264 encoder support using the Media Foundation
Transform (MFT) interface, specifically targeting Windows on ARM (WoA)
devices with Qualcomm hardware. It adds mf-h264.cpp and
mf-h264-encoder.cpp/.hpp to implement the encoding logic.
This commit introduces HEVC encoder support using the Media Foundation
Transform (MFT) interface, specifically targeting Windows on ARM (WoA)
devices with Qualcomm hardware. It adds mf-hevc.cpp and
mf-hevc-encoder.cpp/.hpp to implement the encoding logic.
This commit introduces AV1 encoder support using the Media Foundation
Transform (MFT) interface, specifically targeting Windows on ARM (WoA)
devices with Qualcomm hardware. It adds mf-av1.cpp and
mf-av1-encoder.cpp/.hpp to implement the encoding logic.
This commit updates the CMakeLists.txt file in the plugins directory to
incorporate the mediafoundation plugin into OBS build system. This
ensures that Media Foundation-based encoding is available for Windows
on ARM (WoA) devices, utilizing Qualcomm hardware for efficient video
Encoding.
@thirumalai-qcom
Copy link
Author

Hi @RytoEX & @PatTheMav
All comments from the previous review rounds have been addressed and resolved. Could you please take a moment to review and let us know if there are any additional comments or if we’re good to proceed with approval? Thanks in advance.

Copy link
Member

@PatTheMav PatTheMav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine apart from the few things I found. This will probably require a small refactor to change the plugin ID and also final output directory once we have guidance, but those should be less impactful final changes.

Comment on lines +3 to +4
add_library(win-mf MODULE)
add_library(OBS::win-mf ALIAS win-mf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small heads-up, this name will need to change (here and throughout the plugin code) as it should distinguish itself from the Media Foundation-based capture plugin. I asked for guidance what that name should be, so that'd hopefully just be a final "refactor->rename" operation.

Comment on lines +9 to +22
mf-common.cpp
mf-encoder-descriptor.cpp
mf-h264-encoder.cpp
mf-h264.cpp
mf-hevc-encoder.cpp
mf-av1.cpp
mf-av1-encoder.cpp
mf-hevc.cpp
mf-plugin.cpp
mf-common.hpp
mf-encoder-descriptor.hpp
mf-h264-encoder.hpp
mf-hevc-encoder.hpp
mf-av1-encoder.hpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be sorted alphabetically.

CHECK_HR_ERROR(o->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlaceMode::MFVideoInterlace_Progressive));
CHECK_HR_ERROR(MFSetAttributeRatio(o, MF_MT_PIXEL_ASPECT_RATIO, 1, 1));
CHECK_HR_ERROR(o->SetUINT32(MF_MT_VIDEO_LEVEL, (UINT32)-1));
//CHECK_HR_ERROR(o->SetUINT32(MF_MT_MPEG2_PROFILE, MapProfile(profile)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good reason to retain this as a comment, or is this unused and thus can be removed?

Comment on lines +279 to +280
//#undef MFTEXT
//#undef MFP
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If those lines are not needed anymore, they should be removed.

Comment on lines +284 to +285
//#undef MFTEXT
//#undef MFP
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If those lines are not needed anymore they should be removed.

}

OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("win-mf", "en-US")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted above, the id will need to change. Will reply back once we have guidance for the preferred name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

New Feature New feature or plugin Seeking Testers Build artifacts on CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants