Skip to content

Scan every followed video stream in one pass, and only before demuxing - #1678

Merged
NicolasHug merged 1 commit into
mainfrom
msd/cpp-cached-scan
Sep 3, 2026
Merged

Scan every followed video stream in one pass, and only before demuxing#1678
NicolasHug merged 1 commit into
mainfrom
msd/cpp-cached-scan

Conversation

@NicolasHug

@NicolasHug NicolasHug commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

A scan used to read the container once per stream and cache nothing, so indexing two video streams read the file twice and calling scan() twice read it twice again. It is now two phases: one pass over the container buckets the packets of every followed video stream, and the sort plus tensor build happens per stream, the first time that stream's scan is asked for. The I/O is what a scan costs, and it is now paid once. The sorting is paid per-stream (no way around that), but only if / when requested.

Scanning after the first packet has been demuxed raises.

Scanning a non-video stream now raises rather than quietly producing a frame index for something that has no frames or keyframes.


Stack created with GitHub Stacks CLIGive Feedback 💬

@pytorch-bot

pytorch-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/meta-pytorch/torchcodec/1678

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 7 Pending

As of commit 1608594 with merge base ba6b3f7 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 1, 2026
@NicolasHug
NicolasHug force-pushed the msd/cpp-cached-scan branch 2 times, most recently from 3361e63 to e1b8c56 Compare September 2, 2026 11:06
@NicolasHug
NicolasHug force-pushed the msd/cpp-cached-scan branch 2 times, most recently from d394b1e to c19d13b Compare September 2, 2026 13:47
ReferenceAVPacket packet(auto_packet_);
int status =
read_next_packet(format_context_.get(), scanned_stream->index, packet);
read_next_packet(format_context_.get(), video_stream_indices, packet);

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.

Do we still need the read_next_packet that only takes a single stream index then??

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.

Yes, it's used in SingleStreamDecoder

@NicolasHug
NicolasHug force-pushed the msd/cpp-cached-scan branch 2 times, most recently from ccd7717 to fda056c Compare September 3, 2026 08:29
Base automatically changed from msd/cpp-multi-stream to main September 3, 2026 08:33
@NicolasHug
NicolasHug force-pushed the msd/cpp-cached-scan branch 2 times, most recently from 2aa0235 to 4792ecd Compare September 3, 2026 08:33
A scan used to read the container once per stream and cache nothing, so
indexing two video streams read the file twice and calling scan() twice read
it twice again. It is now two phases: one pass over the container buckets the
packets of every followed video stream, and the sort plus tensor build
happens per stream, the first time that stream's index is asked for. The I/O
is what a scan costs, and it is now paid once.

Scanning after the first packet has been demuxed raises. The scan rewinds the
container, which would silently desynchronise any decoder already being fed
from it - and with several streams there are several decoders to remember to
reset. Requiring the scan up front removes the problem instead of documenting
it: nothing has been fed to a decoder yet, so there is nothing to reset, and
the "a decoder built from it must be reset()" caveat is gone.

Scanning a non-video stream now raises rather than quietly producing a frame
index for something that has no frames or keyframes.

A test pins the rewind behaviour on discard_first_keyframe.mp4: the scan
rewinds with a seek rather than reopening, and on a file whose edit list
discards its first packets those are not obviously equivalent. They are.
@NicolasHug
NicolasHug merged commit d6d009f into main Sep 3, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant