Skip to content

fix(jellycompat): size encodes by the source instead of the client ceiling - #3342

Merged
Quick104 merged 4 commits into
mainfrom
fix/jellycompat-encode-target-source-bitrate
Nov 1, 2026
Merged

Quick104 merged 4 commits into
mainfrom
fix/jellycompat-encode-target-source-bitrate

Conversation

@CoffeeKnyte

@CoffeeKnyte CoffeeKnyte commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: changes #1246 C1 (Jellyfin Web's HEVC-over-HLS encode now targets the source's rate instead of the client's ceiling; the playback criterion is unchanged, but the case needs a re-test on a build with this change)

A Jellyfin client's MaxStreamingBitrate becomes the encoder's target, however small the source is. Jellyfin Web's 120 Mbps limit gives a 113.8 Mbps target for every file, so a 2.9 Mbps source encodes at about 10 Mbps and a 6 Mbps source at about 29 Mbps. That wastes bandwidth and storage for no gain in quality. The reported stream bitrate is wrong too, so a session looks like it needs far more throughput than it does.

This change stops an encode at the source's rate, converted for the output codec.

Approach

buildPlaybackSource now fits the target to the lower of the client's ceiling and the source's video rate. That rate is the video track's own when the probe found one, as in the native planner (playback.SourceVideoKbps), so audio doesn't count. A track rate above the file's, or under a quarter of it, falls back to the file's rate: a target that is too high costs only bandwidth, while a bogus low one would ruin the picture. The rate then goes through playback.EncodeEquivalentKbps, which replaces H264EquivalentKbps and takes the output codec, so an HEVC source still gets H.264 headroom.

The ceiling alone still picks the frame size, so a low-bitrate source is not scaled down, and only a ceiling the source really exceeds reports VideoBitrateNotSupported. An unknown source rate targets the ceiling as before. This is a deliberate difference from Jellyfin, recorded in docs/jellycompat-api.md.

This only changes the Jellyfin-compatible surface, so silo-apple and silo-android need no follow-up.

Validation

  • Focused tests: internal/jellycompat passes except TestAttachmentServesActualFontAndRejectsWrongRoute, which also fails on main in my environment. internal/playback passes in full against the FFmpeg shipped in the Silo image.
  • make lint-changed reports 0 issues. go vet on both packages and make verify-local-paths are clean.
  • End to end, at 69e70d7: built that commit and main as images and ran each with a fresh database and a synthetic library. A scripted client called PlaybackInfo, read the FFmpeg arguments from the server log, and checked the first four segments with ffprobe. The later commits change only how the source rate is chosen and were checked through the PlaybackInfo handler, not end to end.
  • Not run: make test-go and the rest of the full gate locally, and hardware encoders. The end-to-end runs used libx264. No real client app was used.

Evidence

Same request on the same synthetic file: a TS-only H.264 profile with direct play off. Sizes are the first four segments, about 8 s.

Source and ceiling Before (main 84cab92) After (this branch)
MPEG-2 2.9 Mbps, 120 Mbps -maxrate 113808k, 10.0 MB -maxrate 2898k, 3.7 MB
MPEG-2 23 Mbps, 120 Mbps -maxrate 113808k, 43.4 MB -maxrate 23057k, 27.1 MB
HEVC 5 Mbps, 120 Mbps -maxrate 113808k, 35.4 MB -maxrate 8490k, 9.8 MB
H.264 6 Mbps, 120 Mbps, copy off -maxrate 113808k, 29.4 MB -maxrate 5917k, 7.2 MB
MPEG-2 23 Mbps, 8.82 Mbps -maxrate 8187k, 10.0 MB -maxrate 8187k, 9.9 MB (ceiling still wins)

Surface and build: jellycompat PlaybackInfo and HLS output, images built from main 84cab92 and 69e70d7.

PlaybackInfo through the handler, same TS-only request on both builds, trimmed. The client-visible fields don't change, including TranscodeReasons; only the target the server stored for the encode does (server_target_kbps, not a response field).

Before (84cab92):

{"case":"MPEG-2 2.9 Mbps, 120 Mbps ceiling","Bitrate":2900000,"TranscodingContainer":"ts","TranscodeReasons":"VideoCodecNotSupported","server_target_kbps":113808}
{"case":"MPEG-2 7 Mbps file, 2.9 Mbps video, 120 Mbps ceiling","Bitrate":7000000,"TranscodingContainer":"ts","TranscodeReasons":"VideoCodecNotSupported","server_target_kbps":113808}
{"case":"HEVC 5 Mbps, 120 Mbps ceiling","Bitrate":5000000,"TranscodingContainer":"ts","TranscodeReasons":"VideoCodecNotSupported","server_target_kbps":113808}
{"case":"H.264 6 Mbps, 120 Mbps ceiling, copy off","Bitrate":6000000,"TranscodingContainer":"ts","TranscodeReasons":"VideoCodecNotSupported","server_target_kbps":113808}
{"case":"MPEG-2 23 Mbps, 8.82 Mbps ceiling","Bitrate":23000000,"TranscodingContainer":"ts","TranscodeReasons":"VideoBitrateNotSupported","server_target_kbps":8187}

After (847df19):

{"case":"MPEG-2 2.9 Mbps, 120 Mbps ceiling","Bitrate":2900000,"TranscodingContainer":"ts","TranscodeReasons":"VideoCodecNotSupported","server_target_kbps":2900}
{"case":"MPEG-2 7 Mbps file, 2.9 Mbps video, 120 Mbps ceiling","Bitrate":7000000,"TranscodingContainer":"ts","TranscodeReasons":"VideoCodecNotSupported","server_target_kbps":2900}
{"case":"HEVC 5 Mbps, 120 Mbps ceiling","Bitrate":5000000,"TranscodingContainer":"ts","TranscodeReasons":"VideoCodecNotSupported","server_target_kbps":8333}
{"case":"H.264 6 Mbps, 120 Mbps ceiling, copy off","Bitrate":6000000,"TranscodingContainer":"ts","TranscodeReasons":"VideoCodecNotSupported","server_target_kbps":6000}
{"case":"MPEG-2 23 Mbps, 8.82 Mbps ceiling","Bitrate":23000000,"TranscodingContainer":"ts","TranscodeReasons":"VideoBitrateNotSupported","server_target_kbps":8187}

Risks

  • A source whose stored bitrate is too low now gets a lower target than before. A client that wants a higher rate than the source can't force one.
  • A frame scaled down for a client's height or width limit still stops only at the full-frame source rate: a 4K 60 Mbps HEVC file for a 1080p-limited client at a 120 Mbps ceiling targets 100 Mbps, down from 113.8 Mbps. Applying the native planner's per-resolution ceiling here would also cut ordinary downscales below what main targets (4K H.264 at a 15 Mbps ceiling drops from 14 to 10 Mbps), so that belongs in its own change.
  • -b:v is 90% of the target, so a hardware H.264 encode of an H.264 source averages a little under the source's rate. Picture quality on hardware encoders hasn't been checked.

Checklist

  • Complete diff read by the author: pending, to be confirmed before review.
  • This pull request addresses one concern.
  • The added tests meet "Decide what to test": public jellycompat encode targets with real edge cases.
  • The Evidence section shows the server's output before and after.

AI Disclosure

  • Harness: Claude Code
  • Tool(s): Claude Code
  • Model(s): claude-opus-5-5
  • Involvement: AI-assisted. Claude Code wrote the code, tests and this description at the maintainer's direction.
  • Adversarial review: two read-only rounds by separate Claude Code agents (claude-opus-5-5) over the branch diff and its route, session-reload and fallback paths. They found implausible track rates and a track-less source codec (fixed here), inaccurate docs wording (fixed), and the frame-limited gap under Risks. A per-resolution ceiling added in response was reverted after the second round showed it lowered ordinary downscales.

Note

Fix jellycompat encode sizing to cap bitrate by source, not client ceiling

  • Encode targets are now capped at the source's codec-equivalent bitrate via new compatSourceEncodeKbps helper in handlers_playback.go, while output resolution still comes from the client/profile ceiling.
  • HEVC-to-H.264 fallback now carries the H.264 bitrate ceiling into the negotiated source, stored alongside the selected ceiling on PlaybackMediaSource (in-memory only).
  • compatEncodeTarget raises the target to a new shared 64 kbps minimum (compatMinEncodeKbps) when the source cap falls below it but the budget allows it; encodes below the minimum are not advertised.
  • compatTranscodeReasons now compares the source file bitrate against the stored encode ceiling, so a source-limited target no longer triggers VideoBitrateNotSupported on its own.
  • EncodeEquivalentKbps in prepare_file.go accepts a target codec instead of being H.264-only, and SourceVideoKbps in capabilities_v3.go centralizes track-then-file bitrate selection.
  • Behavioral Change: renegotiated playback info responses may advertise lower bitrates (source-limited) than before, and EncodeEquivalentKbps callers must pass a codec argument.

