Skip to content

fix(macos): drop max_ref_frames=1 for h264_videotoolbox#5188

Open
Nottlespike wants to merge 1 commit into
LizardByte:masterfrom
RESMP-DEV:fix/macos/h264-videotoolbox-drop-max-ref-frames
Open

fix(macos): drop max_ref_frames=1 for h264_videotoolbox#5188
Nottlespike wants to merge 1 commit into
LizardByte:masterfrom
RESMP-DEV:fix/macos/h264-videotoolbox-drop-max-ref-frames

Conversation

@Nottlespike
Copy link
Copy Markdown
Contributor

Description

VideoToolbox on Apple Silicon emits an IDR keyframe on every frame when ReferenceBufferCount=1 is set for H.264 — P-frames are never produced, and bandwidth inflates by roughly 3× while frame drops become severe under any sustained load. The hardware encoder's H.264 path interprets the minimum-ref-frames=1 constraint as "you have nothing to reference back to," so it cannot legally emit a P-frame and falls back to IDR-everything.

HEVC and AV1 on the same Apple Silicon hardware are not affected by this quirk — they continue to honor max_ref_frames=1 correctly and produce normal P-frames for reference-frame invalidation, so the constraint is correctly retained for those codecs.

This change removes {"max_ref_frames"s, 1} from the common options block of the h264_videotoolbox encoder entry only. The encoder still goes through Sunshine's max_ref_frames capability probe (config_max_ref_frames test), but without forcing the AVCodecContext option upfront — VideoToolbox is left to pick a sensible default reference count for H.264 (which it does correctly: 1-2 reference frames, producing real P-frames).

Validated on Apple Silicon (M4 Max): H.264 stream bitrate at 1080p60 drops from ~14 Mbps (all-IDR) to ~4.5 Mbps (normal P-frame mix) at identical quality, matching expected behavior for H.264 streaming. HEVC and AV1 paths are bitwise unchanged.

The diagnosis and original fix are from the Lumen fork (github.com/trollzem/Lumen); this PR brings the fix upstream with an inline comment explaining the rationale so future readers don't try to "re-add" the constraint thinking it was an oversight.

Screenshot

N/A — encoder option change.

Issues Fixed or Closed

Roadmap Issues

Type of Change

  • feat: New feature (non-breaking change which adds functionality)
  • fix: Bug fix (non-breaking change which fixes an issue)
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc.)
  • refactor: Code change that neither fixes a bug nor adds a feature
  • perf: Code change that improves performance
  • test: Adding missing tests or correcting existing tests
  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit
  • BREAKING CHANGE: Introduces a breaking change (can be combined with any type above)

Checklist

  • Code follows the style guidelines of this project
  • Code has been self-reviewed
  • Code has been commented, particularly in hard-to-understand areas
  • Code docstring/documentation-blocks for new or existing methods/components have been added or updated
  • Unit tests have been added or updated for any new or modified functionality

AI Usage

  • None: No AI tools were used in creating this PR
  • Light: AI provided minor assistance (formatting, simple suggestions)
  • Moderate: AI helped with code generation or debugging specific parts
  • Heavy: AI generated most or all of the code changes

Copilot AI review requested due to automatic review settings May 25, 2026 13:40

This comment was marked as low quality.

@Nottlespike

This comment was marked as resolved.

@Nottlespike

This comment was marked as resolved.

VideoToolbox on Apple Silicon produces all-IDR output when
ReferenceBufferCount=1 is set for H.264 — every frame becomes a
keyframe, P-frames are never produced, and bandwidth inflates roughly
3x while frame drops become severe. HEVC and AV1 on the same hardware
are unaffected by this quirk and continue to honor max_ref_frames=1
correctly for reference-frame invalidation.

Remove the option from h264_videotoolbox's common-options block only.
HEVC and AV1 retain max_ref_frames=1 as before.

Tracks LizardByte#5013. Diagnosis and original fix from the
Lumen fork (github.com/trollzem/Lumen).
@ReenigneArcher ReenigneArcher force-pushed the fix/macos/h264-videotoolbox-drop-max-ref-frames branch from e2c46eb to 1ce1c6c Compare May 26, 2026 00:39
@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Bundle Report

Bundle size has no change ✅

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
1 Duplicated Blocks on New Code (required ≤ 0)

See analysis details on SonarQube Cloud

@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 17.78%. Comparing base (d7e88da) to head (1ce1c6c).
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5188      +/-   ##
==========================================
- Coverage   17.79%   17.78%   -0.01%     
==========================================
  Files         111      111              
  Lines       24143    24142       -1     
  Branches    10687    10686       -1     
==========================================
- Hits         4296     4294       -2     
- Misses      15471    17380    +1909     
+ Partials     4376     2468    -1908     
Flag Coverage Δ
Archlinux 11.19% <ø> (ø)
FreeBSD-aarch64 ?
FreeBSD-amd64 13.33% <ø> (+<0.01%) ⬆️
Homebrew-ubuntu-22.04 13.53% <ø> (ø)
Linux-AppImage 12.11% <ø> (ø)
Windows-AMD64 14.85% <ø> (ø)
Windows-ARM64 13.20% <ø> (ø)
macOS-arm64 18.86% <ø> (+<0.01%) ⬆️
macOS-x86_64 18.34% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/video.cpp 32.33% <ø> (-0.10%) ⬇️

... and 30 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d7e88da...1ce1c6c. Read the comment docs.

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.

VideoToolbox H.264 encoding results in failure to generate an IDR frame

2 participants