Repository navigation
perf(recording): submit Windows screen frames as D3D11 NV12 textures #4491
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
Open
kyzer1023
wants to merge
4
commits into
CapSoftware:main
Choose a base branch
from
kyzer1023:perf/windows-fragmented-texture-encode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,259
−113
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8845019
perf(recording): submit Windows screen frames as D3D11 textures
kyzer1023 84c28a4
fix(recording): attribute D3D11 open crashes and drain before CPU fal…
kyzer1023 b6c3010
fix(recording): encode D3D11 textures as NV12
kyzer1023 0650913
fix(recording): size the D3D11 texture ring past AMF's input queue
kyzer1023 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| //! Which D3D11 `sw_format` FFmpeg 7.1 will actually encode. | ||
| //! | ||
| //! `hwcontext_d3d11va.c` `supported_formats` accepts NV12 and BGRA and rejects | ||
| //! RGBA (`Unsupported pixel format`). `amfenc.c` `format_map` accepts NV12 and | ||
| //! has `BGR0`, not `BGRA`, so a BGRA frames context fails with | ||
| //! `Format of input frames context (bgra) is not supported by AMF`. | ||
| //! `ff_nvenc_pix_fmts` includes `AV_PIX_FMT_D3D11` and maps NV12 through. | ||
| //! `qsvenc_h264.c` lists only NV12 and QSV, so QSV is a derived `AV_PIX_FMT_QSV` | ||
| //! context on those same NV12 frames. `mfenc.c` lists NV12 and YUV420P and is | ||
| //! not a D3D11 texture input. | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum D3d11SwFormat { | ||
| Nv12, | ||
| } | ||
|
|
||
| impl D3d11SwFormat { | ||
| pub fn name(self) -> &'static str { | ||
| match self { | ||
| Self::Nv12 => "nv12", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum D3d11TexturePath { | ||
| D3d11Nv12, | ||
| QsvFromD3d11Nv12, | ||
| } | ||
|
|
||
| pub fn d3d11_texture_path(encoder: &str) -> Option<D3d11TexturePath> { | ||
| match encoder { | ||
| "h264_amf" | "hevc_amf" | "av1_amf" | "h264_nvenc" | "hevc_nvenc" | "av1_nvenc" => { | ||
| Some(D3d11TexturePath::D3d11Nv12) | ||
| } | ||
| "h264_qsv" | "hevc_qsv" | "av1_qsv" => Some(D3d11TexturePath::QsvFromD3d11Nv12), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| pub fn d3d11_sw_format(encoder: &str) -> Option<D3d11SwFormat> { | ||
| d3d11_texture_path(encoder).map(|_| D3d11SwFormat::Nv12) | ||
| } | ||
|
|
||
| pub fn shared_d3d11_sw_format(encoders: &[&str]) -> Option<D3d11SwFormat> { | ||
| let mut chosen = None; | ||
| for encoder in encoders { | ||
| let format = d3d11_sw_format(encoder)?; | ||
| match chosen { | ||
| None => chosen = Some(format), | ||
| Some(existing) if existing == format => {} | ||
| Some(_) => return None, | ||
| } | ||
| } | ||
| chosen | ||
| } | ||
|
|
||
| pub fn capture_texel_feeds_nv12(format_name: &str) -> bool { | ||
| matches!(format_name, "rgba" | "bgra" | "nv12") | ||
| } | ||
|
|
||
| pub const H264_TEXTURE_ENCODERS: &[&str] = &["h264_amf", "h264_nvenc", "h264_qsv"]; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn amf_and_nvenc_share_nv12_and_reject_rgba_and_bgra() { | ||
| assert_eq!(D3d11SwFormat::Nv12.name(), "nv12"); | ||
| assert_eq!(d3d11_sw_format("h264_amf"), Some(D3d11SwFormat::Nv12)); | ||
| assert_eq!(d3d11_sw_format("hevc_amf"), Some(D3d11SwFormat::Nv12)); | ||
| assert_eq!(d3d11_sw_format("av1_amf"), Some(D3d11SwFormat::Nv12)); | ||
| assert_eq!( | ||
| d3d11_texture_path("h264_nvenc"), | ||
| Some(D3d11TexturePath::D3d11Nv12) | ||
| ); | ||
| assert_eq!( | ||
| d3d11_texture_path("hevc_nvenc"), | ||
| Some(D3d11TexturePath::D3d11Nv12) | ||
| ); | ||
| assert_eq!(d3d11_sw_format("rgba"), None); | ||
| assert_eq!(d3d11_sw_format("bgra"), None); | ||
| assert_eq!( | ||
| shared_d3d11_sw_format(H264_TEXTURE_ENCODERS), | ||
| Some(D3d11SwFormat::Nv12) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn qsv_derives_from_nv12_and_mf_and_libx264_stay_off_the_texture_path() { | ||
| assert_eq!( | ||
| d3d11_texture_path("h264_qsv"), | ||
| Some(D3d11TexturePath::QsvFromD3d11Nv12) | ||
| ); | ||
| assert_eq!(d3d11_sw_format("hevc_qsv"), Some(D3d11SwFormat::Nv12)); | ||
| assert_eq!(d3d11_texture_path("h264_mf"), None); | ||
| assert_eq!(d3d11_texture_path("hevc_mf"), None); | ||
| assert_eq!(d3d11_texture_path("libx264"), None); | ||
| assert_eq!(shared_d3d11_sw_format(&["h264_amf", "libx264"]), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rgba_and_bgra_capture_convert_to_nv12() { | ||
| assert!(capture_texel_feeds_nv12("rgba")); | ||
| assert!(capture_texel_feeds_nv12("bgra")); | ||
| assert!(capture_texel_feeds_nv12("nv12")); | ||
| assert!(!capture_texel_feeds_nv12("rgba64")); | ||
| assert!(!capture_texel_feeds_nv12("other")); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
abandon_d3d11_inputdrops the hardware encoder without first draining it. This also happens when texture conversion becomes unavailable, not just when the encoder fails. Valid frames still queued in a working encoder are then discarded.Try flushing those packets into
self.muxbefore replacing the encoder. If flushing fails, report the lost frames.Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in
84c28a4b9:abandon_d3d11_inputnow drains queued packets from the hardware encoder into the mux before falling back tolibx264(logging any flush error with the encoder name and continuing the CPU fallback).