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

Alpha support (YUVA) for hevc codec? #571

Closed
qwe7989199 opened this issue Mar 13, 2024 · 15 comments
Closed

Alpha support (YUVA) for hevc codec? #571

qwe7989199 opened this issue Mar 13, 2024 · 15 comments

Comments

@qwe7989199
Copy link

It seems that NVIDIA's official SDK explicitly supports hevc hardware encoding with an alpha channel in the following note.

Although the applications for this technique are very limited.
Could you consider adding this feature in future version of nvenc.

@rigaya
Copy link
Owner

rigaya commented Mar 14, 2024

I'm sorry, but I'm not sure what kind of use case we have for supporting alpha channel.

I know it would be useful in a video editor software, but NVEncC is mainly a transcoder. Also, does video players support this alpha channel in HEVC?

@qwe7989199
Copy link
Author

qwe7989199 commented Mar 14, 2024

In fact on the production side, HEVC with alpha channel has been limited to Apple devices in recent years. FFmpeg on macOS can call the videotoolbox hardware encoder for transparent HEVC output, and there is some application like this.

And by browsing this page with my Windows PC, I confirmed that at least Chrome can render HEVC with alpha correctly. This is the sample file.

Sorry for providing wrong information. It's playable only for macOS.

Probably the most significant point of adding this feature is to make windows also have a way to output transparent HEVC.

On the application side, I think the biggest significance may be that when used in conjunction with lossless YUV444, it is possible to store image sequences containing alpha channel in a more efficient way.
For example, compared to ProRes4444XQ (which is an Apple exclusive and can't be hw-decoded), HEVC is much smaller and more compatible with other platforms.

The above is based on my incomplete understanding.
This feature is "better to have", and in fact few people care.

@rigaya
Copy link
Owner

rigaya commented Mar 20, 2024

On Windows, ffmpeg seems to not being able to decode the alpha channel even with sw decode, and therefore its use will be greatly limited on a Windows environment.

I'll consider to add this feature in the future, but the functionality will be limited, for example, I think the filters will not be supported when using alpha channel encoding.

@EliotRagueneau
Copy link

I would need such type of support in my project, as I am embedding transparent videos within a web page, and to support Safari, the only encoder that seems to work so far with alpha channels is hevc_videotoolbox, but this one is only available on M1/2 chips, nd so cannot be encoded on a server

@rigaya
Copy link
Owner

rigaya commented Aug 6, 2024

NVEnc 7.61 adds experimental YUVA420 support for HEVC which could be enabled by “--output-csp yuva420”.

The input needs to have alpha channel to use this feature, and is limited to HEVC 8bit yuva420.

Unfortunately I were not test to the decode the output, ffmpeg on Windows or Linux does not seem to support HEVC alpha channel decoding.

I would appreciate it if you would have a test and check whether it can be decoded properly.

@qwe7989199
Copy link
Author

In fact, I'm stuck at the step of creating the valid input file.
My sample alpha channel hevc file was produced under macOS, and the browser under macOS renders the alpha channel correctly.

movie-hevc.mov

But nvencc doesn't consider it a correct video with alpha channel:

NVEncC64 -i "movie-hevc.mov" -c hevc --output-csp yuva420 -o nv_hevc_alpha.mp4
--------------------------------------------------------------------------------
nv_hevc_alpha.mp4
--------------------------------------------------------------------------------
Input file does not have alpha channel.

If only hevc files with alpha produced by nvidia's encoder can be recognized correctly, it becomes a chicken or egg problem.

At the moment, I don't think this solution is compatible with the macOS one.

@rigaya
Copy link
Owner

rigaya commented Aug 7, 2024

Unfortunately, both software decoder (by libavcodec) or hardware decoder of NVEnc does not support HEVC alpha channel.

Therefore, input files needs to be in other format, so I was testing using ProRes YUVA444 input file and huffyuv rgba input files.

I got sample file from UsingHEVCVideoWithAlpha.zip which can be downloaded from this url. There was a ProRes 4444 sample file "HEVC-Videos-With-Alpha-Exports\Resources\puppets_with_alpha_prores.mov".

It can be encoded by

NVEncC64 -i puppets_with_alpha_prores.mov -o test.mp4 -c hevc --output-csp yuva420

Also, I also tested other input format like below (only for test purpose).

ffmpeg -i puppets_with_alpha_prores.mov -c:v huffyuv -pix_fmt bgra test_huffyuv_bgra.mkv
NVEncC64 -i test_huffyuv_bgra.mkv -o test.mp4 -c hevc --output-csp yuva420

@qwe7989199
Copy link
Author

qwe7989199 commented Aug 8, 2024

As far as I know, Davinci Resolve (the free version) can handle hevc with alpha channel.

I tried with movie-hevc.mov above, and it was recognized as moive with alpha channel successfully, cuz we can set alpha mode in "Clip Attributes".

But I can not render correctly in windows (But correct in macOS).

check_alpha_in_win

When I import the nvencc output of hevc with alpha channel, Davinci Resolve shows only one alpha mode "Premultiply", which is consistent with the behavior of the video without an alpha channel.
only_one_mode

I'm not sure what condition the extra alpha mode in "Clip Attributes" is triggered, but it could be seen as one of the signs for success alpha or not.
As for how to get the correct rendering results on windows, I have no idea at the moment.

Here is the correct result by Davinci Resolve on macOS, alpha channel(the outline of phone) can be splitted and rendered stand alone.
截屏2024-08-08 10 42 34

@rigaya
Copy link
Owner

rigaya commented Aug 11, 2024

Thank you for sharing the information of Davinci Resolve.

I have improved muxer handling in NVEnc 7.62, although the file cannot be rendered (as I'm using Windows version), now other alpha modes can be shown.

image

Would you please have a try?

@qwe7989199
Copy link
Author

qwe7989199 commented Aug 12, 2024

Thank you for your great work.
I can be sure we are not far from success.

Now I am using a simpler validation process:

  1. Start an HTTP server on Windows that holds the nvenc encoded hevc with alpha (.mp4) file.
  2. Use an apple device, either safari on iphone or safari/chrome on macos, access the file address.
  3. Check the result of web browser playback.

In Chrome (macOS), we can see that the alpha channel is rendered but seems to be almost twice as wide as the content of the other channels.
image

In Safari (iOS), we can still recognize the presence of the alpha channel on the left side of the video, but it's still offset from the content, causing the effect to be unnormal.

793c5ead-57e1-41f8-92cf-904b6b6f24b2

@rigaya
Copy link
Owner

rigaya commented Aug 12, 2024

Thank you so much for sharing the validation process using the web server.

It really helps me much, as I can now check using my iPhone!

I was able to find out the bug of handling alpha channel which has caused the width error of alpha channel, NVEnc 7.63 shall fix this, and I think we are now able to get proper playback now.

Below is a check on Safari (iOS).

nvencc_hevc_alpha_iphone_capture_571

@qwe7989199
Copy link
Author

Browser validation on the macOS side also proved successful.
截屏2024-08-12 17 08 34

But Davinci Resolve still couldn't render a alpha channel.
Also I noticed that when using the official puppets_with_alpha_hevc.mov, the alpha mode in the Clip Attribute is recognized by default as "premultiplied", while the nvenc output is recognized as " straight".
I don't know if there is a correlation between this and being able to render correctly.
截屏2024-08-12 17 21 04

Actually it's enough for hevc to be rendered correctly on the web side as the final distributed format.
Thanks again for the great work!

@rigaya
Copy link
Owner

rigaya commented Aug 13, 2024

Thank you for your tests!

the alpha mode in the Clip Attribute is recognized by default as "premultiplied", while the nvenc output is recognized as " straight".

NVEnc 7.64 adds option to change alpha channel mode (--alpha-channel-mode). By adding --alpha-channel-mode premultiplied, I was able to get Davinci Resolve to recognized as "premultiplied" (but not rendered as I've checked on Windows). It also was shown properly on iOS Safari.

Also, NVEnc 7.64 fixed wrong alpha channel related headers.

Will you please have a try on macOS version?

Actually it's enough for hevc to be rendered correctly on the web side as the final distributed format.

Thank you, I think NVEnc 7.64 will be last try, as I'm not being able to come up of other points to be improved.

@qwe7989199
Copy link
Author

qwe7989199 commented Aug 13, 2024

Thank you for your hard work.

On Safari macOS, all things(whether it's premultiplied or straight alpha) behave normally.
But when it comes to Chrome (macOS), translucent values (0<alpha<1) behaves strangely.
You can see the "blurtest" part.
截屏2024-08-13 18 52 33

And I changed the border color of "blurtest" from red to black, Chrome also becames normal.
截屏2024-08-13 18 59 38

I don't know why this is happening. But I think it's a Chrome compatibility issue on macOS that we shouldn't be overly concerned about.

@rigaya
Copy link
Owner

rigaya commented Aug 13, 2024

Thank you for confirming, it's nice that we are now able to have proper playback in Safari on both macOS and iOS.

About the testing Chrome on macOS, it's strange that it behaves different depending on colors, but I also think it might be more of Chrome compatibility issue.

I'll close the issue as I think we have achieved to get HEVC + alpha layer using NVENC hw encode. Please reopen or create a new issue if you get any other problems.

Thanks again for your request, and many cooperation to create and refine this feature.

@rigaya rigaya closed this as completed Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants