spdl.io.NvDecDecoder

class NvDecDecoder

Decodes video packets using NVDEC hardware acceleration.

Use nvdec_decoder() to instantiate.

This decoder supports two decoding workflows:

  1. Batch decoding - Decode all packets at once:

    decoder = spdl.io.nvdec_decoder(cuda_config, codec)
    nv12_buffer = decoder.decode_packets(packets)
    rgb = spdl.io.nv12_to_rgb(nv12_buffer, cuda_config)
    # Buffer shape: [num_frames, height*1.5, width]
    
  2. Streaming decoding - Process packets incrementally with batched output:

    decoder = spdl.io.nvdec_decoder(cuda_config, codec)
    decoder.init_buffer(num_frames)  # Initialize frame buffer
    
    for packets in packet_stream:
        for batch in decoder.streaming_decode_packets(packets):
            # batch is CUDABuffer with shape [num_frames, h*1.5, w]
            rgb = spdl.io.nv12_to_rgb(batch, cuda_config)
            # Process rgb frames
    
    # Flush and get remaining frames
    for batch in decoder.flush():
        rgb = spdl.io.nv12_to_rgb(batch, cuda_config)
        # Process final frames
    

Note

When feeding packets from concurrent demux workers, keep demux concurrency low (2–3 threads). See Demuxer for details.

Note

To decode H264 and HEVC videos, packets must be in Annex B format. Use apply_bsf() to convert:

if codec.name in ("h264", "hevc"):
    packets = spdl.io.apply_bsf(packets, f"{codec.name}_mp4toannexb")

Note

To decode many clips, cuda_decoders() is easier to use: one object per GPU manages the decoding threads and a shared decoder pool, and destroys evicted decoders off the decoding path. With warm decoders, both decode at the same speed for the same number of decoder threads. See CudaDecoders or NvDecDecoder?.

Changed in version 0.9.0:

  • AV1 videos are decoded on GPUs whose NVDEC supports AV1.

  • 10-bit and 12-bit 4:2:0 videos (e.g. HEVC Main10) are decoded into 8-bit NV12. Previously they failed.

  • The decoder keeps the underlying decoders of recently used codecs and reconfigures them for new resolutions, instead of re-creating them.

See also

Attributes

decode_packets

Decode all packets and return NV12 buffer.

flush

Flush the decoder and yield remaining batches.

init_buffer

Initialize frame buffer for streaming decode.

init_decoder

Initialize the decoder.

streaming_decode_packets

Streaming decode packets and yield batches.

decode_packets

Decode all packets and return NV12 buffer.

This method decodes all packets and flushes the decoder in one operation, and returns the resulting frames as one contiguous memory buffer.

Parameters:

packets – Video packets to decode.

Returns:

A CUDABuffer containing NV12 frames with shape [num_frames, h*1.5, width], where num_frames reflects the actual number of decoded frames, which should match the number of packets.

flush

Flush the decoder and yield remaining batches.

Call this method at the end of video stream to flush the decoder and retrieve any remaining buffered frames as batches.

Returns:

Iterator that yields remaining CUDABuffer batches in NV12 format.

init_buffer

Initialize frame buffer for streaming decode.

This must be called before using streaming_decode_packets() for streaming decode.

Parameters:

num_frames – The number of frames per batch.

init_decoder

Initialize the decoder.

Changed in version 0.2.0: This method was renamed from init().

Deprecated since version 0.1.7: This method was merged with nvdec_decoder(). Pass these parameters directly to initialize the decoder. The old pattern of calling decoder.init() after nvdec_decoder() will be removed in a future version.

Note

Creation and destruction of underlying decoder object is expensive. Typically, they take tens and hundreds of milliseconds respectively.

To mitigate this the implementation reuses the decoder. The decoder object keeps the underlying decoders of the recently used codecs (and bit depths), and reconfigures them when the resolution of the video changes.

Parameters:
  • cuda_config – The device configuration. Specifies the GPU of which video decoder chip is used, the CUDA memory allocator and CUDA stream used to fetch the result from the decoder engine.

  • codec – The information of the source video.

  • crop_left (int) – Optional: Crop the given number of pixels from each side.

  • crop_top (int) – Optional: Crop the given number of pixels from each side.

  • crop_right (int) – Optional: Crop the given number of pixels from each side.

  • crop_bottom (int) – Optional: Crop the given number of pixels from each side.

  • scale_width (int) – Optional: Resize the frame. Resizing is applied after cropping.

  • scale_height (int) – Optional: Resize the frame. Resizing is applied after cropping.

streaming_decode_packets

Streaming decode packets and yield batches.

This method decodes packets and yields batches of frames as they become ready. The frame buffer must be initialized with init_buffer() before calling this.

Parameters:

packets – Video packets to decode.

Returns:

Iterator that yields CUDABuffer batches in NV12 format.