Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the codec level for AMF encoders #11548

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lexano-ivs
Copy link
Contributor

Description

The default "level" setting was being used for each codec (AVC, HEVC, AV1) supported by AMF. For example, all HEVC encoders were using level 6.2 and this caused some playback devices to reject decoding the bitstream because the device reported a maximum decode level lower than 6.2.

This PR adds functionality to determine the best match for the codec level instead of relying on the defaults, for AMF encoders.

Motivation and Context

The default behaviour of setting the codec level to the maximum resulted in several Android-based devices failing to decode HEVC streams in the TEB (Twitch Enhanced Broadcasting) beta program, which is currently testing HEVC compatibility. Adjusting the level appropriately instantly fixed the decode failures.

Also, most APIs (such as NVENC and VAAPI) have an "Auto" option for setting the codec level, in which case the driver or library code adjust the level. This Auto option doesn't seem to be available for AMF, hence the need to implement something at the application layer.

How Has This Been Tested?

Several local unit tests were carried out by using a configuration override with Enhanced Broadcasting to verify the selected level is accurate. The code has been (and continues to be) tested in the TEB beta builds.

Types of changes

  • Enhancement (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 the Enhancement Improvement to existing functionality label Nov 24, 2024
@Lain-B
Copy link
Collaborator

Lain-B commented Nov 24, 2024

I'm generally fine with this, but I sort of want to hear other people's opinions.

@RytoEX
Copy link
Member

RytoEX commented Feb 12, 2025

To check this, can I just record locally with AMF and check the codec level with FFmpeg or MediaInfo?

Is this something that can be fixed in a driver or AMF update, or is this always expected to be handled by the encoder implementation?

Should be there bugs filed at https://github.com/GPUOpen-LibrariesAndSDKs/AMF?

Copy link
Member

@RytoEX RytoEX left a comment

Choose a reason for hiding this comment

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

Comment lines can be expanded to up to 120 columns.

@lexano-ivs
Copy link
Contributor Author

lexano-ivs commented Feb 12, 2025

To check this, can I just record locally with AMF and check the codec level with FFmpeg or MediaInfo?

Yes that would work. If you stream using TEB you can also check the log message when streaming starts; I added the "level" output to confirm this was working.

Is this something that can be fixed in a driver or AMF update, or is this always expected to be handled by the encoder implementation?

All AMF documentation and source code indicates the encoder implementation should set this. There is no "Auto" setting. Refer to AMF_VIDEO_ENCODER_HEVC_PROFILE_LEVEL at AMF HEVC docs.

Should be there bugs filed at https://github.com/GPUOpen-LibrariesAndSDKs/AMF?

Yeah probably, but it's an enhancement, not a bug. I've opened GPUOpen-LibrariesAndSDKs/AMF#533.

@RytoEX
Copy link
Member

RytoEX commented Feb 12, 2025

You will probably have to manually rewrap comments to 120 columns because I don't think clang-format will touch them with our settings.

@RytoEX
Copy link
Member

RytoEX commented Feb 12, 2025

I confirmed that the HEVC level is incorrect before these changes. Will check with the changes at a later time.

I did also confirm that the AVC level does change based on the output settings (resolution+FPS) and seems to be mostly what I'd expect (I got 5.1 when I expected 5.2, but this is still different from the default 4.2), so something within AMF appears to be changing that as needed for AVC, but not for HEVC.

The default "level" setting was being used for each codec
(AVC, HEVC, AV1) supported by AMF. For example, all HEVC
encoders were using level 6.2 and this caused some playback
devices to reject the bitstream for decode because the device
reported a maximum decode level lower than 6.2.

Add functionality to determine the best match for the codec
level instead of relying on the defaults.
@lexano-ivs lexano-ivs force-pushed the lexano/amf-codec-level branch from 84e0128 to 94a2599 Compare February 12, 2025 22:59
@lexano-ivs
Copy link
Contributor Author

You will probably have to manually rewrap comments to 120 columns because I don't think clang-format will touch them with our settings.

Reformatted the comments to 120 columns with latest push.

@lexano-ivs
Copy link
Contributor Author

I confirmed that the HEVC level is incorrect before these changes. Will check with the changes at a later time.

I did also confirm that the AVC level does change based on the output settings (resolution+FPS) and seems to be mostly what I'd expect (I got 5.1 when I expected 5.2, but this is still different from the default 4.2), so something within AMF appears to be changing that as needed for AVC, but not for HEVC.

We tripped over this issue when testing HEVC playback in the TEB beta program, where the HEVC level was set to the maximum by default, so it's consistent with your findings re: HEVC. I don't recall at the moment what AVC was reporting back, but given that HEVC needed to be adjusted to solve mobile player compatibility issues (i.e. the mobile app player is doing the right thing and rejecting levels it can't handle), I adjusted AVC, HEVC, and AV1 "just in case".

@RytoEX
Copy link
Member

RytoEX commented Feb 12, 2025

I confirmed that the HEVC level is incorrect before these changes. Will check with the changes at a later time.
I did also confirm that the AVC level does change based on the output settings (resolution+FPS) and seems to be mostly what I'd expect (I got 5.1 when I expected 5.2, but this is still different from the default 4.2), so something within AMF appears to be changing that as needed for AVC, but not for HEVC.

We tripped over this issue when testing HEVC playback in the TEB beta program, where the HEVC level was set to the maximum by default, so it's consistent with your findings re: HEVC. I don't recall at the moment what AVC was reporting back, but given that HEVC needed to be adjusted to solve mobile player compatibility issues (i.e. the mobile app player is doing the right thing and rejecting levels it can't handle), I adjusted AVC, HEVC, and AV1 "just in case".

My comment was more along the lines of "there may be a bug in AMF where HEVC levels are not set" since AVC levels do seem to be automatically adjusted, despite also having a default value (4.2). I would suggest testing their sample apps, which notably do not all manually set levels. Since we do not currently set the AVC level in OBS Studio, that it adjusts on its own makes me think that AMF should be able to do this for HEVC and AV1. I could be wrong, but it seems odd.

@rhutsAMD
Copy link
Contributor

I confirmed that the HEVC level is incorrect before these changes. Will check with the changes at a later time.
I did also confirm that the AVC level does change based on the output settings (resolution+FPS) and seems to be mostly what I'd expect (I got 5.1 when I expected 5.2, but this is still different from the default 4.2), so something within AMF appears to be changing that as needed for AVC, but not for HEVC.

We tripped over this issue when testing HEVC playback in the TEB beta program, where the HEVC level was set to the maximum by default, so it's consistent with your findings re: HEVC. I don't recall at the moment what AVC was reporting back, but given that HEVC needed to be adjusted to solve mobile player compatibility issues (i.e. the mobile app player is doing the right thing and rejecting levels it can't handle), I adjusted AVC, HEVC, and AV1 "just in case".

My comment was more along the lines of "there may be a bug in AMF where HEVC levels are not set" since AVC levels do seem to be automatically adjusted, despite also having a default value (4.2). I would suggest testing their sample apps, which notably do not all manually set levels. Since we do not currently set the AVC level in OBS Studio, that it adjusts on its own makes me think that AMF should be able to do this for HEVC and AV1. I could be wrong, but it seems odd.

Hi @RytoEX , to clarify, AMF does currently adjust the profile levels based on the frame size however it does so by only increasing the profile level "upwards" and the current HEVC default profile level is set to the highest one. I have lowered the HEVC default profile level however this will only be take effect in a future public driver release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Improvement to existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants