Skip to content

feat(downloads): pair download presets with a resolution ladder - #1670

Merged
Quick104 merged 13 commits into
fix/hw-encoder-bitrate-ceilingfrom
feat/download-quality-ladder
Sep 29, 2026
Merged

Quick104 merged 13 commits into
fix/hw-encoder-bitrate-ceilingfrom
feat/download-quality-ladder

Conversation

@Quick104

@Quick104 Quick104 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: reaches #1168 C1 (server downloads); Silo-Server/silo-apple#319 C1 and Silo-Server/silo-android#329 C1 exercise it from the apps. None has a recorded result yet.

Stacked on #1659.

Download quality presets (20mbps … 1mbps) set only a bitrate. The output resolution came from the client's coarse max_resolution, so it depended on which app asked:

  • Android sends no device caps, so a 4K film downloaded at 1 Mbps was encoded at 2160p and 1 Mbps.
  • Apple sends max_resolution: "1080p" (a deliberate ceiling for older servers), so every converted download was 1080p, even at 20 Mbps on a 4K-capable iPhone.

A preset could also re-encode a source that was already smaller than the preset, and the encode used the real-time veryfast preset although nothing watches a download while it prepares.

This change pairs each preset with a resolution from one bitrate ladder, keeps the output within what the requesting device decodes, and reports each preset's resolution so the apps can label it.

Approach

Ladder. internal/playback/quality_ladder.go holds one table of H.264 floors at 30 fps or less:

Class Floor
2160p 20 Mbps
1080p 5 Mbps
720p 2 Mbps
540p 1.2 Mbps
480p below that

The floors follow Apple's HLS authoring ladder and Jellyfin's ResolutionNormalizer. Above 30 fps the budget shrinks by √(fps/30), so 60 fps drops a class. HEVC output counts at 0.6× the bitrate. The presets therefore reach 2160p (20 Mbps), 1080p (10 and 5 Mbps), 720p (2 Mbps) and 480p (1 Mbps), or 540p with HEVC. docs/architecture/quality-ladder.md records the invariants.

Per download (playback.ResolveDownloadTranscodeTarget):

  • The source is never enlarged. It is fitted into the class's 16:9 box, so a 3840x1600 film at 1080p becomes 1920x800, not 2592x1080.
  • The encode never targets more than the source's own bitrate, after converting between codecs.
  • The class steps down until the device's decoder can take the output. Strict-tier video_decode entries give the bound, preferring hardware decoders, as docs/downloads-api.md already promised. A decoder counts only if it takes the source's frame rate and lists the output profile. Its H.264 level list is also checked against the level libx264 would pick at the bitrate the encoder gets. As in playback, only a hardware decoder at the platform_attested tier is exempt from the profile and level lists. Each qualifying decoder is tried, and the one that reaches the tallest class wins, with the class earned from the preset or the decoder's bitrate or level limit, whichever is lower. If no H.264 decoder meets every limit, the listed ones' sizes still bound the output. Without these entries, max_resolution applies.
  • HEVC is used when playback.allow_hevc_encoding is on and the caps attest an 8-bit HEVC decoder, not bound to a level list, that reaches at least the class H.264 would. Otherwise the output is H.264.
  • A preset answers quality_unavailable when strict caps attest no decoder for a codec the server may encode, or none that takes 480p. Real Apple and Android caps always attest H.264.
  • 4K sources convert only when allow_4k_transcode is on, the same gate streaming uses. Without it, presets stop at 1080p. The account's max playback quality and a policy override's ceiling also cap the class, so a prepared file always passes the serve-time access check.

The artifact row stores the class label (1080p) for manifests and access checks. The encode derives the exact height from the file.

Serving the original. If a preset would leave an SDR source unchanged and the device's caps show it plays the file, the server serves the original (or a remux) instead of re-encoding. The row keeps the requested quality; effective_quality is original.

Encoder settings for downloads only, set in buildPrepareFileArgs, which HLS never calls:

  • x264 and QSV use medium; software HEVC uses fast.
  • libx264 chooses its own level instead of a fixed 4.1, which is wrong above 1080p30.
  • HEVC is tagged hvc1, which iOS requires to open HEVC in MP4.

The five per-backend scale helpers now share one parser that accepts any even NNNp height. Streaming only passes non-ladder labels equal to the source height, so for streaming this becomes a same-size scale.

Contract. GET /api/v2/capabilities/downloads gains quality_options (preset, bitrate_kbps, max_height), in the same order as quality_presets. max_height accounts for the 4K setting, the user's quality ceiling, and a policy override's transcode ceiling. It is additive; v1 is unchanged.

Older transcode nodes are not fenced: a node on the previous build would leave an exact box-fit height such as 800p unscaled. Nodes ship with the server image, so a deployment updates them together.

Validation

  • Go tests: internal/playback, internal/downloads (including DB-backed tests), internal/downloadprepare, internal/transcodenode, internal/apiv2, internal/jellycompat, internal/config: pass. internal/api/handlers shows the two known DB-fixture failures that also fail on main. New tests cover the ladder table, box fitting, device ceilings (size, frame rate, profile, H.264 level and bitrate limits, choosing among several decoders, platform-attested software decoders), HEVC selection, unavailable targets, the 4K setting, serving the original, capability heights with policy ceilings, and the download-only encoder arguments.
  • Other checks: make lint-changed clean; make verify-apiv2-openapi, verify-apiv2-web-types and verify-apiv2-contract pass (one additive, non-breaking change); web typecheck passes.
  • Isolated sandbox, driven from the updated Android and iOS apps (Silo-Server/silo-apple and Silo-Server/silo-android PRs to follow):
Request Source Result
Android, 2 Mbps 1080p H.264, 4.9 Mbps H.264 1280x720, 847 kbps video, plays offline
Android, 5 Mbps 1080p H.264, 2.6 Mbps MKV served as the original
No caps, 5 Mbps same film H.264 1080p, 1.6 Mbps video (ceiling 2.6 Mbps), level 4.0
iOS simulator, 20 Mbps 4K HEVC SDR, 3 Mbps H.264 1920x1080: the simulator attests only a 1080p H.264 decoder; plays offline
No caps, 2 Mbps 1920x800 H.264 720p class, fitted to 1276x532
  • Labels: both apps show "20 Mbps · up to 1080p" with 4K transcoding off, and "20 Mbps · up to 4K" and "1 Mbps · up to 540p" once 4K and HEVC are enabled.
  • After the review fixes, the same sandbox rebuilt from the top of the stack returned the same decisions for the rows above. A 10 Mbps request with HEVC-only caps and HEVC encoding on produced an HEVC Main file tagged hvc1, 1920x1080 8-bit, 660 kbps video under a 3039 kbps cap (libx265 on CPU). A 20 Mbps request with iPhone-style caps served the 3 Mbps 4K HEVC episode as the original.
  • Not covered: hardware HEVC download encodes; the sandbox host has no GPU.

Risks

  • Existing artifacts keep their files. A new request for the same file and preset can produce a different recipe and a new artifact. A tone-mapped or audio-downmix artifact still queued at upgrade can fail its fingerprint check once and is rebuilt on the next request.
  • Downloads now encode on slower presets. Software HEVC at fast is still slow for 4K on CPU-only servers.
  • Android episode downloads fail with 404 on current Android builds for an unrelated reason (the app sends the episode id as content_id); fix(downloads): register episode downloads under their series silo-android#407 fixes that.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.

AI Disclosure

  • Harness: Claude Code, running in T3 Code
  • Tool(s): Claude Code
  • Model(s): claude-opus-5-5
  • Involvement: Fully AI-generated at the maintainer's request. The agent wrote the change and ran the checks and the sandbox validation.
  • Adversarial review: A separate Claude Code subagent (claude-opus-5-5) reviewed this PR and the one below it. It checked fingerprints and parameter hashes for tone-mapped and downmixed artifacts, remote-node parity for the exact-height labels, the serve-the-original conditions, device ceilings against the caps each app sends, bitrate units, 4K gating and the API contract. Its only finding was the VideoToolbox rate control noted in the PR below; nothing in this PR needed changing. The sandbox run separately found a 2-pixel overshoot in box fitting (1282 wide in a 1280 box), which is fixed and covered by a test.

🤖 Generated with Claude Code

Note

Pair download quality presets with a shared resolution ladder

  • Adds a shared bitrate/resolution ladder in quality_ladder.go with codec efficiency factors (HEVC/VP9 at 60%, AV1 at 50% of H.264 bitrate) and frame-rate normalization. Ladder classes are 480p, 540p, 720p, 1080p, and 2160p.
  • Reworks download quality resolution in policy.go: a bitrate preset can now serve the source directly when it fits, otherwise it picks a transcode target capped by device decoder capabilities, H.264 level limits, source bitrate, server 4K/HEVC settings, and account/policy ceilings. New ResolveDownloadTranscodeTarget in prepare_file.go selects codec, ladder class, and bitrate from client capabilities.
  • Updates streaming quality planning in plan_v3.go to use source-aware ladder classes from an 80% bandwidth budget, with bounded H.264 fallback when HEVC is unsupported.
  • Exposes per-preset bitrate and max height in the download capability API via quality_options, defined in openapi.json and typed in schema.ts.
  • Behavioral Change: download capability responses now require quality_options; streaming H.264 still pins level 4.1 while prepared-file encodes omit it; presets no longer advertise 4K output when the server 4K-transcode setting is disabled; download creation rows now store the requested preset for source-served bitrate requests; scale filters accept arbitrary even heights 2–4320.

Macroscope summarized 3962557.

@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: d055555d-e76d-41e7-a61d-1d50e19994ae

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-29T16:20:07.567310Z 0ef6e42 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Copy link
Copy Markdown

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: d69004f111

ℹ️ 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 internal/playback/prepare_file.go Resolved
Comment thread internal/playback/quality_ladder.go Resolved
@Quick104
Quick104 force-pushed the feat/download-quality-ladder branch from d69004f to c480d51 Compare September 29, 2026 02:15

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

Copy link
Copy Markdown

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: c480d51470

ℹ️ 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 internal/playback/prepare_file.go Resolved

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

Copy link
Copy Markdown

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: e243f9cf93

ℹ️ 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 internal/playback/prepare_file.go Resolved
Comment thread internal/playback/prepare_file.go Resolved
Comment thread internal/playback/prepare_file.go Resolved
@Quick104
Quick104 force-pushed the feat/download-quality-ladder branch from e243f9c to 5d986b6 Compare September 29, 2026 03:27

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

Copy link
Copy Markdown

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: 5d986b6dfd

ℹ️ 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 internal/playback/prepare_file.go Outdated
Comment on lines +90 to +92
if source.BitrateKbps > 0 {
sourceEquivalent := int(float64(source.BitrateKbps) * codecEfficiency(codec) / codecEfficiency(source.VideoCodec))
target.TargetBitrateKbps = min(capKbps, max(sourceEquivalent, 1))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Select the ladder after capping to the source bitrate

When a high-resolution source's codec-adjusted bitrate is below the requested preset, the resolution class is chosen from the larger preset before these lines lower the actual encode cap. For example, a 4K H.264 source at 8 Mbps requested as 20mbps remains 2160p but is encoded with an 8 Mbps ceiling, even though the new ladder maps that effective budget to 1080p. Apply the source-equivalent cap before selecting the class, as is already done for decoder bitrate limits, so the output is not underprovisioned for its resolution.

Useful? React with 👍 / 👎.

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.

Leaving the class tied to the preset and the device on purpose; the source-bitrate bound only caps the encode's bitrate. Applying it to the class would downscale sources that already fit: a 1080p H.264 source at 2.6 Mbps requested at 5mbps would drop to the 720p class, and because it no longer "fits", the server would stop serving that original to a device that plays it and transcode it to 720p instead. When a transcode does happen at the source's own class, re-encoding at the source's bitrate keeps its resolution rather than trading it away; the ladder decides how far down to go only when the preset's budget, not the source's, is the limit.

@Quick104
Quick104 force-pushed the feat/download-quality-ladder branch from 5d986b6 to 50eaf90 Compare September 29, 2026 03:48

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

Copy link
Copy Markdown

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: 50eaf90d7c

ℹ️ 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 internal/playback/prepare_file.go Resolved
Comment thread internal/playback/prepare_file.go Resolved
@Quick104
Quick104 force-pushed the feat/download-quality-ladder branch from 50eaf90 to 6983d96 Compare September 29, 2026 04:00

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

Copy link
Copy Markdown

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: 6983d962a9

ℹ️ 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 internal/downloads/service.go Resolved
Comment thread internal/playback/prepare_file.go Resolved
@Quick104
Quick104 force-pushed the feat/download-quality-ladder branch from 6983d96 to e0f46c2 Compare September 29, 2026 04:23

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

Copy link
Copy Markdown

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: e0f46c274e

ℹ️ 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 internal/playback/prepare_file.go Resolved
Comment thread internal/playback/prepare_file.go Resolved
Comment thread internal/downloads/policy.go Resolved
@Quick104
Quick104 force-pushed the feat/download-quality-ladder branch from e0f46c2 to d4068da Compare September 29, 2026 04:45

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

Copy link
Copy Markdown

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: b41cee6d28

ℹ️ 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 on lines +181 to +182
if !cfg.Allow4KTranscode && (maxHeight == 0 || maxHeight > nonUHDMaxHeight) {
maxHeight = nonUHDMaxHeight

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 Remove the stale 4K artifact rejection

When allow_4k_transcode is false and the source is 2160p, this now resolves the preset to a 1080p transcode, but ArtifactManager.Ensure immediately passes that target to resolveToneMapTarget, which still rejects every non-copy 4K source at internal/downloads/artifacts.go:389-401. Consequently the default-off setting makes every advertised bitrate preset for a 4K title fail with quality_unavailable, rather than producing the 1080p artifact expected by this new ladder behavior; update the artifact gate to account for the capped output.

Useful? React with 👍 / 👎.

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.

This is intended, not stale. allow_4k_transcode keeps its existing meaning for downloads: it decides whether a 4K source may be converted at all, the same gate HLS streaming applies (TerminalMessage4KTranscodeDisabledV3). That was the maintainer's decision for this change: when 4K transcoding is on, 4K downloads are allowed; when it is off, they are not. docs/downloads-api.md states it: with the setting off, presets stop at 1080p and every preset of a 4K title answers quality_unavailable. main behaves the same way. The 1080p cap in downloadTranscodeTarget covers the other sources: it keeps a 1440p source (or a served original) within the advertised max_height. A 4K title stays downloadable as original.

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

Copy link
Copy Markdown

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: ec3691094f

ℹ️ 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".

TargetCodecVideo: a.CodecVideo,
TargetCodecAudio: a.CodecAudio,
TargetResolution: a.Resolution,
TargetResolution: playback.DownloadScaleResolution(file, a.Resolution),

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 Fence exact-height recipes from older transcode nodes

During a rolling upgrade, this can send an exact height such as 800p to a node running the previous build, whose scale helpers recognize only the fixed resolution labels and therefore leave a 4K cinema-aspect source unscaled. The old node still attests the same request fingerprint because it hashes the 800p label rather than the produced dimensions, so the server accepts and persists an artifact that can exceed the requesting decoder's bounds; route these recipes only to nodes advertising support for arbitrary heights, or version the recipe so an old node cannot attest it.

AGENTS.md reference: AGENTS.md:L13-L18

Useful? React with 👍 / 👎.

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.

Leaving this unfenced on purpose, as the PR description says under Approach. Transcode nodes ship in the same image as the server, and a deployment updates them together. Silo is pre-1.0 and takes one coordinated switchover instead of carrying mixed-version fences. The earlier version of this concern on #1659 got the same answer. The fence would also cost a status family and a node capability version for a window that a normal upgrade does not have.

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

Copy link
Copy Markdown

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: 0140fd192b

ℹ️ 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 internal/downloads/policy.go Resolved
@Quick104
Quick104 force-pushed the fix/hw-encoder-bitrate-ceiling branch from 0372b65 to 95ea643 Compare September 29, 2026 13:35
@Quick104
Quick104 force-pushed the feat/download-quality-ladder branch from 4a51793 to 776f8ee Compare September 29, 2026 13:35

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

Copy link
Copy Markdown

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: 776f8ee1a0

ℹ️ 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 internal/playback/prepare_file.go Resolved
Quick104 and others added 5 commits September 29, 2026 14:00
A download preset set only a bitrate. The output resolution came from the
client's coarse max_resolution, so a 4K source downloaded at 1 Mbps by a
client without caps was encoded at 2160p, and Apple's deliberate "1080p"
ceiling capped every converted download at 1080p.

Each preset now earns a resolution from one shared ladder (2160p at
20 Mbps, 1080p at 10 and 5, 720p at 2, 480p at 1 Mbps at <=30 fps; higher
frame rates drop a class, HEVC stretches the budget). The encode never
enlarges the source, box-fits cinema aspects, never exceeds the source's
own bitrate, and steps down until the device's attested decoder takes it.
HEVC is used when the server allows HEVC encoding and the caps attest an
8-bit HEVC decoder. When a preset would leave an SDR source unchanged and
the device plays it, the source is served and the row still records the
requested preset.

Download encodes use a slower preset, let libx264 choose the level, and
tag HEVC hvc1 so Apple players open it. The downloads capability adds
quality_options with each preset's bitrate and maximum height for labels.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The download size choice read only a decoder's width and height. A
decoder that cannot take the source's frame rate, or at the exact tier
does not list the High profile the H.264 encode produces, is now skipped;
a level-bound HEVC decoder keeps the encode on H.264, as HLS does; and
the target bitrate stays within the chosen decoder's maximum.

The ladder's comment and architecture note no longer claim that streaming
and jellycompat read the table; they do not until they move onto it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
At the exact tier an H.264 decoder's level list now bounds the output:
each size is checked against the level libx264 would pick for it
(including its doubled rate buffer), and the level also caps the
bitrate. A decoder bitrate limit now lowers the budget the ladder class
is chosen from, so a 3 Mbps decoder no longer gets a 1080p class.

When frame-rate or profile checks leave no H.264 decoder, the largest
decoder's size still bounds the output instead of leaving it unbounded.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ested lists

Pick the strict-tier decoder whose limits reach the tallest ladder class
instead of the one with the largest frame, so a 4K decoder capped at a low
bitrate no longer drags a download below what a second decoder plays.
Check profile and level lists on platform-attested software decoders, as
playback does; only platform-attested hardware decoders are exempt. When
strict caps attest no decoder for any codec the server may encode, answer
quality_unavailable rather than producing H.264 the device never listed.

Label quality_options with the transcode ceiling a policy override sets,
matching what Resolve produces.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…the encode bitrate

With 4K transcoding off, quality_options advertised presets up to 1080p,
but a 20 Mbps request could still serve a 4K original or keep a 1440p
source at its size. Cap the preset class at 1080p in that case, which
also matches the documented quality_unavailable answer for 4K titles.

Check a decoder's H.264 level at the bitrate the encoder will get, which
never exceeds the source's own, so a 3 Mbps 1080p source is not scaled
down for a level-4.0 decoder. A decoder smaller than the 480p class no
longer receives 480p output; with no decoder that takes a class, the
preset is unavailable.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Quick104 and others added 5 commits September 29, 2026 14:00
FitLadderBox returned zeros whenever the width was unknown, so a caller
fell back to the class box and could enlarge a short source. Keep such a
source's own height when it fits the class and the class height when it
is taller, leaving the width unknown.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A download encode that keeps the source frame passed no scale, so an odd
height or width reached a 4:2:0 encoder unchanged and could fail. Scale
such a frame to the even height at or below it; scale=-2 evens the width.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The scale parser rejected heights below 144, so a very wide source fitted
into a small class (1280x100 into the 480p box is 844x66) was left
unscaled while its recipe claimed the fitted size. Accept every even
height up to 4320.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
quality_options already capped each preset at the account's max playback
quality, but Resolve applied only a policy override's ceiling, so with 4K
transcoding on a 1080p account could prepare a 2160p artifact that the
serve-time access check then refused. Apply the narrower of the two.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ites

A source that fits a class keeps its size, but an odd frame is evened by
scale=-2, so a 1917x1080 source was checked against a 1917-wide decoder
and then encoded 1918 wide. Check each class at the frame the encoder
actually produces.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Quick104
Quick104 force-pushed the feat/download-quality-ladder branch from 776f8ee to e4197a0 Compare September 29, 2026 14:05

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

Copy link
Copy Markdown

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: e4197a02b1

ℹ️ 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".

if decoders := strictDownloadDecoders(caps, codec, frameRate, true); len(decoders) > 0 || codec != transcodeCodecH264 {
return decoders
}
return strictDownloadDecoders(caps, codec, frameRate, false)

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 Do not reuse decoders that failed strict checks

When every exact-tier H.264 decoder is rejected for frame rate or profile, this fallback calls strictDownloadDecoders with qualify=false and selects the same incompatible decoder based only on its size/bitrate bounds. The encode still preserves the source frame rate and emits High profile, so a 60 fps source with only a 30 fps decoder, or a device attesting only Baseline, receives an artifact outside its authoritative capabilities. Fresh evidence in this revision is TestResolveDownloadTranscodeTargetHonorsDecoderLimits, which now expects the 30 fps-only decoder to produce a 720p artifact for a 60 fps source rather than rejecting the target.

Useful? React with 👍 / 👎.

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.

Keeping the relaxed fallback, on purpose. video_decode entries carry one max_frame_rate per decoder, and Android measures it at the decoder's largest supported size. MediaCodecCapabilitiesProbe takes getSupportedFrameRatesFor(largest) and the performance point at that size, so a phone whose H.264 decoder does 1080p60 but 4K30 reports 3840x2160 @ 30. Treating that as an absolute frame-rate limit would refuse every preset of a 60 fps source on such devices, although they play the 1080p60 file the ladder produces (above 30 fps the class already drops, per LadderClassForBitrate). So when every listed H.264 decoder fails only the frame-rate or profile qualification, their size and bitrate limits still bound the output. That behavior is documented in downloadDecodersFor and docs/downloads-api.md, and pinned by the test you cite. A device with no usable H.264 entry at all, or none that takes 480p, still answers quality_unavailable. Making this exact would need per-size frame-rate facts in video_decode (a contract change), not a stricter reading of the current field.

…#1671)

* feat(playback): use the shared ladder for automatic streaming quality

Automatic streaming quality, the plain resolution preferences, bandwidth
caps and jellycompat each had their own bitrate-to-resolution table, and
they disagreed with each other and with download presets. All of them
now read the shared ladder: 80% of the bandwidth budget earns a class for
the source's frame rate, the source is fit into that class's box so a
scope film at 1080p streams at 1920x800, and a 540p class sits between
720p and 480p. The encode never targets more than that 80% budget, and a
source that fits the class but exceeds the estimate's budget is re-encoded
at its own size instead of sent as-is. Explicit menu rungs keep their own
table.

An original-quality route that still needs a video transcode labelled a
3840x1600 source "1080p", which scaled it to 2592x1080 while the plan
reported 3840x1600. It now keeps the exact source height.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): bound auto transcodes by the source and jellycompat by video budget

An automatic transcode that only scales a low-bitrate source down could
target the class bitrate even when the source used fewer bits; it now
stays within the source's own bitrate, counted as H.264. jellycompat
chose its class from the client's whole bitrate ceiling while encoding
at the video share after audio and mux overhead; the class now follows
that video budget.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): keep source-size labels even and budget unknown source bitrates

A route that keeps the source frame but still encodes labeled an odd
source height such as 1081p, which the scale helpers reject, leaving an
odd frame for a 4:2:0 encoder. Round the label down to the even height.

Automatic quality sent a source of unknown bitrate as-is whenever its
class fit the estimate, so a 7 Mbps estimate could stream a 1080p file of
any bitrate. Count an unknown bitrate as the full bitrate of the class it
would be sent at, so it goes as-is only when the budget covers that.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): keep cropped sources in the class they earned

Automatic quality bounded the target by the source height before snapping
to the ladder, so a 1920x800 film under a 1080p estimate snapped to 720p:
over its budget it was scaled to about 1276x532 at 2 Mbps instead of being
re-encoded at its own size, and with an unknown bitrate it was sized as a
2 Mbps stream. A target that keeps the source height now snaps into the
class the source's frame belongs to by both dimensions, within the class
the preference, device, bandwidth or cap allows.

jellycompat no longer upscales a track known only by its height.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): re-encode an odd source frame at an even size

An automatic transcode that keeps the source frame labeled an odd height
such as 1079p, which the scale helpers reject, so the odd frame reached a
4:2:0 encoder. Round the fitted height down to even and report the width
scale=-2 gives it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): bound HEVC stream output by the source's HEVC bitrate

The quality policy caps a scaled encode at the source's bitrate counted
as H.264, and the planner can choose HEVC afterwards without revisiting
it, so a 3 Mbps HEVC source converted to 1080p HEVC could target 5 Mbps.
Re-cap the bitrate at the source's bits counted as HEVC when HEVC output
is chosen.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): check the HEVC target the planner encodes and keep H.264's on fallback

The planner checked the HEVC decoder against the H.264-equivalent bitrate
before capping it for HEVC, so a decoder limited to 4 Mbps refused a
3 Mbps HEVC encode and an HEVC-only delivery had no route. Cap first and
check that candidate. A failed HEVC attempt now falls back to H.264 at
the bitrate the policy chose for H.264 instead of the HEVC cap.

Routes that keep the source size also report the even frame they encode
(1918x1080 for a 1920x1081 source), so the HEVC decoder check sees the
real output.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): encode an unknown-bitrate source under a bandwidth cap

With the shared ladder a 7 Mbps cap earns the 1080p class, so a 1080p
source of unknown bitrate passed the cap check and was sent unchanged.
The cap is a hard ceiling: treat an unknown video bitrate as over it, as
the server bitrate cap already does, so the source is re-encoded within
the cap.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): reserve the server-cap audio share before checking HEVC

The planner checked the HEVC decoder against the video bitrate before a
server bitrate cap reserved its AAC and mux share, so an HEVC-only client
whose decoder took the capped rate was refused over the uncapped one and
had no route. Reserve the share first; it does not depend on the video
codec.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* docs(playback): describe plain-label sizing and unknown bitrates

The protocol doc still promised height-only behavior for plain resolution
labels, which now fit the source into the class box like auto, and said a
cap re-encodes only a source known to exceed it. State both rules as the
planner applies them.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): cap every HEVC encode at the source's HEVC-equivalent rate

A route that keeps the source size but still re-encodes it (a codec the
client cannot play, or a subtitle burn) skipped the HEVC cap, so an 8 Mbps
H.264 source was checked as an 8 Mbps HEVC output and an HEVC-only client
with a 6 Mbps decoder had no route. Cap every HEVC encode; the cap only
lowers the bitrate when HEVC is the more efficient codec.

Document that a source whose height fits the class is sent as-is even
when it is wider than the 16:9 box.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): keep H.264 stream targets within the attested decoder bitrate

HLS treats H.264 as the universal output and never checked its bitrate
against the client's decoder. The shared ladder reaches 1080p at lower
estimates, so a 7 Mbps estimate now targets 5.6 Mbps where the previous
ladder chose 2 Mbps, past an H.264 decoder that attests a 4 Mbps limit.
Lower an H.264 target to the limit of an attested decoder that takes the
output size; HEVC output keeps its own decoder check.

Qualify the ladder invariants for the streaming preservation exception
and the H.264 bound.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): step H.264 stream targets down to an attested decoder's size

A client whose attested H.264 decoders stop at 1280x720 received a 1080p
H.264 target at a 7 Mbps estimate, where the previous ladder chose 720p.
Size the H.264 candidate to the tallest class an attested H.264 decoder
takes before bounding its bitrate; HEVC output keeps its own class, and
the H.264 fallback after a failed HEVC attempt uses the H.264 size.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): step H.264 targets down until the fitted size fits a decoder

Snapping an attested H.264 decoder's limits to a ladder box let a 1280x962
decoder count as 1080p, so a 1920x1080 target passed although it is wider
than the decoder. Step down class by class until the fitted size itself
fits an attested decoder, stopping at the smallest class since H.264 stays
the universal HLS output.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): fit same-size H.264 conversions to an attested decoder

A source whose size fits the estimate but whose codec the client cannot
play is converted at its own size, and that conversion skipped the H.264
decoder fit, so an H.264-only client with a 720p decoder received a 1080p
H.264 stream. Fit any H.264 encode the attested decoders cannot take,
scaled or not.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* docs(playback): note that H.264 conversions are fitted to the decoder too

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): match H.264 decoders by frame rate as well as size

The decoder fit compared only width and height, so a 60 fps source was
sent at 1080p to a client whose 1080p H.264 decoder stops at 30 fps and
whose 60 fps decoder stops at 720p. The encode keeps the source frame
rate, so a decoder must take it too.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): fit streamed H.264 to the frame the encoder writes

The H.264 decoder fit compared box-fit sizes that scale=-2 later evens,
so an odd-width source could pass a decoder limit it then exceeded by a
pixel. Check the encoded frame, sharing encodedFrame with downloads and
the source-size routes. The ladder doc now states the smallest-class
floor for streaming and the unavailable answer for downloads.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>

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

Copy link
Copy Markdown

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: afdf2e1768

