Skip to content

fix(enc-mediafoundation): tag hardware encodes as BT.709 limited - #4484

Open
kyzer1023 wants to merge 8 commits into
CapSoftware:mainfrom
kyzer1023:fix/windows-mf-bt709-tags
Open

kyzer1023 wants to merge 8 commits into
CapSoftware:mainfrom
kyzer1023:fix/windows-mf-bt709-tags

Conversation

@kyzer1023

@kyzer1023 kyzer1023 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Why

Windows hardware H.264 and HEVC encodes convert BGRA through a D3D11 video processor whose color word ORs the nominal-range enum into the low bits. The output word is 0x3: video-processing usage, studio RGB range, BT.601 matrix, nominal range left undefined. The media types omit matrix, primaries, transfer, and nominal range, so the file is untagged and players treat the samples as BT.601. The FFmpeg software encoder writes BT.709 limited.

What changed

bt709_processor_color_words packs the output as usage 1, full RGB, BT.709, nominal 16-235 (21). A full-range RGB stream uses the same word with nominal 0-255 (37). VideoProcessor::new sets those words once. The H.264 and HEVC encoders first set the same output type main sets, with no color attributes. They then add MFVideoTransferMatrix_BT709, MFVideoPrimaries_BT709, MFVideoTransFunc_709, and MFNominalRange_16_235 one at a time on the output and input types, keeping each one the transform accepts. A rejected tag is left off and logged, and encoder init continues. The same color values are also tried through ICodecAPI, and a rejection there is ignored. An ignored Windows test encodes a 1280x720 patch of RGB (125, 62, 93) at Cap's recording bitrate (multiplier 0.15, 30 fps). It checks the decoded center against limited BT.709 YUV (83, 135, 154) within 2, with color_space BT.709 and color_range tv.

Scope

cap-enc-mediafoundation and its lockfile entry. The muxer is unchanged. Recording continues to skip AMD before this encoder.

Tradeoffs

If an MFT rejects a color attribute, that attribute is dropped and the encoder opens as it does on main. The file then carries whichever tags were accepted. HEVC in this crate gets the same tags. The app does not call that encoder today.

Blast Radius

Intel and NVIDIA hardware recordings change color to match the software encoder. The fragmented Instant branch (#2940) and the AMD MP4 branch (#3223) can drop their second, gated setter on rebase. Calling that setter again writes the same words.

Verification

On the red commit, cargo test -p cap-enc-mediafoundation --lib color::tests::processor_words_are_bt709_limited_out_and_match_the_input_range failed: output 0x3 fields (1, 1, 0, 0) against (1, 0, 1, 1). After the fix, cargo test -p cap-enc-mediafoundation --lib color::tests passed (5 tests, including the tag probe keeping accepted tags and dropping a rejected one). cargo clippy -p cap-enc-mediafoundation --all-targets -- -D warnings passed on Linux. cargo xwin check and cargo xwin clippy --all-targets -- -D warnings for x86_64-pc-windows-msvc passed.

On Windows 11 with an AMD Radeon 780M, cargo test -p cap-enc-mediafoundation --lib bt709_patch::color_patch_decodes_as_limited_bt709 -- --ignored --exact --nocapture passed at 230bdcc in 0.48 s.

mft_name=AMDh264Encoder
color_tags=output_accepted=yuv_matrix,primaries,transfer_function,nominal_range output_rejected=none input_accepted=yuv_matrix,primaries,transfer_function,nominal_range input_rejected=none codec_api_set=none codec_api_rejected=output_primaries,output_transfer,output_matrix,output_nominal_range,input_primaries,input_transfer,input_matrix,input_nominal_range

The decoded center pixel of the 1280x720 MP4 was YUV (83, 135, 154), an exact match. ffprobe reported:

codec_name=h264
color_range=tv
color_space=bt709
color_transfer=bt709
color_primaries=bt709

The AMD transform took all four tags on the media types and rejected the ICodecAPI color properties. The media types alone were enough for the tags to reach the file.

Not verified on NVIDIA or Intel Windows encoders: no such hardware was available.

RetriggerView in GreptileConfidence Score: 5/5

The changes appear safe to merge; all previous findings are addressed and no new actionable issue was found.

Summary

This PR makes Windows Media Foundation encodes use BT.709 limited output.

  • Windows Media Foundation encoders now produce BT.709 limited-range video.
  • Windows camera recordings use the color details reported by the camera.

All three previous findings are addressed. No new actionable issues were found in the changes since the previous review. No builds or runtime tests were run during this review.

Reviews (4) · Last reviewed commit: "fix: map Media Foundation nominal range ..." · Reviewed by Greptile

cursoragent and others added 5 commits November 23, 2026 15:34
The video processor still ORs the nominal-range enums into the low bits, so the output word is 0x3 (BT.601, nominal range undefined). The new test expects BT.709 limited output and a full-range stream for RGB input.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
Pack the video processor color words as BT.709 with a studio output range, and write the same matrix, primaries, transfer, and limited nominal range onto the H.264 and HEVC media types.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
Clippy on the Windows target rejects the one-element vec. The test prints the Media Foundation transform it opens, uses the software encoder when none is marked hardware, and keeps the mp4 when CAP_MF_BT709_MP4 is set.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
Commit the H.264 and HEVC output type with no color attributes, then add matrix, primaries, transfer, and nominal range one at a time. A tag SetOutputType rejects is left off. The same probe covers the input type. Codec API color properties are attempted after the types are set, and a rejection there does not fail encoder init.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
The AMD H.264 transform rejects the 64x64 output type before any color attribute is added. Use 1280x720 at 30 fps with the 0.15 bitrate multiplier from the Windows recording path, and RGBA input like screen capture. Print the output type fields when SetOutputType fails.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
@vercel

vercel Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@cursoragent is attempting to deploy a commit to the Cap Software Inc Team on Vercel.

A member of the Team first needs to authorize it.

@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai

Comment thread crates/enc-mediafoundation/src/color.rs Outdated
Comment on lines +20 to +21
output: USAGE_VIDEO_PROCESSING | MATRIX_BT709 | (NOMINAL_STUDIO << NOMINAL_SHIFT),
stream: USAGE_VIDEO_PROCESSING | MATRIX_BT709 | (nominal << NOMINAL_SHIFT),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Camera colors can shift

bt709_processor_color_words now declares both input and output as BT.709, including camera YUV input. WindowsCameraMuxer passes NV12 and YUY2 frames directly to this processor. If a camera supplies BT.601 YUV, these matching matrix settings do not request a BT.601-to-BT.709 conversion, but the encoder now labels those samples BT.709. Players then show the wrong colors.

Keep the source matrix separate from the output matrix and convert BT.601 camera input before tagging it BT.709.

Knowledge Base Used: Media encoding and muxing

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/enc-mediafoundation/src/color.rs
Line: 20-21

Comment:
**Camera colors can shift**

`bt709_processor_color_words` now declares both input and output as BT.709, including camera YUV input. `WindowsCameraMuxer` passes NV12 and YUY2 frames directly to this processor. If a camera supplies BT.601 YUV, these matching matrix settings do not request a BT.601-to-BT.709 conversion, but the encoder now labels those samples BT.709. Players then show the wrong colors.

Keep the source matrix separate from the output matrix and convert BT.601 camera input before tagging it BT.709.

**Knowledge Base Used:** [Media encoding and muxing](https://app.greptile.com/-/switch-tenant?tenant=cap&next=%2Fcustom-context%2Fknowledge-base%2Fcapsoftware%2Fcap%2F-%2Fdocs%2Fmedia-encoding-and-muxing.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 12b60fc. The camera pipeline reports a pixel format and a size rather than a matrix, so SD YUV (shorter side <= 576) now stays BT.601 on the processor input and BT.709 on the output, converting before tagging. RGB at every size (including the verified screen path) is unchanged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 54e47cbef: VideoFormat::new_mf now reads MF_MT_YUV_MATRIX and MF_MT_VIDEO_NOMINAL_RANGE from the Media Foundation camera media type and threads them through NativeCameraFrame into H264Encoder, HevcEncoder, and VideoProcessor as explicit source color, separate from the BT.709 limited output (and removed bt709_processor_color_words). An explicit BT.601 camera (including 720p/1080p) stays BT.601 on the processor input so the processor converts before BT.709 tagging; the size heuristic remains only when the attribute is unset/unknown, and screen RGB stays unchanged at output 21 / stream 37.

Comment thread crates/enc-mediafoundation/src/color.rs Outdated
Comment on lines +170 to +183
use windows::Win32::Media::MediaFoundation::MF_E_INVALIDMEDIATYPE;
let mut probe = TagProbe::new();
while let Some(attempt) = probe.next_attempt() {
match try_set(&attempt) {
Ok(()) => probe.observe(true),
Err(error) if error.code() == MF_E_INVALIDMEDIATYPE => probe.observe(false),
Err(error) => return Err(error),
}
}
if let Err(error) = try_set(probe.accepted())
&& error.code() != MF_E_INVALIDMEDIATYPE
{
return Err(error);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Tag retries lack direct tests

The rejection tests exercise TagProbe directly, not probe_color_tags. That leaves the new error-code checks, retries, and final call with accepted tags untested. The ignored GPU test cannot reliably cover rejection branches either. A change here could break encoder startup while the unit tests still pass.

Add callback-based tests for an accepted tag, a rejected tag followed by another accepted tag, an unexpected error, and the final call. These tests need no GPU.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/enc-mediafoundation/src/color.rs
Line: 170-183

Comment:
**Tag retries lack direct tests**

The rejection tests exercise `TagProbe` directly, not `probe_color_tags`. That leaves the new error-code checks, retries, and final call with accepted tags untested. The ignored GPU test cannot reliably cover rejection branches either. A change here could break encoder startup while the unit tests still pass.

Add callback-based tests for an accepted tag, a rejected tag followed by another accepted tag, an unexpected error, and the final call. These tests need no GPU.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 12b60fc. Added callback-based unit tests for probe_color_tags covering accepted tags, rejected-then-accepted tags, unexpected errors without a final call, and the final call receiving only the accepted tags.

kyzer1023 added a commit to kyzer1023/Cap that referenced this pull request Nov 23, 2026
Camera frames report a size and a pixel format, not a matrix. SD YUV keeps BT.601 on the processor input so the BT.709 output is a real conversion. RGB and HD YUV still use the same color words. The tag probe now has callback tests for an accepted tag, a rejection, an unexpected error, and the final accepted set.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai

kyzer1023 added a commit to kyzer1023/Cap that referenced this pull request Nov 23, 2026
kyzer1023 added a commit to kyzer1023/Cap that referenced this pull request Nov 23, 2026
Windows camera formats keep MF_MT_YUV_MATRIX and MF_MT_VIDEO_NOMINAL_RANGE and pass them into the Media Foundation encoder. An explicit BT.601 camera, including HD, stays BT.601 on the processor input so the BT.709 output is a conversion. Unset attributes still use the SD/HD fallback. Screen RGB still uses the full-range BT.709 words.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai

Comment thread crates/enc-mediafoundation/src/color.rs Outdated
Comment on lines +15 to +16
const MF_NOMINAL_RANGE_16_235: u32 = 1;
const MF_NOMINAL_RANGE_0_255: u32 = 2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Camera ranges are reversed

MF_NOMINAL_RANGE_16_235 and MF_NOMINAL_RANGE_0_255 have their values swapped. Media Foundation uses 1 for full range and 2 for limited range; D3D11 uses the opposite values.

Camera attributes reach from_media_type unchanged. A camera reporting limited range is therefore treated as full range, producing washed-out recordings. Full-range cameras get the opposite mistake and lose shadow and highlight detail. The Windows test media_foundation_enum_values_match_the_attribute_decoder also fails with this mapping.

Swap these two constants while keeping the separate D3D11 constants unchanged.

Suggested change
const MF_NOMINAL_RANGE_16_235: u32 = 1;
const MF_NOMINAL_RANGE_0_255: u32 = 2;
const MF_NOMINAL_RANGE_16_235: u32 = 2;
const MF_NOMINAL_RANGE_0_255: u32 = 1;
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/enc-mediafoundation/src/color.rs
Line: 15-16

Comment:
**Camera ranges are reversed**

`MF_NOMINAL_RANGE_16_235` and `MF_NOMINAL_RANGE_0_255` have their values swapped. Media Foundation uses `1` for full range and `2` for limited range; D3D11 uses the opposite values.

Camera attributes reach `from_media_type` unchanged. A camera reporting limited range is therefore treated as full range, producing washed-out recordings. Full-range cameras get the opposite mistake and lose shadow and highlight detail. The Windows test `media_foundation_enum_values_match_the_attribute_decoder` also fails with this mapping.

Swap these two constants while keeping the separate D3D11 constants unchanged.

```suggestion
const MF_NOMINAL_RANGE_16_235: u32 = 2;
const MF_NOMINAL_RANGE_0_255: u32 = 1;
```

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 150a72384: swapped MF_NOMINAL_RANGE_0_255 to 1 (full) and MF_NOMINAL_RANGE_16_235 to 2 (limited) to match windows 0.60.0 (MFNominalRange_0_255 = 1, MFNominalRange_16_235 = 2), leaving the separate D3D11 video processor nominal-range bits untouched (screen RGB stays output 21 / stream 37) and updating the SDK-value tests accordingly.

MFNominalRange_0_255 is 1 and MFNominalRange_16_235 is 2 in the
Windows SDK. The decoder constants had those two values swapped,
so a full-range camera was treated as limited and a limited camera
as full. The D3D11 processor bitfield stays 1 for 16-235 and 2 for
0-255.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai

kyzer1023 added a commit to kyzer1023/Cap that referenced this pull request Nov 23, 2026
kyzer1023 added a commit to kyzer1023/Cap that referenced this pull request Nov 23, 2026

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

2 participants