Repository navigation
perf(trickplay): start Matroska list entries at their keyframes - #2484
Conversation
A seek-preview list entry that starts at its sample time makes the concat demuxer read every packet of every stream from the keyframe before the sample up to it, most of the file when keyframes are seconds apart. Sheets lists over Matroska now start each entry at the sample's keyframe, read once per run from the file's Cues, and leave out samples that share the previous sample's keyframe; the repeat repair decodes those at their own time. A keyframe list whose decoder passes on frames out of list order or drops a keyframe as a duplicate picture order count (open-GOP HEVC across long gaps) is abandoned for sample-time entries. Closes #2476 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cap the Cues reads in flight across the process, skip the read when every slot is taken, and stop a read between calls once its run stops waiting. The cancellation test now waits for its lookup to exit, so it no longer leaks into the next test's stub. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
Security findingsAdvisory findings (1)
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
There was a problem hiding this comment.
🛡️ Codex Security Review · Automatically triggered
Here are some automated security review suggestions for this pull request.
Reviewed commit: 78265f62ac
ℹ️ About Codex security reviews in GitHub
This is an experimental Codex feature. Security reviews are triggered when:
- You comment "@codex security review"
- A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review
Once complete, Codex will leave suggestions, or a comment if no findings are found.
Parsing Cues allocates a few times their size, and keyframes.ReadMatroska accepts up to 64 MiB, so the in-flight bound also bounds that memory. A lookup takes milliseconds; four slots serve any number of workers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
When ffmpeg exited cleanly and the attempt's time ran out while its buffered frames tripped the guard, the abandoned read's result was returned as a success. An abandoned read is now always discarded: reread from sample times while the attempt lives, else fail as timed out or canceled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b0d9223618
ℹ️ 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".
| return nil | ||
| } | ||
| defer func() { _ = file.Close() }() | ||
| index, err := keyframes.ReadMatroska(contextReaderAt{ctx: ctx, r: file}, info.Size()) |
There was a problem hiding this comment.
Require a verified cue index before optimizing seeks
For a valid Matroska file whose Cues omit a video keyframe outside the four clusters sampled by checkCueCoverage, ReadMatroska returns the incomplete index even though VerifyMatroska would reject it. The new plan can then start a 0.5-second entry at an older cue; a first or single sample accepts that stale frame because it is neither missing nor comparable to a preceding cell for repeat repair, producing an incorrect thumbnail instead of falling back to sample-time entries. Use only a fully verified index here, or treat an unverified index as unavailable until verification completes.
AGENTS.md reference: AGENTS.md:L73-L75
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not a regression, so I'm keeping ReadMatroska. The concat demuxer's seek uses these same Cues. With a keyframe missing from the Cues, a sample-time entry on main seeks to the same earlier cued keyframe. It decodes that keyframe first, then the uncued one up to the sample, and keeps the first frame. A keyframe entry starting at that cue decodes the same first frame. Both builds show the same picture, and the shared-keyframe and repeat-repair logic sees the same cue for both samples.
VerifyMatroska (via Load/LoadVerified) reads the block headers of every cluster, which is a full pass over the file. That would cost about as much as the reads this change removes. On real fixtures (two 4K remuxes, 1,521 and 1,315 sample times), "last cue at or before the sample" matched where ffprobe's -read_intervals seek, the same avformat_seek_file call concat makes, landed every time. checkCueCoverage still rejects files whose Cues skip keyframes in the sampled clusters, and those keep sample-time entries.
I haven't reproduced a sparse-Cues file here: ffmpeg's muxer cues every video keyframe, and mkvmerge isn't available. The argument above follows from both builds seeking through the same index.
…me-inpoints # Conflicts: # internal/mediasample/run.go
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
syscall.Mkfifo does not exist on Windows, so the package's tests did not build there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9adbc37226
ℹ️ 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".
Checking the path before opening it left a window in which a FIFO could replace the file and hold the open, and so a lookup slot, indefinitely. Open non-blocking and check the descriptor instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c0ac83a9bb
ℹ️ 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".
… starts Co-Authored-By: Claude Opus 5.5 <[email protected]>
#2463 adds H.264's "illegal short term buffer state" as a dropped-keyframe signal (droppedKeyframe) and has samples() watch it alongside duplicate POCs. sheets() keeps main's log-handler signature: a keyframe list now abandons on any dropped keyframe, and its caller's handlers see none of an abandoned read's log from the line that abandons it on. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
…ead test The test waited for the attempt's deadline by the wall clock, but the context's own timer can fire a moment later on a loaded runner, so the attempt still looked live and the run took the reread path. Drive attemptRun.sheets with an attempt context whose deadline has passed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
Problem
Closes #2476
Related issue: N/A
Validation tasks: none
Generating seek previews reads most of each Matroska file to make a few hundred thumbnails. On hosts whose library sits on spinning disks, the preview queue keeps a disk busy for every file and slows playback from that disk. This change starts each Matroska list entry at the sample's keyframe instead of at the sample time, so ffmpeg reads the keyframe and a short span after it rather than everything between the keyframe and the sample. On a 1080p H.264 film with two audio tracks, the reads drop from 6.9 GB to 0.7 GB, and the published previews are the same cells. It builds on the approach and measurements in #2464.
A list entry that starts at its sample time makes the concat demuxer seek to the keyframe before the sample and read every packet of every stream, video and all audio tracks, up to the sample. The decoder only needs the keyframe (
-skip_frame nokey).Approach
Runner.Runreads the input's keyframes once, before any attempt, from the Matroska Cues withkeyframes.ReadMatroska. Cues are the index the demuxer's own seek uses, and the read happens in-process without decoding. It runs in a goroutine with a 10-second bound and at most 4 reads in flight per process, so stalled storage cannot hold a trickplay worker and parsing cannot stack (Cues parsing allocates a few times their size; the parser's own limit is [bug] Reading a Matroska file's keyframe index can allocate hundreds of megabytes #2487, fixed in fix(playback): bound the Cues a Matroska keyframe index reads #2492). A file without usable Cues, or a read that fails, keeps today's sample-time entries. A hardware attempt that falls back to software reuses the lookup.mainhid some repeats from the repair (a new test with a 40-second GOP fails onmainfor this reason).maindoes. Without this guard, open-GOP HEVC placed more wrong cells thanmain.docs/architecture/media-sampling.mdgains a "Keyframe entries" section, and its Repairs section is updated.Server and transcode nodes only. Clients and the Jellyfin-compatible surface read the same published sheets and need no change. The user manual does not describe how previews are built.
Validation
go build ./...,gofmt,go vet ./...,make lint-changed,make verify-local-paths,make verify-case-collisions: clean. The web gate and the API contract checks were not run; the change touches no web, API, or migration files.make test-gowith ffmpeg 6.1 on 78265f6: every package passes except twointernal/mediasamplereal-ffmpeg tests that fail onmaintoo with that ffmpeg. On the current head, after mergingmain,internal/mediasamplefails onlyTestRunStatsWithRealFFmpegwith ffmpeg 6.1, asmaindoes. With Jellyfin FFmpeg 7.1.4, the build Silo ships, the wholeinternal/mediasamplepackage passes, including the new real-ffmpeg tests.main: x264 and x265 at default settings and with scene cuts off at 5, 10, 20, and 40-second GOPs; open- and closed-GOP HEVC, includinglog2-max-poc-lsb=8; Matroska and MP4; samples every 5 and 10 seconds; counting cells whose picture is not the scene at the sample time. Rerun againstmainafter fix(trickplay): publish seek previews for XviD/DivX AVI and interlaced MPEG-2 #2462 and fix(trickplay): publish seek previews for H.264 files whose keyframes reference skipped pictures #2463, with this branch merged on top and interlaced MPEG-2 and MPEG-4 Part 2 at 0.5 and 10-second GOPs added: no case is worse thanmainwith ffmpeg 6.1 (1,329 wrong cells against 1,444) or Jellyfin FFmpeg 7.1.4 (1,717 against 1,832). MP4 cases are identical, and MPEG-2 and MPEG-4 Part 2 place no wrong cells on either build.main(after fix(trickplay): publish seek previews for XviD/DivX AVI and interlaced MPEG-2 #2462 and fix(trickplay): publish seek previews for H.264 files whose keyframes reference skipped pictures #2463) and from this branch merged on top: seek previews for seven release fixtures, with reads taken from/proc/<pid>/iofor every ffmpeg the trickplay worker ran. Every file published the same number of cells, none filled. Six publish byte-identical sheets. On the 1080p H.264 film, 3 of 622 cells differ: onmaineach showed the keyframe half a second after its sample, the same picture as the next cell, and now each shows the keyframe before its sample.Reads per file,
mainvs this branchmain: read, read callsMeasured against
mainat 8f28583 with this branch merged on top. The Dolby Vision file's open-GOP keyframes come out of order across 10-second jumps, so the branch abandons its keyframe list in each of two chunks within about a second and reads asmaindoes; those two runs are the 0.8% extra. The HDR10 file has keyframes under a second apart, so most of each entry's remaining cost is the concat demuxer opening and probing the file again for every entry, about 5 MB of the roughly 10 MB per entry. This change does not address that.Evidence
Before-and-after sheets from the sandbox, the three cells that differ, and the comparison matrix tables.
Evidence: https://evidence.siloserver.org/r/silo-server/issue-2476/
Risks
Possible follow-ups, not in this change:
option probesize/analyzedurationin the list), which dominates on short-GOP 4K remuxes.main, whatever the keyframe distance.Checklist
AI Disclosure
main, that leaving out a sample whose distinct keyframe fell inside the previous entry's span could lose a valid frame, that reordered output makes source times unreliable, and that an in-process Cues read could block past the attempt timeout. These led to the reorder and duplicate guard, omitting only samples that share a keyframe, and the bounded goroutine lookup. Round 2 reviewed the implementation, found unbounded leftover lookups and a test race, and both were fixed. Codex's security review found that Cues parsing allocates a few times their size; the lookup cap is now 4, and the parser's own bound, which predates this change through playback, is [bug] Reading a Matroska file's keyframe index can allocate hundreds of megabytes #2487, fixed in fix(playback): bound the Cues a Matroska keyframe index reads #2492. It reran hardware, threading, multi-track, all-intra, WebM, negative-start, unknown-size-segment and fractional-timescale cases with equal or better cells. A background review by Codex (gpt-6.1-sol) of 78265f6 found one issue: an abandoned keyframe read could be published when ffmpeg exited cleanly and the attempt's time ran out while its frames tripped the guard. It is fixed in b0d9223 with a test that fails before the fix.🤖 Generated with Claude Code
Note
Start Matroska Sheets list entries at their keyframes in
mediasampleto speed up trickplayRunner.Runfor non-ReadThrough Sheets requests, with a 10-second timeout and 4 concurrent lookups process-wide. Lookup uses a nonblocking open and skips non-regular files such as FIFOs. See keyframes.go.Macroscope summarized 16411a6.