Skip to content

fix(macos): drop max_ref_frames=1 for h264_videotoolbox and enable PARALLEL_ENCODING#5200

Merged
ReenigneArcher merged 2 commits into
LizardByte:masterfrom
RESMP-DEV:fix/macos/h264-videotoolbox-drop-max-ref-frames
May 27, 2026
Merged

fix(macos): drop max_ref_frames=1 for h264_videotoolbox and enable PARALLEL_ENCODING#5200
ReenigneArcher merged 2 commits into
LizardByte:masterfrom
RESMP-DEV:fix/macos/h264-videotoolbox-drop-max-ref-frames

Conversation

@Nottlespike
Copy link
Copy Markdown
Contributor

@Nottlespike Nottlespike commented May 27, 2026

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

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).
Copilot AI review requested due to automatic review settings May 27, 2026 07:43
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Removes the max_ref_frames=1 setting for H.264 encoding on VideoToolbox to fix an issue where Apple Silicon produces all-IDR frames, causing severe bandwidth inflation and frame drops.

Changes:

  • Removes max_ref_frames from the H.264 VideoToolbox common options block.
  • Adds an explanatory comment documenting the rationale and referencing issue #5013.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e8318a536

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/video.cpp
Comment on lines +1137 to +1141
// Note: max_ref_frames is intentionally omitted for H.264 because
// VideoToolbox on Apple Silicon produces all-IDR output when
// ReferenceBufferCount=1 is set for H.264, causing massive bandwidth
// inflation (~3x) and frame drops. HEVC and AV1 are unaffected and
// retain max_ref_frames=1. See LizardByte/Sunshine#5013.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Also suppress ctx->refs for H.264 VideoToolbox

For macOS H.264 streams where the client requests numRefFrames=1, this still sets VideoToolbox ReferenceBufferCount=1 through the generic ctx->refs = config.numRefFrames path: validate_encoder() will mark H.264 REF_FRAMES_RESTRICT true after the max-ref probe succeeds, and FFmpeg's VideoToolbox encoder maps avctx->refs > 0 to kVTCompressionPropertyKey_ReferenceBufferCount (per the upstream videotoolboxenc refs patch). So removing only the max_ref_frames AVOption does not avoid the all-IDR Apple Silicon behavior described here; H.264 VideoToolbox needs to opt out of the REF_FRAMES_RESTRICT/ctx->refs path too.

Useful? React with 👍 / 👎.

Per andygrundman + ReenigneArcher review on the original PR thread:

> @andygrundman: "I can reproduce this bug and this is the right fix.
> @ReenigneArcher can you also enable PARALLEL_ENCODING for all VT
> encoders?"
>
> @ReenigneArcher: "@Nottlespike could you add @andygrundman's
> suggestion to this PR please?"

VideoToolbox encoders on macOS support multiple concurrent compression
sessions (the kernel media engine schedules them), so the
PARALLEL_ENCODING capability flag is correct to advertise. Sunshine
uses this flag to decide whether the probe path may concurrently
attempt multiple format/profile combinations against the same encoder.

The flag lives on the encoder_t struct itself, not on per-codec
options, so this single change covers all VT codecs the encoder
registers (h264, hevc, and prores when present via the experimental
opt-in in a separate PR).
@Nottlespike
Copy link
Copy Markdown
Contributor Author

Pushed @andygrundman's PARALLEL_ENCODING change as a follow-up commit (5620cd6a).

@andygrundman: "@ReenigneArcher can you also enable PARALLEL_ENCODING for all VT encoders?"
@ReenigneArcher: "@Nottlespike could you add @andygrundman's suggestion to this PR please?"

Carried forward from the original PR thread (#5188, before the closed/reopen cycle that re-numbered this to #5200). The flag lives on the encoder_t struct itself, so a single change covers h264_videotoolbox and hevc_videotoolbox — and any other VT encoder we register in future (e.g., the experimental ProRes path in #5202 inherits it via the same videotoolbox encoder_t).

Sorry for the delay on this — got caught up in the close/reopen shuffle and missed bringing this forward when I recreated the PRs.

@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 27, 2026

Bundle Report

Bundle size has no change ✅

@ReenigneArcher ReenigneArcher changed the title fix(macos): drop max_ref_frames=1 for h264_videotoolbox fix(macos): drop max_ref_frames=1 for h264_videotoolbox and enable PARALLEL_ENCODING May 27, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 17.82%. Comparing base (3c7952b) to head (5620cd6).
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5200      +/-   ##
==========================================
+ Coverage   17.78%   17.82%   +0.04%     
==========================================
  Files         111      111              
  Lines       24154    24153       -1     
  Branches    10688    10687       -1     
==========================================
+ Hits         4296     4306      +10     
- Misses      14682    16524    +1842     
+ Partials     5176     3323    -1853     
Flag Coverage Δ
Archlinux 11.18% <ø> (ø)
FreeBSD-aarch64 ?
FreeBSD-amd64 13.32% <ø> (+<0.01%) ⬆️
Homebrew-ubuntu-22.04 13.52% <ø> (ø)
Linux-AppImage 12.15% <ø> (+0.04%) ⬆️
Windows-AMD64 14.85% <ø> (ø)
Windows-ARM64 13.19% <ø> (-0.02%) ⬇️
macOS-arm64 18.86% <100.00%> (+<0.01%) ⬆️
macOS-x86_64 18.34% <100.00%> (+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.45% <100.00%> (+0.01%) ⬆️

... and 56 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 3c7952b...5620cd6. Read the comment docs.

@ReenigneArcher ReenigneArcher added the ai PR has signs of heavy ai usage (either indicated by user or assumed) label May 27, 2026
@ReenigneArcher ReenigneArcher added this to the stable release milestone May 27, 2026
@ReenigneArcher ReenigneArcher merged commit 3ee4144 into LizardByte:master May 27, 2026
60 of 61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai PR has signs of heavy ai usage (either indicated by user or assumed)

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

3 participants