Skip to content

Add Packet.from_bytes and Packet.from_tensor, and integer pts on decoded frames - #1782

Draft
NicolasHug wants to merge 1 commit into
pickle-packetsfrom
packet-from-bytes
Draft

NicolasHug wants to merge 1 commit into
pickle-packetsfrom
packet-from-bytes

Conversation

@NicolasHug

@NicolasHug NicolasHug commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

This is part of a stack that adds demuxer-free decoding to the low-level APIs: decoding packets that don't come from a Demuxer (#1585), demuxing and decoding in different processes, and raw H.264 / HEVC streams (#1771).


Adds Packet.from_bytes() and Packet.from_tensor() to build packets from compressed data that doesn't come from a Demuxer, e.g. packets from the network or from a database, as in #1585:

params = VideoPacketDecoderParameters("h264", time_base=(1, 1_000_000_000))
decoder = VideoPacketDecoder(params, device="cuda")

for data, pts in rows:  # from a keyframe, in decoding order
    for raw_frame in decoder.decode(Packet.from_bytes(data, pts=pts)):
        if raw_frame.pts == target_pts:
            ...
  • Copying: the data is copied. FFmpeg needs zeroed padding past the end of packet data, which user buffers don't have. Zero-copy could come later, through new entry points into memory we allocate.
  • Optional fields: pts, dts, duration, is_key_frame and side_data are all optional. Decoders only need the data, plus pts to report frame timestamps. In particular dts isn't needed for streams with B-frames: there's a test for that, CPU and CUDA.
  • Accessors: packets also expose pts, dts, duration, is_key_frame, data and side_data.
  • Integer pts on frames: RawFrame.pts and RawAudioSamples.pts now expose the decoded frames' pts as integers, in the new VideoPacketDecoder.time_base / AudioPacketDecoder.time_base, or None when unknown. Packets go in with integer timestamps, so this lets users match a decoded frame to the timestamp they asked for exactly, rather than reproducing our float conversion bit for bit.

Addresses the use case of #1585.

Tests

  • Packets rebuilt from demuxed packets' bytes decode identically, CPU and CUDA.
  • Packets with only pts, or no timestamps at all, decode fine.
  • A Add Packet.from_tensor to build packets from raw data #1585-style test: decode targets from keyframe-to-target packet windows, reusing one decoder.
  • Accessors, copy semantics, validation errors, and integer pts matching the scanned index.

Stack created with GitHub Stacks CLI • Give Feedback 💬

@pytorch-bot

pytorch-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 18 New Failures, 8 Unrelated Failures

As of commit 86e1dcf with merge base ffb098a (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@NicolasHug
NicolasHug added this pull request to stack #1784 October 27, 2026 13:04
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 27, 2026
@NicolasHug NicolasHug changed the title Add Packet.from_bytes and Packet.from_tensor Add Packet.from_bytes and Packet.from_tensor, and integer pts on decoded frames Oct 27, 2026
@NicolasHug
NicolasHug force-pushed the packet-from-bytes branch 2 times, most recently from c9aa236 to 4db75e3 Compare October 27, 2026 13:44
Packets can now be built from compressed data that doesn't come from a
Demuxer, e.g. packets received from the network or stored in a
database, together with hand-built VideoPacketDecoderParameters or
AudioPacketDecoderParameters. The data is copied. pts, dts, duration,
is_key_frame and side data are all optional: decoders only need the data
(and pts, to report frame timestamps). In particular dts isn't needed
for streams with B-frames.

Packets also expose pts, dts, duration, is_key_frame, data and
side_data.

Since such packets go in with integer timestamps, RawFrame.pts and
RawAudioSamples.pts now expose the decoded frames' pts as integers too,
in the new VideoPacketDecoder.time_base / AudioPacketDecoder.time_base,
or None when unknown. That's what lets users match a decoded frame to the
packet timestamp they asked for exactly, rather than comparing floats.

Addresses the use case of #1585.

This branch has not been deployed

No deployments
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