ℹ️ 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 on lines +1209 to +1210
if limit > 0 && (quality.BitrateKbps <= 0 || quality.BitrateKbps > limit) {
quality.BitrateKbps = limit

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Apply decoder bitrate caps before choosing the H.264 class

Fresh evidence beyond the fixed download path is this newly added streaming helper: it chooses the output dimensions without considering MaxBitrateKbps, then caps the bitrate afterward. For example, an exact 1080p H.264 decoder limited to 3 Mbps receives 1080p at 3 Mbps even though the shared ladder maps that budget to 720p. Feed each decoder's effective bitrate limit into class selection, ideally by sharing the decoder-ranking logic already used for downloads.

AGENTS.md reference: AGENTS.md:L73-L79

Useful? React with 👍 / 👎.

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 3962557. In h264BoundedQualityV3 a decoder now takes a class only when its max_bitrate_kbps earns that class through LadderClassForBitrate, the rule downloadLadderClass uses. Your example is a test case: a 1080p decoder limited to 3 Mbps gets 720p at 2 Mbps. A second case pairs a 4K decoder capped at 1 Mbps with a 1080p decoder at 20 Mbps and keeps 1080p. The bitrate check comes from the download path, but the download ranking itself is not reused: it checks H.264 levels against the level libx264 picks, while the HLS recipe pins level 4.1. The existing bitrate-clamp test now uses a 5 Mbps limit, which still earns 1080p.

Comment thread internal/playback/plan_v3.go Outdated
Comment on lines +1233 to +1235
if len(decoders) > 0 {
return decoders
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Fall back to a usable software H.264 decoder

When strict caps contain any hardware H.264 entry, this returns the hardware set before checking whether those entries can take the planned frame rate or dimensions, excluding opted-in software decoders entirely. For a 60 fps transcode with a 30 fps hardware decoder and a 60 fps software decoder, h264BoundedQualityV3 walks down to 480p and still has no matching hardware decoder instead of retaining the class the software decoder supports. Prefer hardware only when at least one hardware entry is usable, as the download decoder selection already does.

AGENTS.md reference: AGENTS.md:L73-L79

Useful? React with 👍 / 👎.

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 3962557. attestedH264DecodersV3 now returns the hardware H.264 decoders only when one of them takes the source frame rate. Otherwise it returns the opted-in software decoders that do. If neither kind takes it, the planner falls back to the old hardware-first list. Test: a 60 fps source with a 1080p30 hardware decoder and an opted-in 1080p60 software decoder now streams at 1080p, and without the opt-in it still drops to 480p.

Quick104 and others added 2 commits September 29, 2026 12:14
… rate

The streaming H.264 decoder fit chose the output size before looking at a
decoder's bitrate limit, so a 1080p decoder limited to 3 Mbps got a 1080p
stream at 3 Mbps where the shared ladder maps that budget to 720p. A
decoder now takes a class only when its limit earns that class, the rule
download targets already follow.

It also used the hardware H.264 decoders whenever any were attested, even
when none took the source's frame rate, so an opted-in 60 fps software
decoder was ignored and a 60 fps source fell to 480p. Hardware decoders
now win only when one takes the frame rate; otherwise the software ones
that do are used.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Quick104
Quick104 merged commit e0bc169 into fix/hw-encoder-bitrate-ceiling Sep 29, 2026
14 checks passed
@Quick104
Quick104 deleted the feat/download-quality-ladder branch September 29, 2026 16:16

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

Copy link
Copy Markdown

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: 0ef6e42dec

ℹ️ 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".

}
if qualify && ((decoder.MaxFrameRate > 0 && frameRate > decoder.MaxFrameRate+0.01) ||
(checkLists && len(decoder.Profiles) > 0 && !videoProfileSupportedV3(codec, outputProfile, decoder.Profiles)) ||
(codec == transcodeCodecHEVC && len(decoder.Levels) > 0)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor platform-attested HEVC level exemptions

When video_evidence is platform_attested and a hardware HEVC decoder reports a non-empty Levels list, this unconditional check rejects the decoder even though checkLists deliberately exempts platform-attested hardware from profile and H.264-level validation. With HEVC-only caps, every preset therefore returns quality_unavailable; with H.264 also present, the HEVC setting is silently ignored. Fresh evidence beyond the earlier exact-level discussion is the adjacent checkLists exemption introduced for this tier; apply the HEVC-level rejection only when that decoder's lists are authoritative. This also matters for valid capability payloads from third-party clients.

AGENTS.md reference: AGENTS.md:L20-L23

Useful? React with 👍 / 👎.

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 2260abe. A level-bound HEVC decoder is now skipped only when its lists are checked: every entry at the exact tier, and opted-in software entries at platform_attested. A platform-attested hardware HEVC decoder with a level list now takes HEVC output. New case in TestResolveDownloadTranscodeTargetHonorsDecoderLimits. Apple sends empty level lists, so this changes behavior only for third-party clients. hlsHEVCOutputSupportedV3 on main still rejects every level-bound HEVC decoder for streaming. That is stricter but safe (the stream stays H.264), and changing it is outside this PR.

Quick104 added a commit that referenced this pull request Sep 29, 2026
…hardware encoders (#1659)

* fix(playback): treat bitrate caps as ceilings on hardware encoders

QSV and NVENC set -b:v equal to -maxrate, which QSV runs as CBR: a 4 Mbps
source transcoded with a 10 Mbps cap grew to 10 Mbps. VAAPI passed -qp,
which selects CQP and ignores -maxrate, so its cap was never enforced.

A capped hardware encode now averages at 90% of the cap with the cap as
-maxrate, and VAAPI switches to VBR when a cap is set. Uncapped encodes
keep their constant-quality modes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): let VAAPI choose its capped rate-control mode

Forcing -rc_mode VBR fails encoder initialization on a VAAPI driver that
does not advertise VBR. Without -qp, FFmpeg's automatic selection picks
VBR when the driver has it and CBR otherwise; both honor the cap.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): force VBR on capped VAAPI encodes and probe for it

FFmpeg's automatic VAAPI rate control tries AVBR before VBR, and AVBR
does not honor -maxrate, so leaving the mode to FFmpeg could break the
cap on drivers that offer AVBR. Capped VAAPI encodes force VBR again,
and the VAAPI hardware probe now smoke-encodes in that mode, so
automatic backend selection never picks a device without VBR.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): detect VAAPI VBR per device instead of requiring it

Forcing VBR broke an explicitly configured VAAPI backend whose driver
lacks it, and requiring VBR in the availability probe would have kept
automatic selection from choosing a device that can still encode
uncapped. Each start now checks, per device and cached, whether VAAPI
offers VBR for the target encoder: capped encodes force VBR where it
does (FFmpeg's automatic mode would try AVBR first, which ignores
-maxrate) and otherwise leave the mode to FFmpeg, which falls back to
CBR. The availability probe is unchanged from main.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): fall back to CBR on VAAPI devices without VBR

The capped VAAPI encode forced VBR only on a device that passed the VBR
probe and otherwise left the mode to FFmpeg, whose automatic choice can
be AVBR, which ignores -maxrate. Probe VBR, then CBR, and force the first
one the device accepts. A start whose context ends during the probe now
returns that error instead of launching FFmpeg.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): encode in software when VAAPI verifies no capped mode

A capped VAAPI encode on a device that accepted neither VBR nor CBR left
the rate-control mode to FFmpeg, which can pick AVBR and ignore -maxrate.
Move that encode to software instead, as a device without HEVC encoding
already does, keeping a frozen GPU tone-map graph and downloading its
frames to the software encoder. The field that carries that split is
renamed from softwareHEVCEncode to softwareEncode now that it covers
both codecs.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): verify libx265 before a VAAPI HEVC software fallback

A capped HEVC encode on a VAAPI device with no capped rate control moved
to libx265 without checking the FFmpeg build has it, because a working
hevc_vaapi had skipped the software probe. Run the same cached libx265
smoke the HEVC encoder choice uses and fail the start when it is missing.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* test(handlers): answer VAAPI rate-control smoke encodes in the fake FFmpeg

The shared fake FFmpeg slept on every command it did not recognize, so a
capped VAAPI start waited out both rate-control probes and then, with the
software fallback, reported a software encoder. Answer those one-frame
smoke encodes at once, as a VBR-capable driver would.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): verify libx264 before a VAAPI H.264 software fallback

The fallback for a VAAPI device with no capped rate control checked
libx265 for HEVC targets but assumed libx264 for H.264, so a build with
h264_vaapi and no libx264 failed at launch. Probe whichever software
encoder the fallback needs, with the same cached one-frame smoke.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* feat(downloads): pair download presets with a resolution ladder (#1670)

* feat(downloads): pair download presets with a resolution ladder

A download preset set only a bitrate. The output resolution came from the
client's coarse max_resolution, so a 4K source downloaded at 1 Mbps by a
client without caps was encoded at 2160p, and Apple's deliberate "1080p"
ceiling capped every converted download at 1080p.

Each preset now earns a resolution from one shared ladder (2160p at
20 Mbps, 1080p at 10 and 5, 720p at 2, 480p at 1 Mbps at <=30 fps; higher
frame rates drop a class, HEVC stretches the budget). The encode never
enlarges the source, box-fits cinema aspects, never exceeds the source's
own bitrate, and steps down until the device's attested decoder takes it.
HEVC is used when the server allows HEVC encoding and the caps attest an
8-bit HEVC decoder. When a preset would leave an SDR source unchanged and
the device plays it, the source is served and the row still records the
requested preset.

Download encodes use a slower preset, let libx264 choose the level, and
tag HEVC hvc1 so Apple players open it. The downloads capability adds
quality_options with each preset's bitrate and maximum height for labels.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(downloads): honor decoder frame rate, profile and bitrate limits

The download size choice read only a decoder's width and height. A
decoder that cannot take the source's frame rate, or at the exact tier
does not list the High profile the H.264 encode produces, is now skipped;
a level-bound HEVC decoder keeps the encode on H.264, as HLS does; and
the target bitrate stays within the chosen decoder's maximum.

The ladder's comment and architecture note no longer claim that streaming
and jellycompat read the table; they do not until they move onto it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(downloads): bound downloads by H.264 level and decoder bitrate

At the exact tier an H.264 decoder's level list now bounds the output:
each size is checked against the level libx264 would pick for it
(including its doubled rate buffer), and the level also caps the
bitrate. A decoder bitrate limit now lowers the budget the ladder class
is chosen from, so a 3 Mbps decoder no longer gets a 1080p class.

When frame-rate or profile checks leave no H.264 decoder, the largest
decoder's size still bounds the output instead of leaving it unbounded.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(downloads): choose the decoder by achievable output and honor attested lists

Pick the strict-tier decoder whose limits reach the tallest ladder class
instead of the one with the largest frame, so a 4K decoder capped at a low
bitrate no longer drags a download below what a second decoder plays.
Check profile and level lists on platform-attested software decoders, as
playback does; only platform-attested hardware decoders are exempt. When
strict caps attest no decoder for any codec the server may encode, answer
quality_unavailable rather than producing H.264 the device never listed.

Label quality_options with the transcode ceiling a policy override sets,
matching what Resolve produces.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(downloads): stop presets at 1080p without 4K and check levels at the encode bitrate

With 4K transcoding off, quality_options advertised presets up to 1080p,
but a 20 Mbps request could still serve a 4K original or keep a 1440p
source at its size. Cap the preset class at 1080p in that case, which
also matches the documented quality_unavailable answer for 4K titles.

Check a decoder's H.264 level at the bitrate the encoder will get, which
never exceeds the source's own, so a 3 Mbps 1080p source is not scaled
down for a level-4.0 decoder. A decoder smaller than the 480p class no
longer receives 480p output; with no decoder that takes a class, the
preset is unavailable.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): keep a source known only by its height within the class

FitLadderBox returned zeros whenever the width was unknown, so a caller
fell back to the class box and could enlarge a short source. Keep such a
source's own height when it fits the class and the class height when it
is taller, leaving the width unknown.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(downloads): give an odd source frame an even size when encoding it

A download encode that keeps the source frame passed no scale, so an odd
height or width reached a 4:2:0 encoder unchanged and could fail. Scale
such a frame to the even height at or below it; scale=-2 evens the width.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): scale to any even height a box fit produces

The scale parser rejected heights below 144, so a very wide source fitted
into a small class (1280x100 into the 480p box is 844x66) was left
unscaled while its recipe claimed the fitted size. Accept every even
height up to 4320.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(downloads): cap preset targets at the account's quality ceiling

quality_options already capped each preset at the account's max playback
quality, but Resolve applied only a policy override's ceiling, so with 4K
transcoding on a 1080p account could prepare a 2160p artifact that the
serve-time access check then refused. Apply the narrower of the two.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(downloads): check decoder bounds against the frame the encoder writes

A source that fits a class keeps its size, but an odd frame is evened by
scale=-2, so a 1917x1080 source was checked against a 1917-wide decoder
and then encoded 1918 wide. Check each class at the frame the encoder
actually produces.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* feat(playback): use the shared ladder for automatic streaming quality (#1671)

* feat(playback): use the shared ladder for automatic streaming quality

Automatic streaming quality, the plain resolution preferences, bandwidth
caps and jellycompat each had their own bitrate-to-resolution table, and
they disagreed with each other and with download presets. All of them
now read the shared ladder: 80% of the bandwidth budget earns a class for
the source's frame rate, the source is fit into that class's box so a
scope film at 1080p streams at 1920x800, and a 540p class sits between
720p and 480p. The encode never targets more than that 80% budget, and a
source that fits the class but exceeds the estimate's budget is re-encoded
at its own size instead of sent as-is. Explicit menu rungs keep their own
table.

An original-quality route that still needs a video transcode labelled a
3840x1600 source "1080p", which scaled it to 2592x1080 while the plan
reported 3840x1600. It now keeps the exact source height.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): bound auto transcodes by the source and jellycompat by video budget

An automatic transcode that only scales a low-bitrate source down could
target the class bitrate even when the source used fewer bits; it now
stays within the source's own bitrate, counted as H.264. jellycompat
chose its class from the client's whole bitrate ceiling while encoding
at the video share after audio and mux overhead; the class now follows
that video budget.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): keep source-size labels even and budget unknown source bitrates

A route that keeps the source frame but still encodes labeled an odd
source height such as 1081p, which the scale helpers reject, leaving an
odd frame for a 4:2:0 encoder. Round the label down to the even height.

Automatic quality sent a source of unknown bitrate as-is whenever its
class fit the estimate, so a 7 Mbps estimate could stream a 1080p file of
any bitrate. Count an unknown bitrate as the full bitrate of the class it
would be sent at, so it goes as-is only when the budget covers that.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): keep cropped sources in the class they earned

Automatic quality bounded the target by the source height before snapping
to the ladder, so a 1920x800 film under a 1080p estimate snapped to 720p:
over its budget it was scaled to about 1276x532 at 2 Mbps instead of being
re-encoded at its own size, and with an unknown bitrate it was sized as a
2 Mbps stream. A target that keeps the source height now snaps into the
class the source's frame belongs to by both dimensions, within the class
the preference, device, bandwidth or cap allows.

jellycompat no longer upscales a track known only by its height.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): re-encode an odd source frame at an even size

An automatic transcode that keeps the source frame labeled an odd height
such as 1079p, which the scale helpers reject, so the odd frame reached a
4:2:0 encoder. Round the fitted height down to even and report the width
scale=-2 gives it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): bound HEVC stream output by the source's HEVC bitrate

The quality policy caps a scaled encode at the source's bitrate counted
as H.264, and the planner can choose HEVC afterwards without revisiting
it, so a 3 Mbps HEVC source converted to 1080p HEVC could target 5 Mbps.
Re-cap the bitrate at the source's bits counted as HEVC when HEVC output
is chosen.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): check the HEVC target the planner encodes and keep H.264's on fallback

The planner checked the HEVC decoder against the H.264-equivalent bitrate
before capping it for HEVC, so a decoder limited to 4 Mbps refused a
3 Mbps HEVC encode and an HEVC-only delivery had no route. Cap first and
check that candidate. A failed HEVC attempt now falls back to H.264 at
the bitrate the policy chose for H.264 instead of the HEVC cap.

Routes that keep the source size also report the even frame they encode
(1918x1080 for a 1920x1081 source), so the HEVC decoder check sees the
real output.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): encode an unknown-bitrate source under a bandwidth cap

With the shared ladder a 7 Mbps cap earns the 1080p class, so a 1080p
source of unknown bitrate passed the cap check and was sent unchanged.
The cap is a hard ceiling: treat an unknown video bitrate as over it, as
the server bitrate cap already does, so the source is re-encoded within
the cap.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): reserve the server-cap audio share before checking HEVC

The planner checked the HEVC decoder against the video bitrate before a
server bitrate cap reserved its AAC and mux share, so an HEVC-only client
whose decoder took the capped rate was refused over the uncapped one and
had no route. Reserve the share first; it does not depend on the video
codec.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* docs(playback): describe plain-label sizing and unknown bitrates

The protocol doc still promised height-only behavior for plain resolution
labels, which now fit the source into the class box like auto, and said a
cap re-encodes only a source known to exceed it. State both rules as the
planner applies them.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): cap every HEVC encode at the source's HEVC-equivalent rate

A route that keeps the source size but still re-encodes it (a codec the
client cannot play, or a subtitle burn) skipped the HEVC cap, so an 8 Mbps
H.264 source was checked as an 8 Mbps HEVC output and an HEVC-only client
with a 6 Mbps decoder had no route. Cap every HEVC encode; the cap only
lowers the bitrate when HEVC is the more efficient codec.

Document that a source whose height fits the class is sent as-is even
when it is wider than the 16:9 box.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): keep H.264 stream targets within the attested decoder bitrate

HLS treats H.264 as the universal output and never checked its bitrate
against the client's decoder. The shared ladder reaches 1080p at lower
estimates, so a 7 Mbps estimate now targets 5.6 Mbps where the previous
ladder chose 2 Mbps, past an H.264 decoder that attests a 4 Mbps limit.
Lower an H.264 target to the limit of an attested decoder that takes the
output size; HEVC output keeps its own decoder check.

Qualify the ladder invariants for the streaming preservation exception
and the H.264 bound.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): step H.264 stream targets down to an attested decoder's size

A client whose attested H.264 decoders stop at 1280x720 received a 1080p
H.264 target at a 7 Mbps estimate, where the previous ladder chose 720p.
Size the H.264 candidate to the tallest class an attested H.264 decoder
takes before bounding its bitrate; HEVC output keeps its own class, and
the H.264 fallback after a failed HEVC attempt uses the H.264 size.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): step H.264 targets down until the fitted size fits a decoder

Snapping an attested H.264 decoder's limits to a ladder box let a 1280x962
decoder count as 1080p, so a 1920x1080 target passed although it is wider
than the decoder. Step down class by class until the fitted size itself
fits an attested decoder, stopping at the smallest class since H.264 stays
the universal HLS output.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): fit same-size H.264 conversions to an attested decoder

A source whose size fits the estimate but whose codec the client cannot
play is converted at its own size, and that conversion skipped the H.264
decoder fit, so an H.264-only client with a 720p decoder received a 1080p
H.264 stream. Fit any H.264 encode the attested decoders cannot take,
scaled or not.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* docs(playback): note that H.264 conversions are fitted to the decoder too

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): match H.264 decoders by frame rate as well as size

The decoder fit compared only width and height, so a 60 fps source was
sent at 1080p to a client whose 1080p H.264 decoder stops at 30 fps and
whose 60 fps decoder stops at 720p. The encode keeps the source frame
rate, so a decoder must take it too.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): fit streamed H.264 to the frame the encoder writes

The H.264 decoder fit compared box-fit sizes that scale=-2 later evens,
so an odd-width source could pass a decoder limit it then exceeded by a
pixel. Check the encoded frame, sharing encodedFrame with downloads and
the source-size routes. The ladder doc now states the smallest-class
floor for streaming and the unavailable answer for downloads.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): size streamed H.264 by the decoder's bitrate and frame rate

The streaming H.264 decoder fit chose the output size before looking at a
decoder's bitrate limit, so a 1080p decoder limited to 3 Mbps got a 1080p
stream at 3 Mbps where the shared ladder maps that budget to 720p. A
decoder now takes a class only when its limit earns that class, the rule
download targets already follow.

It also used the hardware H.264 decoders whenever any were attested, even
when none took the source's frame rate, so an opted-in 60 fps software
decoder was ignored and a 60 fps source fell to 480p. Hardware decoders
now win only when one takes the frame rate; otherwise the software ones
that do are used.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* docs(playback): note the streamed H.264 bitrate and frame-rate rules

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): keep streamed H.264 sizes for fast, tall, and wide sources

Three streaming-ladder cases produced a smaller or larger encode than
main did:

- A 50/60 fps source whose attested H.264 decoders all report a lower
  rate (Android reports the rate at the decoder's largest size) fell to
  the 480p floor. The sizes alone now bound the encode, as they do for
  downloads.
- An original-quality transcode of a source taller than 2160 lines
  encoded at its own height (4320p, or unscaled above 4320). It is now
  fitted into the 2160p box.
- A same-size conversion of a wide source (2560x1080) was classed 2160p
  by its width, so a decoder bitrate limit below 20 Mbps stepped it down
  to 1920x810. It keeps the 1080p class its height earned.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* docs(playback): describe the capped VAAPI rate-control fallback

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(playback): bound a streamed H.264 frame by the decoder's pixel rate

The previous commit let decoder sizes alone bound a high-frame-rate
encode when no attested decoder listed the source rate, so a 1080p30
decoder could be sent 1080p60. A reported rate holds at the decoder's
largest size, so a frame now fits when its pixel rate does: a 4K30
decoder takes 1080p60, and a 1080p30 decoder takes 720p60.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.

1 participant