Macroscope summarized 847df19.

…iling

A client's MaxStreamingBitrate became the encoder's target and the reported
stream bitrate, so Jellyfin Web's 120 Mbps limit produced a 113.8 Mbps target
for a 27 Mbps file. Encodes now stop at the source's rate for the output codec.
The ceiling still picks the frame size and decides VideoBitrateNotSupported.
@silo-triage silo-triage Bot added the size/M 50–199 added + deleted lines (all files); change volume, not priority. label Nov 1, 2026
@silo-kody

silo-kody Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 473fcd80-9fd9-48f5-917b-3560d590e559
Commits

Reviewing files that changed from the base of the PR and between 69e70d7 and 847df19.

Files selected for processing: 7
  • docs/jellycompat-api.md
  • internal/jellycompat/deviceprofile.go
  • internal/jellycompat/handlers_playback.go
  • internal/jellycompat/playback_output_conditions_test.go
  • internal/jellycompat/playback_sessions.go
  • internal/playback/capabilities_v3.go
  • internal/playback/quality_ladder_test.go
Files skipped from review as they are similar to previous changes: 1
  • docs/jellycompat-api.md

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Walkthrough

Encode bitrate targets now account for source rate and target codec. Playback handling tracks encode ceilings separately from selected targets, applies a 64 Kbps minimum, and uses the ceiling for bitrate-based transcode reasons.

Changes: Jellyfin-compatible encode bitrate

Layer / File(s) Summary
Source bitrate and codec equivalence
internal/playback/capabilities_v3.go, internal/playback/prepare_file.go, internal/jellycompat/deviceprofile.go, internal/playback/quality_ladder_test.go
Playback helpers select and normalize the source video rate and calculate its equivalent rate for a target codec. Uncapped H.264 encode facts use the source-aware rate.
Playback target and ceiling handling
internal/jellycompat/handlers_playback.go, internal/jellycompat/playback_sessions.go
Target selection caps bitrate at the source-equivalent rate and tracks the available ceiling separately. The ceiling is carried through H.264 fallback and transcode-reason checks. Encode and subtitle-burning eligibility use a 64 Kbps minimum.
Behavior checks and documentation
internal/jellycompat/playback_output_conditions_test.go, internal/jellycompat/roku_profile_test.go, docs/jellycompat-api.md
Tests cover source-rate caps, resolution, transcode reasons, and the Roku HEVC profile case. The API documentation describes the source-rate cap and its relationship to frame size.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related issues

Possibly related PRs

  • Silo-Server/silo-server#2987: It changes the same playback negotiation path, including codec-specific limits and H.264 fallback behavior.
  • Silo-Server/silo-server#921: Its session telemetry records effective targets affected by this PR’s target-selection changes.
  • Silo-Server/silo-server#3341: It changes playback negotiation routes in the same handler where this PR changes encode ceilings and source-rate targets.

Suggested labels: impact: playback

Suggested reviewers: brady3035, tomislav

Merge Risk: Low · up to 847df

Audio-heavy files can receive higher encode targets than their video rate suggests, though client limits still apply. This is a bounded risk to note when merging.

Security architecture risk: Low · up to 847df

Playback bitrate selection changes within existing access checks and encoding limits. The reviewed paths show no new authority or data exposure. Recovery and deployment validation remains limited.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • observed — The changed target propagates to local and remote encoders for negotiated playback. The inspected dependency path changes the selected numeric bitrate, not the credentials, destination selection, or executor authority supplied by that path.

Trust Boundaries and Controls

  • observed — Client bitrate preferences remain constrained by existing request, server, and profile limits. Source-rate fitting narrows the encode target within that budget rather than granting the client a new resource-control channel.

Resilience and Maintainability Implications

  • observed — Finalized targets are stored after fallback. Existing in-memory locking and durable transactional replacement continue to serialize repeated negotiations and preserve started sessions; the new ceilings do not become shared mutable runtime authority.
Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly summarizes the main change: Jellycompat encode targets are sized by the source rate instead of only the client ceiling.
Description check Passed The description directly explains the source-rate cap, codec conversion, resolution behavior, tests, evidence, and known risks described by the changeset.
Docstring Coverage Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 10 functions across 8 files. (1 skipped: 1 unsupported.)

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@macroscopeapp

