Repository navigation
Add decoder parameters, and build packet decoders from them - #1779
Draft
NicolasHug wants to merge 2 commits into
Draft
NicolasHug wants to merge 2 commits into
NicolasHug wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/meta-pytorch/torchcodec/1779
Note: Links to docs will display an error until the docs builds have been completed. ❌ 8 New Failures, 10 Pending, 7 Unrelated FailuresAs of commit 2141766 with merge base ffb098a ( NEW FAILURES - The following jobs have failed:
FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
NicolasHug
force-pushed
the
packet-decoder-parameters-cpp
branch
2 times, most recently
from
October 27, 2026 13:43
9117bbc to
183023e
Compare
NicolasHug
force-pushed
the
packet-decoder-parameters-cpp
branch
2 times, most recently
from
October 27, 2026 13:54
baa0520 to
2583ab0
Compare
These return a VideoPacketDecoderParameters / AudioPacketDecoderParameters: everything FFmpeg knows about a stream that a decoder needs to decode its packets. That's every field of FFmpeg's AVCodecParameters, plus the stream's time base, and for video its r_frame_rate and rotation. They're frozen dataclasses of plain values, with FFmpeg's enums stored by name, so they can be pickled, inspected, and built by hand. The next PR builds packet decoders from them. On the C++ side, DecoderParameters holds the same thing, and crosses over to Python as a map of strings (bytes in hex). The class names are provisional, marked TODO_DEMUXER_FREE. Part of #1585.
VideoPacketDecoder(parameters, device, *, num_ffmpeg_threads, decoder) and AudioPacketDecoder(parameters, *, decoder) can now be built directly from VideoPacketDecoderParameters / AudioPacketDecoderParameters, whether they come from a stream or are built by hand. stream.make_decoder() goes through the same path, so a demuxed stream and its parameters can't decode differently. decoder= selects a specific FFmpeg decoder. Invalid parameters (unknown codec, enum name or field) raise when building the decoder. On the C++ side, PacketDecoder is now built from DecoderParameters instead of a Demuxer, and so is the device interface: DeviceInterface::initialize_video_decoding() takes DecoderParameters instead of an AVStream and AVFormatContext. This breaks out-of-tree device interfaces that override it. Since there may not be a container anymore, NVDEC no longer picks its bitstream filter from the container name: the mp4toannexb filters are applied when the extradata isn't Annex B, the same check those filters make, and mpeg4_unpack_bframes is applied to every MPEG-4 stream (it passes through packets that aren't packed). Part of #1585.
NicolasHug
force-pushed
the
packet-decoder-parameters-cpp
branch
from
October 27, 2026 19:04
aec5f26 to
497f19e
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
VideoStream.decoder_parameters/AudioStream.decoder_parameters, which return aVideoPacketDecoderParameters/AudioPacketDecoderParameters: everything FFmpeg knows about a stream that a decoder needs to decode its packets.VideoPacketDecoderandAudioPacketDecodercan be built directly from them, whether they come from a stream or are built by hand:Design
AVCodecParametersis exposed, plus the stream'stime_base, and for videor_frame_rateandrotation. So users building parameters by hand never need a new release to set the field their codec needs. Unset fields (None) keep FFmpeg's defaults."yuv420p","bt709","stereo"). So they pickle as is, and nothing is hidden. The one exception is a private flag for MPEG-PS packet misalignment.stream.make_decoder()goes through the parameters too, so a demuxed stream and its parameters can't decode differently.decoder=selects a specific FFmpeg decoder, which must decode the parameters' codec.framerateneeds FFmpeg 6 andcoded_side_dataneeds FFmpeg 6.1; setting them on older versions raises.pixel_format,widthandheight: NVDEC checks them before decoding to decide whether it supports the stream, and falls back to CPU decoding if it doesn't. Without them it would always fall back, silently, so leaving them out raises instead.The class names are provisional, marked
TODO_DEMUXER_FREE.C++ changes worth a look
DecoderParametersholds the same thing as the Python classes (an ownedAVCodecParametersplus the stream-level fields). It crosses over to Python as a map of strings, with bytes in hex.DemuxerinPacketDecoder: it's built fromDecoderParameters.DeviceInterface::initialize_video_decoding()(andinitialize_video()) take theDecoderParametersinstead of anAVStreamand anAVFormatContext, so out-of-tree interfaces that override it need updating. The in-tree third-party interface test doesn't override it.SingleStreamDecoderbuilds itsDecoderParametersfrom its stream with the sameget_decoder_parameters()as theDemuxerpath.h264_mp4toannexb/hevc_mp4toannexbare now applied when the extradata isn't Annex B, using the same check as the filters themselves: Annex B extradata is empty or starts with a start code.mpeg4_unpack_bframesis now applied to every MPEG-4 stream, not just AVI ones. It passes any packet that isn't DivX-packed through unchanged.Part of #1585.
Tests
codecandtime_base, PCM. CPU and CUDA. The CUDA cases go through the extradata-based filter choice.time_base/codec_tagnormalization, validation errors,decoder=, pickling,repr.Stack created with GitHub Stacks CLI • Give Feedback 💬