Skip to content

Pass packets as plain AVPacket through the demux/decode seams - #1695

Merged
NicolasHug merged 3 commits into
mainfrom
demuxer-owned-packets
Sep 14, 2026
Merged

NicolasHug merged 3 commits into
mainfrom
demuxer-owned-packets

Conversation

@NicolasHug

@NicolasHug NicolasHug commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Applies F.7: read_next_packet() now takes AVPacket& and send_packet() takes const AVPacket&, instead of ReferenceAVPacket&. Callers keep whatever ownership suits them. This drops two defensive refcount dances: the alloc + av_packet_move_ref() in Demuxer::next_packet(), and the per-packet av_packet_ref in PacketDecoder::send_packet() (now done only in apply_bsf(), the one place that actually consumes the packet).

Drive-by fix: read_next_active_packet() never unref'd the packets it skipped. av_read_frame() doesn't free what it overwrites, so each one leaked its payload — worst case, a scan of an A/V file leaked the whole audio track.

@pytorch-bot

pytorch-bot Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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 4, 2026
@NicolasHug
NicolasHug force-pushed the demuxer-owned-packets branch 2 times, most recently from 7ebd839 to 2b0ac1b Compare September 4, 2026 09:28
@NicolasHug
NicolasHug force-pushed the demuxer-owned-packets branch from 2b0ac1b to 665ea66 Compare September 4, 2026 10:02
@NicolasHug
NicolasHug force-pushed the demuxer-owned-packets branch from 665ea66 to 2d404db Compare September 4, 2026 10:18
@NicolasHug
NicolasHug removed this pull request from stack #1700 September 10, 2026 08:52
@NicolasHug
NicolasHug added this pull request to stack #1702 September 10, 2026 08:55
@NicolasHug
NicolasHug force-pushed the demuxer-owned-packets branch from 2d404db to ee088c3 Compare September 10, 2026 08:55
Base automatically changed from blocks-planes-coverage to main September 11, 2026 10:53
@NicolasHug
NicolasHug force-pushed the demuxer-owned-packets branch from ee088c3 to d0618ef Compare September 11, 2026 10:53
@NicolasHug NicolasHug changed the title Let the Demuxer read straight into the packet it hands out Pass packets as plain AVPacket through the demux/decode seams Sep 14, 2026
next_packet() has to return a packet the caller owns indefinitely: the
Python-level Packet can be held, moved to another thread, and decoded much
later. That is a UniqueAVPacket, and the AutoAVPacket / ReferenceAVPacket
pair is for the opposite thing - a loop that reuses one allocation and
unrefs at the end of each iteration.

So next_packet() was using the loop idiom and then undoing it: it read into
the shared packet, allocated a second one anyway, and moved the reference
across. Neither half of the idiom paid for itself - the allocation isn't
saved, and av_packet_move_ref() leaves the source blank so the scope-exit
unref has nothing to do.

read_next_packet() has no opinion on how the packet is owned, it just needs
somewhere to write, so it takes the AVPacket itself. Its callers keep the
ownership type that suits them: a ReferenceAVPacket for the two read loops
(SingleStreamDecoder and scan), a UniqueAVPacket for next_packet(). It also
unrefs between iterations, which av_read_frame() documents as the caller's
job rather than something it does for you.
@NicolasHug
NicolasHug force-pushed the demuxer-owned-packets branch from d0618ef to 4bc28de Compare September 14, 2026 10:51
@NicolasHug
NicolasHug merged commit ea5caab into main Sep 14, 2026
16 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