Skip to content

TorchCodec 1.0

Latest

Choose a tag to compare

@NicolasHug NicolasHug released this 02 Dec 10:17
· 10 commits to main since this release
6902b22

TorchCodec 1.0 is out! It is compatible with torch >= 2.11 and Python >= 3.11.

TorchCodec started out as a video decoding library with a simple goal: efficiently turning encoded videos into RGB video frame tensors. Since then, it has grown to decode and encode all media, including audio and images. Our APIs have been stable for a while, and 1.0 makes it official!

The highlights of this release are:

  • The low-level decoding APIs are now stable. Introduced as Beta in 0.17, they give you full control over the demuxing, decoding, and conversion stages, enabling multi-threaded and multi-process pipelines, hill-climbs, raw YUV access, custom color conversion, and more.
  • Demuxer-free and container-free decoding: decode packets that don't come from a Demuxer (stored in a database, received over the network, parsed by your own demuxer), and decode raw streams (e.g. H.264 / HEVC data) that have no container at all (e.g. from cameras).
  • get_image_metadata(): read an image's metadata without decoding it.

Low-level decoding APIs are now stable

The low-level APIs expose the three stages of a decoding pipeline in different objects, for video and audio. These APIs are no longer Beta. There are no breaking changes compared to 0.17. The only change is the recommended way to create a decoder: build it explicitly from the stream's codec parameters, with VideoPacketDecoder(stream.codec_parameters), rather than with stream.make_decoder(). stream.make_decoder() is still supported for backward compatibility.

from torchcodec.decoders import ColorConverter, Demuxer, VideoPacketDecoder

demuxer = Demuxer("video.mp4", streams="video")
video_stream = demuxer.streams[0]
decoder = VideoPacketDecoder(video_stream.codec_parameters, device="cuda")
converter = ColorConverter(device="cuda")

# Each of these three stages can run on its own thread:
for packet in demuxer:                        # demuxing, on CPU
    for raw_frame in decoder.decode(packet):  # decoding, on GPU
        frame = converter.convert(raw_frame)  # color conversion, on GPU

VideoDecoder and AudioDecoder are unchanged, and they remain the easiest way to decode video and audio.

Check out our low-level API tutorials to get started. We also wrote a new hill-climbing tutorial: the low-level APIs expose a lot of performance knobs, which makes them a great fit for auto-tuning with a coding agent. Our agent ended up 1.4–1.9x faster than a simple baseline, and up to 1.36x faster than an equally tuned VideoDecoder. The best knobs will be different on your setup, so we encourage you to hill-climb your own!

Per-clip hill-climbs

Demuxer-free and container-free decoding

A VideoPacketDecoder or AudioPacketDecoder doesn't need a Demuxer anymore: all it needs are the stream's codec parameters (VideoCodecParameters or AudioCodecParameters), and Packet objects. You can build both yourself, which means you can decode packets that were demuxed ahead of time and stored in a database, received over the network, or parsed by your own container reader:

from torchcodec.decoders import ColorConverter, Packet, VideoCodecParameters, VideoPacketDecoder

codec_parameters = VideoCodecParameters(
    "h264",
    time_base=time_base,
    extradata=extradata,
    width=width,
    height=height,
    pixel_format="yuv420p",
)
decoder = VideoPacketDecoder(codec_parameters, device="cuda")
converter = ColorConverter(device="cuda")

for data, pts, flags in rows_from_your_database:
    packet = Packet.from_bytes(data, pts=pts, flags=flags)
    for raw_frame in decoder.decode(packet):
        frame = converter.convert(raw_frame)

Learn more in our demuxer-free decoding tutorial, and in the multi-process section of the pipelines tutorial.

Image metadata

The new get_image_metadata() allows you to retrieve image metadata without paying for a full decode:

from torchcodec.decoders import get_image_metadata

metadata = get_image_metadata("image.jpg")
# ImageMetadata(format='jpeg', width=..., height=..., num_channels=..., bit_depth=..., num_frames=..., rotation=...)

Other improvements

Decoding

  • Faster Packet field access, especially in multi-threaded pipelines on free-threaded Python.
  • EXIF orientation coverage: we now read the orientation in more cases, matching Pillow's ImageOps.exif_transpose().

Packaging and arch support

  • One wheel for all Python versions: our wheels are now py3-none, so a single wheel per platform works on every supported Python version, including free-threaded builds. At the moment, TorchCodec support Python 3.11 to 3.15(t).
  • CUDA 13.2 and 13.4 wheels, matching torch 2.15.

Bug Fixes

  • Video decoding:
    • Fixed wrong frames being returned by index-based APIs after calling get_frame_played_at() or other timestamp-based APIs (#1865).
    • Fixed CPU decoding of videos with 'reserved' color metadata on FFmpeg 8+ (#1762).
  • CUDA decoding: fixed NVDEC sometimes returning a later frame instead of the requested one when decoding sequentially (#1758).
  • Audio decoding:
    • Fixed AudioDecoder silently dropping samples when packets have no pts (e.g. MS-ADPCM WAV files), or when the first frame has a zero pts and duration (#1768).
    • get_samples_played_in_range() no longer drops the frame containing start_seconds or stop_seconds on streams with a coarse time base, like Matroska (#1827).
    • Fixed a sample being returned twice when a chunk boundary falls exactly in the middle of a sample (#1826).
    • Fixed the number of channels of the output of get_samples_played_in_range() when start_seconds == stop_seconds and num_channels is set (#1856).
    • Fixed WavDecoder on float WAV files with a WAVE_FORMAT_EXTENSIBLE header (#1857).
  • Audio encoding: bit_rate=None now keeps the codec's default bit rate instead of setting it to 0 (#1863).
  • Low-level APIs: fixed a potential out-of-bounds access in scan() (#1776).
  • Memory leak: avoid a memory leak on every frame and packet with torch 2.11–2.13 (#1834). This is a torch bug, not a torchcodec bug.
  • CUDA JPEG decoding: fixed corrupted images in decode_jpeg batches on GPUs without a hardware JPEG engine (#1803).
  • GIF: fixed a heap buffer over-read in the GIF decoder (#1764).
  • JPEG EXIF orientation: fixed the orientation being ignored when an XMP segment comes before the EXIF segment (#1797).

Backward-incompatible changes

  • Exceptions raised by the read(), write() or seek() methods of a user's file-like object now surface as RuntimeError. The message contains the original error (#1837). This was a necessary trade-off to enable py3-none wheels (see above).