macroscopeapp Bot commented Nov 1, 2026

Copy link
Copy Markdown

PlaybackInfo reports a source-capped target bitrate (internal/jellycompat/handlers_playback.go), but the table shows only FFmpeg maxrate and HLS segment sizes; add before-and-after PlaybackInfo response excerpts for the same request showing the changed target bitrate.
TranscodingUrl's TranscodeReasons can also change (internal/jellycompat/handlers_playback.go), but no excerpt shows it; include its before-and-after value, including whether VideoBitrateNotSupported is reported.
Suggested fix: No diff — add trimmed response excerpts for both fields.
Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness.

Posted via Macroscope — Visible change evidence

@silo-triage silo-triage Bot added bug Something isn't working go Pull requests that update go code priority: P2 Limited scope, workaround exists, or polish labels Nov 1, 2026
@coderabbitai coderabbitai Bot added the impact: playback Can't play, stalls, or wrong media label Nov 1, 2026
@macroscopeapp

macroscopeapp Bot commented Nov 1, 2026

Copy link
Copy Markdown

The recorded #1246 C1 run exercises Jellyfin Web's HEVC-over-HLS transcode on 31fabc723 with #2934/#2935/#2970. This PR changes that path's encode target to the source-equivalent rate and changes bitrate-reason reporting, so the prior result no longer covers the same stream behavior. The PR must explain why the validated behavior changes; #1246 C1 needs re-validation after merge, especially the HEVC transcode case.

Suggested fix: No diff — re-run #1246 C1 against the merged build and record the result.

Automated check: Macroscope check run agent (gpt-6-luna). No validation was performed.

Posted via Macroscope — v1 validation impact

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

Nitpick comments: 1
internal/playback/quality_ladder_test.go: 1

573-574: Trivial · Functional Correctness · Quick win

Add an H.264-to-HEVC bitrate assertion.

The changed TestEncodeEquivalentKbps always uses transcodeCodecH264. The Jellycompat tests select HEVC output in some cases, but they either use a VP9 source or assert only codec selection, resolution, or fallback behavior. No test asserts the target bitrate for an H.264 source converted to HEVC under a generous ceiling. An incorrect H.264-to-HEVC conversion could therefore pass the current tests.


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 14fcab52-3537-417b-8be7-1b0721dd6995
Commits

Reviewing files that changed from the base of the PR and between 84cab92 and 69e70d7.

Files selected for processing: 8
  • docs/jellycompat-api.md
  • internal/jellycompat/deviceprofile.go
  • internal/jellycompat/handlers_playback.go
  • internal/jellycompat/playback_output_conditions_test.go
  • internal/jellycompat/playback_sessions.go
  • internal/jellycompat/roku_profile_test.go
  • internal/playback/prepare_file.go
  • internal/playback/quality_ladder_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

…e rate

The file rate includes audio, so a 2.9 Mbps video track with lossless audio
was still encoded near 7 Mbps. The native planner already reads the video
track first; both surfaces now share SourceVideoKbps.
A track rate above the file's, or under a quarter of it, falls back to the
file's rate. A target that is too high only costs bandwidth, while a bogus low
one would ruin the picture. The source codec now also reads the track first,
as the native planner does.
…e-target-source-bitrate

# Conflicts:
#	internal/jellycompat/handlers_playback.go
#	internal/jellycompat/playback_output_conditions_test.go
@silo-kody

silo-kody Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@macroscopeapp

macroscopeapp Bot commented Nov 1, 2026

Copy link
Copy Markdown

The user-visible HLS output for a file whose video-track rate is below its file rate (for example, 7 Mbps file / 2.9 Mbps video) lacks matching end-to-end evidence: the segment results are from 69e70d7, while the current-head excerpt shows only server_target_kbps, which is not a response field. Please include before-and-after FFmpeg targets and HLS segment sizes for that same request, with the after capture from 847df19.

Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness.

Posted via Macroscope — Visible change evidence

@Quick104
Quick104 merged commit e9c649b into main Nov 1, 2026
26 checks passed
@Quick104
Quick104 deleted the fix/jellycompat-encode-target-source-bitrate branch November 1, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code impact: playback Can't play, stalls, or wrong media priority: P2 Limited scope, workaround exists, or polish size/M 50–199 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants