Rate this Page
★ ★ ★ ★ ★

Source code for torchcodec.decoders._blocks._packet_decoder

# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import annotations

import dataclasses
from fractions import Fraction
from typing import Any, Generic, TypeVar

import torch

from torchcodec._core.ops import (
    _blocks_audio_packet_decoder_receive_frame,
    _blocks_create_packet_decoder,
    _blocks_packet_decoder_receive_frame,
    _blocks_packet_decoder_reset,
    _blocks_packet_decoder_send_eof,
    _blocks_packet_decoder_send_packet,
)

from .._decoder_utils import convert_device_to_str
from ._codec_parameters import (
    _CodecParameters,
    AudioCodecParameters,
    VideoCodecParameters,
)
from ._frame import Packet, RawAudioSamples, RawFrame
from ._helpers import _bytes_to_tensor, _process_local


# TODO_API_BREAKDOWN DOC P1 revisit every single comment across all low-level APIs

_Decoded = TypeVar("_Decoded", RawFrame, RawAudioSamples)


@_process_local(
    "A codec context holds reference frames and entropy state that FFmpeg "
    "cannot serialise. Build one in each process, from the stream's "
    "codec_parameters: those can be pickled."
)
class _BasePacketDecoder(Generic[_Decoded]):
    _handle: torch.Tensor
    _drained: bool
    _generation: int | None

    def _init(
        self,
        codec_parameters: _CodecParameters,
        *,
        media_type: str,
        device_str: str,
        num_ffmpeg_threads: int | None = None,
    ) -> None:
        # The op takes the fields of the codec parameters under their own
        # names, except for coded_side_data, which it takes as two lists.
        kwargs: dict[str, Any] = {}
        for f in dataclasses.fields(codec_parameters):
            value = getattr(codec_parameters, f.name)
            if not f.init or value is None:
                continue
            if isinstance(value, Fraction):
                value = [value.numerator, value.denominator]
            elif isinstance(value, bytes):
                value = _bytes_to_tensor(value)
            elif f.name == "coded_side_data":
                kwargs["coded_side_data_names"] = list(value)
                kwargs["coded_side_data_values"] = [
                    _bytes_to_tensor(data) for data in value.values()
                ]
                continue
            kwargs[f.name] = value
        if codec_parameters._container_format_PRIVATE_DO_NOT_USE is not None:
            kwargs["container_format"] = (
                codec_parameters._container_format_PRIVATE_DO_NOT_USE
            )

        self._handle = _blocks_create_packet_decoder(
            media_type,
            num_threads=num_ffmpeg_threads,
            device=device_str,
            **kwargs,
        )
        self._drained = False
        # The demuxer position these packets come from. None until the first
        # packet, and again after every reset(), so it is adopted rather than
        # tracked: the decoder never needs a reference back to the demuxer.
        self._generation = None

    def _receive_ready_frames(self) -> list[_Decoded]:
        raise NotImplementedError

    def decode(self, packet: Packet) -> list[_Decoded]:
        if self._drained:
            raise RuntimeError(
                "This decoder has been drained, and a codec that has been told "
                "the stream ended ignores any further packet. Create a new "
                "decoder to decode another stream."
            )
        if self._generation is None:
            self._generation = packet._generation
        elif self._generation != packet._generation:
            raise RuntimeError(
                "The demuxer seeked since this decoder was last reset(), so "
                "this packet is from a position the codec knows nothing about "
                "- decoding it would produce plausible-looking garbage. Call "
                "reset() on every decoder fed by that demuxer after a seek."
            )
        status = _blocks_packet_decoder_send_packet(self._handle, packet._handle)
        if status < 0:
            raise RuntimeError(f"Failed to send packet to decoder (status {status})")
        return self._receive_ready_frames()

    def drain(self) -> list[_Decoded]:
        _blocks_packet_decoder_send_eof(self._handle)
        frames = self._receive_ready_frames()
        self._drained = True
        return frames

    def reset(self) -> None:
        """Drop the codec's buffered state and start over.

        Needed after a :meth:`Demuxer.seek`, and after :meth:`drain`.
        """
        _blocks_packet_decoder_reset(self._handle)
        self._drained = False
        self._generation = None


[docs] class VideoPacketDecoder(_BasePacketDecoder[RawFrame]): """Decodes the compressed :class:`Packet`\\ s of one video stream into :class:`RawFrame`\\ s. This is a low-level API: for straightforward decoding, use :class:`~torchcodec.decoders.VideoDecoder` instead. Build it from the :class:`VideoCodecParameters` of the stream to decode, then feed it one packet at a time, and drain it at the end:: demuxer = Demuxer("video.mp4") decoder = VideoPacketDecoder(demuxer.streams[0].codec_parameters) for packet in demuxer: for raw_frame in decoder.decode(packet): ... for raw_frame in decoder.drain(): ... The codec parameters can also be pickled, to decode in another process than the demuxer, or built by hand, to decode packets that don't come from a :class:`Demuxer` (see :ref:`sphx_glr_generated_examples_low_level_demuxer_free.py`). Frames come out on the device the decoder was built for. It is stateful. It holds the codec's reference-frame buffer, so it expects the packets of its own stream, in the order the demuxer produced them. Args: codec_parameters (VideoCodecParameters): The parameters of the stream to decode, e.g. :attr:`VideoStream.codec_parameters`. device (str or torch.device, optional): The device to decode on (cpu or CUDA). If ``None`` (default), the current default device is used (see ``torch.set_default_device``). With codec parameters you built yourself, CUDA needs some fields to be set: see :class:`VideoCodecParameters`. num_ffmpeg_threads (int, optional): The number of threads to use for CPU decoding. This has no effect when decoding on GPU. Use 1 for single-threaded decoding, which may be best if you are decoding multiple streams in parallel. Use a higher number for multi-threaded decoding, which is best for a single stream. Passing 0 lets FFmpeg decide on the number of threads. Default: 1. """ def __init__( self, codec_parameters: VideoCodecParameters, device: str | torch.device | None = None, *, num_ffmpeg_threads: int = 1, ): if not isinstance(codec_parameters, VideoCodecParameters): raise TypeError( "codec_parameters must be a VideoCodecParameters, got " f"{type(codec_parameters).__name__}." ) if num_ffmpeg_threads is None: raise ValueError(f"{num_ffmpeg_threads = } should be an int.") self._init( codec_parameters, media_type="video", device_str=convert_device_to_str(device), num_ffmpeg_threads=num_ffmpeg_threads, ) # methods calling super() only to pin the return type down to RawFrame. The # base class is generic over _Decoded, which isn't ideal for the rendered # docs.
[docs] def decode(self, packet: Packet) -> list[RawFrame]: """Send one :class:`Packet` to the codec and return the :class:`RawFrame`\\ s that are ready. **This can return zero, one, or more than one** :class:`RawFrame`. What comes back is not the decoding of the packet you just passed: a codec that is buffering B-frames, or still priming itself, will emit what it owes you on a later call. Args: packet (Packet): A packet of this decoder's own stream. Returns: The possibly empty list of :class:`RawFrame`\\ s that the codec has ready, in presentation order. Raises: RuntimeError: If this decoder has been drained, or if the demuxer seeked without it being :meth:`reset` afterwards. """ return super().decode(packet)
[docs] def drain(self) -> list[RawFrame]: """Tell the codec the stream has ended, and return the :class:`RawFrame`\\ s it was still holding. Skipping this loses the tail of the stream. A drained decoder refuses any further packet; :meth:`reset` makes it usable again. Returns: The possibly empty list of :class:`RawFrame`\\ s that the codec was still holding, in presentation order. """ return super().drain()
def _receive_ready_frames(self) -> list[RawFrame]: frames = [] while True: handle, status, pts, pts_seconds, duration_seconds, storage = ( _blocks_packet_decoder_receive_frame(self._handle) ) if status != 0: # EAGAIN (need more packets) or EOF: nothing ready break frames.append( RawFrame( handle, pts, pts_seconds, duration_seconds, storage=storage if storage.numel() > 0 else None, ) ) return frames
[docs] class AudioPacketDecoder(_BasePacketDecoder[RawAudioSamples]): """Decodes the compressed :class:`Packet`\\ s of one audio stream into :class:`RawAudioSamples`. This is a low-level API: for straightforward decoding, use :class:`~torchcodec.decoders.AudioDecoder` instead. Build it from the :class:`AudioCodecParameters` of the stream to decode, then feed it one packet at a time, and drain it at the end:: demuxer = Demuxer("audio.mp3", streams="audio") decoder = AudioPacketDecoder(demuxer.streams[0].codec_parameters) for packet in demuxer: for raw_samples in decoder.decode(packet): ... for raw_samples in decoder.drain(): ... The codec parameters can also be pickled, to decode in another process than the demuxer, or built by hand, to decode packets that don't come from a :class:`Demuxer` (see :ref:`sphx_glr_generated_examples_low_level_demuxer_free.py`). Audio is always decoded on the CPU. It is stateful. A lossy codec carries state from one frame to the next, so it expects the packets of its own stream, in the order the demuxer produced them. After a :meth:`Demuxer.seek`, :meth:`reset` is necessary but not sufficient: the first samples that come out are subtly wrong until the codec re-primes, so decode a margin before your target and throw it away. Args: codec_parameters (AudioCodecParameters): The parameters of the stream to decode, e.g. :attr:`AudioStream.codec_parameters`. """ def __init__(self, codec_parameters: AudioCodecParameters): if not isinstance(codec_parameters, AudioCodecParameters): raise TypeError( "codec_parameters must be an AudioCodecParameters, got " f"{type(codec_parameters).__name__}." ) # No num_ffmpeg_threads: audio is always decoded on a single thread. self._init(codec_parameters, media_type="audio", device_str="cpu") # See VideoPacketDecoder: pinning the return type down to RawAudioSamples.
[docs] def decode(self, packet: Packet) -> list[RawAudioSamples]: """Send one :class:`Packet` to the codec and return the :class:`RawAudioSamples` that are ready. **This can return zero, one, or more than one** :class:`RawAudioSamples`. What comes back is not the decoding of the packet you just passed: a codec that is still priming itself will emit what it owes you on a later call. Args: packet (Packet): A packet of this decoder's own stream. Returns: The possibly empty list of :class:`RawAudioSamples` that the codec has ready, in presentation order. Raises: RuntimeError: If this decoder has been drained, or if the demuxer seeked without it being :meth:`reset` afterwards. """ return super().decode(packet)
[docs] def drain(self) -> list[RawAudioSamples]: """Tell the codec the stream has ended, and return the :class:`RawAudioSamples` it was still holding. Skipping this loses the tail of the stream. A drained decoder refuses any further packet; :meth:`reset` makes it usable again. Returns: The possibly empty list of :class:`RawAudioSamples` that the codec was still holding, in presentation order. """ return super().drain()
def _receive_ready_frames(self) -> list[RawAudioSamples]: samples = [] while True: data, status, pts, pts_seconds, duration_seconds, sample_rate = ( _blocks_audio_packet_decoder_receive_frame(self._handle) ) if status != 0: # EAGAIN (need more packets) or EOF: nothing ready break samples.append( RawAudioSamples( data=data, sample_rate=sample_rate, pts=pts, pts_seconds=pts_seconds, duration_seconds=duration_seconds, # Carried onward so AudioConverter can make the same check: # a seek invalidates the resampler's state too. _generation=self._generation or 0, ) ) return samples