Skip to content

vllm.multimodal.video_decoders.pynvvideocodec

Classes:

PyNvVideoCodecDecoderSlot

A retained PyNv decoder slot and its CUDA stream.

The decoder is reused across requests: reconfigure_decoder repoints the existing decoder at each new source instead of paying a fresh SimpleDecoder construction per request. Construction (CUVID parser + decoder + surface-pool allocation) is the dominant per-request cost, so reconfiguring is far cheaper. Decoder dimensions are bounded at construction so resolution changes do not replace its native frame pool. A single decoder serves both metadata (len/get_stream_metadata) and frame decode -- no separate metadata decoder.

Methods:

  • recover –

    Release decoder resources and drain work after a failed request.

  • reset –

    Rebuild this slot after draining only its CUDA stream.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
class PyNvVideoCodecDecoderSlot:
    """A retained PyNv decoder slot and its CUDA stream.

    The decoder is reused across requests: `reconfigure_decoder` repoints the
    existing decoder at each new source instead of paying a fresh
    `SimpleDecoder` construction per request. Construction (CUVID parser +
    decoder + surface-pool allocation) is the dominant per-request cost, so
    reconfiguring is far cheaper. Decoder dimensions are bounded at construction
    so resolution changes do not replace its native frame pool. A single decoder
    serves both metadata (`len`/`get_stream_metadata`) and frame decode -- no
    separate metadata decoder.
    """

    def __init__(self, stream) -> None:
        self.stream = stream
        self.decoder = None
        self.source_path: str | None = None
        self.max_width = 0
        self.max_height = 0

    def invalidate(self) -> None:
        decoder = self.decoder
        self.decoder = None
        self.source_path = None
        self.max_width = 0
        self.max_height = 0
        if decoder is not None:
            with suppress(Exception):
                decoder.stop()

    def reset(self) -> None:
        """Rebuild this slot after draining only its CUDA stream."""
        with suppress(Exception):
            self.stream.synchronize()
        self.invalidate()

    def recover(self) -> None:
        """Release decoder resources and drain work after a failed request."""
        self.reset()

        with suppress(Exception):
            import torch

            with torch.accelerator.device_index(self.stream.device.index):
                torch.accelerator.synchronize()
                torch.accelerator.empty_cache()

    def _construct(
        self,
        file_path: str,
        nvc,
        device_index: int,
        max_width: int,
        max_height: int,
    ) -> None:
        decoder = nvc.SimpleDecoder(
            file_path,
            output_color_type=nvc.OutputColorType.RGB,
            use_device_memory=True,
            need_scanned_stream_metadata=True,
            gpu_id=device_index,
            cuda_stream=self.stream.cuda_stream,
            decoder_cache_size=PYNVVIDEOCODEC_DECODER_CACHE_SIZE,
            max_width=max_width,
            max_height=max_height,
        )
        self.decoder = decoder
        self.source_path = file_path
        self.max_width = max_width
        self.max_height = max_height

    def get_decoder(
        self,
        file_path: str,
        nvc,
        device_index: int,
        max_width: int,
        max_height: int,
    ):
        if self.decoder is None:
            self._construct(file_path, nvc, device_index, max_width, max_height)
        elif self.source_path != file_path:
            if max_width > self.max_width or max_height > self.max_height:
                self.reset()
                self._construct(file_path, nvc, device_index, max_width, max_height)
            else:
                try:
                    self.decoder.reconfigure_decoder(file_path)
                    self.source_path = file_path
                except Exception:
                    # reconfigure unsupported/unsafe for this source -> rebuild.
                    self.reset()
                    self._construct(file_path, nvc, device_index, max_width, max_height)
        return self.decoder

recover()

Release decoder resources and drain work after a failed request.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
def recover(self) -> None:
    """Release decoder resources and drain work after a failed request."""
    self.reset()

    with suppress(Exception):
        import torch

        with torch.accelerator.device_index(self.stream.device.index):
            torch.accelerator.synchronize()
            torch.accelerator.empty_cache()

reset()

Rebuild this slot after draining only its CUDA stream.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
def reset(self) -> None:
    """Rebuild this slot after draining only its CUDA stream."""
    with suppress(Exception):
        self.stream.synchronize()
    self.invalidate()

PyNvVideoCodecSourceMetadata

Bases: NamedTuple

Metadata needed before GPU video decode.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
class PyNvVideoCodecSourceMetadata(NamedTuple):
    """Metadata needed before GPU video decode."""

    source: VideoSourceMetadata
    width: int
    height: int
    key_frame_indices: tuple[int, ...] = ()

PyNvVideoCodecVideoBackendMixin

PyNvVideoCodec utilities for GPU-backed frame decode.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
class PyNvVideoCodecVideoBackendMixin:
    """PyNvVideoCodec utilities for GPU-backed frame decode."""

    _DEVICE_INDEX: ClassVar[int] = 0

    @classmethod
    def _create_decoder_slot(cls) -> PyNvVideoCodecDecoderSlot:
        import torch

        return PyNvVideoCodecDecoderSlot(torch.cuda.Stream(device=cls._DEVICE_INDEX))

    @classmethod
    def _configure_decoder_slots(cls, hw_decoders: object) -> None:
        hw_decoders = validate_pynvvideocodec_hw_decoders(hw_decoders)
        _pynv_decoder_pool.configure(hw_decoders)

    @staticmethod
    @contextmanager
    def _torch_stream_context(stream):
        import torch

        torch.accelerator.set_device_index(stream.device.index)
        previous_stream = torch.accelerator.current_stream()
        torch.accelerator.set_stream(stream)
        try:
            yield
        finally:
            torch.accelerator.set_stream(previous_stream)

    @classmethod
    @contextmanager
    def _borrow_decoder_slot(cls):
        pool = _pynv_decoder_pool
        create_slot = False
        with pool.cond:
            if pool.max_slots is None:
                raise RuntimeError("PyNvVideoCodec decoder slots are not configured")
            while True:
                if pool.slots:
                    slot = pool.slots.pop()
                    break
                if pool.active < pool.max_slots:
                    pool.active += 1
                    create_slot = True
                    break
                pool.cond.wait()

        if create_slot:
            try:
                slot = cls._create_decoder_slot()
            except Exception:
                with pool.cond:
                    pool.active -= 1
                    pool.cond.notify()
                raise

        borrow_succeeded = False
        try:
            yield slot
            borrow_succeeded = True
        finally:
            if not borrow_succeeded:
                slot.recover()
            with pool.cond:
                if borrow_succeeded:
                    pool.slots.append(slot)
                else:
                    pool.active -= 1
                pool.cond.notify()

    @staticmethod
    def _metadata_value(metadata, *names: str, default=None):
        for name in names:
            value = getattr(metadata, name, None)
            if value is not None:
                return value
        return default

    @classmethod
    def _validate_decoder_caps(
        cls,
        file_path: str,
        nvc,
        max_width: int | None = None,
        max_height: int | None = None,
    ) -> tuple[int, int]:
        demuxer = nvc.CreateDemuxer(file_path)
        width = demuxer.Width()
        height = demuxer.Height()
        caps = _get_decoder_caps(
            nvc,
            cls._DEVICE_INDEX,
            demuxer.GetNvCodecId(),
            demuxer.ChromaFormat(),
            demuxer.BitDepth(),
        )
        width_min = int(caps["width_min"])
        width_max = int(caps["width_max"])
        height_min = int(caps["height_min"])
        height_max = int(caps["height_max"])
        mb_num_max = int(caps["mb_num_max"])
        macroblock_count = ((width + 15) // 16) * ((height + 15) // 16)
        if (
            not caps["supported"]
            or width < width_min
            or width > width_max
            or height < height_min
            or height > height_max
            or macroblock_count > mb_num_max
        ):
            raise ValueError("Invalid or unsupported video file.")

        if max_width is None or max_height is None:
            max_dimension = max(width, height)
            orientation_mb_count = ((max_dimension + 15) // 16) ** 2
            if (
                max_dimension <= width_max
                and max_dimension <= height_max
                and orientation_mb_count <= mb_num_max
                and _decoder_surface_bytes(max_dimension, max_dimension)
                <= PYNVVIDEOCODEC_DECODER_GPU_MEMORY_BYTES
            ):
                return max_dimension, max_dimension
            return width, height

        decoder_max_width = min(max_width, width_max)
        decoder_max_height = min(max_height, height_max)
        decoder_mb_count = ((decoder_max_width + 15) // 16) * (
            (decoder_max_height + 15) // 16
        )
        if decoder_mb_count > mb_num_max:
            decoder_max_width = width
            decoder_max_height = height
        if width > decoder_max_width or height > decoder_max_height:
            raise ValueError("Invalid or unsupported video file.")
        return decoder_max_width, decoder_max_height

    @classmethod
    def _read_source_metadata(
        cls,
        file_path: str,
        nvc,
        max_width: int,
        max_height: int,
        *,
        include_keyframes: bool = False,
    ) -> PyNvVideoCodecSourceMetadata:
        with cls._borrow_decoder_slot() as decoder_slot:
            with cls._torch_stream_context(decoder_slot.stream):
                decoder = decoder_slot.get_decoder(
                    file_path,
                    nvc,
                    device_index=cls._DEVICE_INDEX,
                    max_width=max_width,
                    max_height=max_height,
                )
                key_frame_indices: tuple[int, ...] = ()
                try:
                    metadata = decoder.get_stream_metadata()
                    total_frames_num = len(decoder)
                    # Only scan the entire stream if finding keyframes.
                    if include_keyframes:
                        scanned = decoder.get_scanned_stream_metadata()
                        key_frame_indices = tuple(
                            int(index)
                            for index in cls._metadata_value(
                                scanned, "key_frame_indices", default=()
                            )
                        )
                finally:
                    del decoder
            width = int(cls._metadata_value(metadata, "width", default=0))
            height = int(cls._metadata_value(metadata, "height", default=0))
            original_fps = float(
                cls._metadata_value(
                    metadata,
                    "average_fps",
                    "avg_frame_rate",
                    "frame_rate",
                    "frameRate",
                    default=0.0,
                )
            )
            duration = float(
                cls._metadata_value(metadata, "duration", default=0.0)
                or (total_frames_num / original_fps if original_fps > 0 else 0.0)
            )
            if total_frames_num <= 0:
                raise ValueError("Could not determine video frame count")
            if width <= 0 or height <= 0:
                raise ValueError("Could not determine video dimensions")
            return PyNvVideoCodecSourceMetadata(
                source=VideoSourceMetadata(total_frames_num, original_fps, duration),
                width=width,
                height=height,
                key_frame_indices=key_frame_indices,
            )

    @classmethod
    def _decode_to_pinned_host(
        cls,
        file_path: str,
        frame_idx: list[int],
        nvc,
        max_width: int,
        max_height: int,
    ) -> npt.NDArray:
        import torch

        if not frame_idx:
            return np.empty((0,), dtype=np.uint8)

        with cls._borrow_decoder_slot() as decoder_slot:
            stream = decoder_slot.stream
            with cls._torch_stream_context(stream):
                decoder = decoder_slot.get_decoder(
                    file_path,
                    nvc,
                    device_index=cls._DEVICE_INDEX,
                    max_width=max_width,
                    max_height=max_height,
                )
                host_frames = None
                decoded_count = 0
                for start in range(0, len(frame_idx), PYNVVIDEOCODEC_DECODE_BATCH_SIZE):
                    chunk_indices = frame_idx[
                        start : start + PYNVVIDEOCODEC_DECODE_BATCH_SIZE
                    ]
                    invalid_video = False
                    try:
                        decoded_frames = decoder.get_batch_frames_by_index(
                            chunk_indices
                        )
                    except Exception as exc:
                        decoder = None
                        if not isinstance(
                            exc,
                            _pynvvideocodec_exception_types(nvc) + (IndexError,),
                        ):
                            raise
                        invalid_video = True
                    if invalid_video:
                        raise ValueError("Invalid or unsupported video file.")

                    torch_frames = [
                        torch.from_dlpack(frame) for frame in decoded_frames
                    ]
                    if not torch_frames:
                        stream.synchronize()
                        break
                    device_frames = _pynvvc_frames_to_nhwc(torch.stack(torch_frames))
                    if device_frames.ndim != 4:
                        raise ValueError(
                            "PyNvVideoCodec returned frames with unexpected shape "
                            f"{tuple(device_frames.shape)}"
                        )
                    if host_frames is None:
                        host_frames = torch.empty(
                            (len(frame_idx), *device_frames.shape[1:]),
                            dtype=device_frames.dtype,
                            device="cpu",
                            pin_memory=True,
                        )
                    elif tuple(device_frames.shape[1:]) != tuple(host_frames.shape[1:]):
                        raise ValueError(
                            "PyNvVideoCodec returned frames with inconsistent shapes"
                        )

                    next_count = decoded_count + len(device_frames)
                    host_frames[decoded_count:next_count].copy_(
                        device_frames, non_blocking=True
                    )
                    stream.synchronize()
                    decoded_count = next_count
                    del decoded_frames, torch_frames, device_frames

                    if decoded_count < start + len(chunk_indices):
                        break

                if decoded_count < len(frame_idx):
                    logger.warning(
                        "pynvvideocodec video loading: expected %d frames but got %d.",
                        len(frame_idx),
                        decoded_count,
                    )
                if host_frames is None:
                    return np.empty((0,), dtype=np.uint8)
                return host_frames[:decoded_count].numpy()

    @classmethod
    def decode_frames_pynvvideocodec(
        cls,
        loader_cls,
        data: bytes,
        target: VideoTargetMetadata,
        max_width: int | None = None,
        max_height: int | None = None,
        *,
        keyframe_sampling: str | None = None,
        **kwargs,
    ) -> tuple[npt.NDArray, VideoSourceMetadata, list[int], list[int]]:
        import PyNvVideoCodec as nvc

        from vllm.multimodal.gpu_ipc_memory import get_mm_gpu_ipc_pool

        temp_fd, temp_path = tempfile.mkstemp(suffix=".mp4")
        try:
            with os.fdopen(temp_fd, "wb") as temp_file:
                temp_file.write(data)

            invalid_video = False
            try:
                decoder_max_width, decoder_max_height = cls._validate_decoder_caps(
                    temp_path,
                    nvc,
                    max_width=max_width,
                    max_height=max_height,
                )
            except Exception as exc:
                if not isinstance(exc, _pynvvideocodec_exception_types(nvc)):
                    raise
                invalid_video = True
            if invalid_video:
                raise ValueError("Invalid or unsupported video file.")

            try:
                gpu_source = cls._read_source_metadata(
                    temp_path,
                    nvc,
                    decoder_max_width,
                    decoder_max_height,
                    include_keyframes=keyframe_sampling is not None,
                )
            except Exception as exc:
                if not isinstance(exc, _pynvvideocodec_exception_types(nvc)):
                    raise
                invalid_video = True
            if invalid_video:
                raise ValueError("Invalid or unsupported video file.")
            check_frame_pixel_limit(gpu_source.width, gpu_source.height)
            source = loader_cls._prepare_source(gpu_source.source)
            frame_idx = loader_cls.compute_frames_index_to_sample(
                source=source, target=target, **kwargs
            )
            if keyframe_sampling is not None:
                frame_idx = _sample_nearest_keyframes(
                    frame_idx,
                    gpu_source.key_frame_indices,
                    source.total_frames_num,
                )
            charge_sampled_frames(len(frame_idx), gpu_source.width, gpu_source.height)
            raw_frame_bytes = len(frame_idx) * gpu_source.height * gpu_source.width * 3
            pool = get_mm_gpu_ipc_pool()
            if pool is None or raw_frame_bytes == 0:
                frames = cls._decode_to_pinned_host(
                    temp_path,
                    frame_idx,
                    nvc,
                    decoder_max_width,
                    decoder_max_height,
                )
            else:
                with pool.acquire(raw_frame_bytes):
                    frames = cls._decode_to_pinned_host(
                        temp_path,
                        frame_idx,
                        nvc,
                        decoder_max_width,
                        decoder_max_height,
                    )
        finally:
            with suppress(FileNotFoundError):
                os.unlink(temp_path)

        valid_frame_indices = frame_idx[: int(frames.shape[0])]
        return frames, source, frame_idx, valid_frame_indices

_PyNvDecoderPool

Process-wide singleton managing PyNvVideoCodec decoder slot state.

Prevents subclass counter shadowing (GHSA-j682-9xp5-rrf3) by storing all mutable pool state in a single module-level instance rather than in ClassVar attributes that get shadowed by Python's augmented assignment semantics on subclasses.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
class _PyNvDecoderPool:
    """Process-wide singleton managing PyNvVideoCodec decoder slot state.

    Prevents subclass counter shadowing (GHSA-j682-9xp5-rrf3) by storing
    all mutable pool state in a single module-level instance rather than
    in ClassVar attributes that get shadowed by Python's augmented
    assignment semantics on subclasses.
    """

    def __init__(self) -> None:
        self.slots: list[PyNvVideoCodecDecoderSlot] = []
        self.active: int = 0
        self.cond: threading.Condition = threading.Condition()
        self.max_slots: int | None = None

    def configure(self, hw_decoders: int) -> None:
        with self.cond:
            if self.max_slots is None:
                self.max_slots = hw_decoders
            elif self.max_slots != hw_decoders:
                raise RuntimeError(
                    "PyNvVideoCodec decoder count is already configured as "
                    f"{self.max_slots}, got {hw_decoders}"
                )

_get_decoder_caps(nvc, device_index, codec, chroma_format, bit_depth) cached

nvc.GetDecoderCaps, cached per device and stream format.

Each call takes ~180 ms and holds the GIL, which stalls every other request in the API process; the result only depends on these arguments. Callers must not mutate the returned dict.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
@lru_cache(maxsize=PYNVVIDEOCODEC_DECODER_CAPS_CACHE_SIZE)
def _get_decoder_caps(
    nvc, device_index: int, codec, chroma_format, bit_depth: int
) -> dict[str, Any]:
    """`nvc.GetDecoderCaps`, cached per device and stream format.

    Each call takes ~180 ms and holds the GIL, which stalls every other request in
    the API process; the result only depends on these arguments. Callers must not
    mutate the returned dict.
    """
    return nvc.GetDecoderCaps(device_index, codec, chroma_format, bit_depth)

_pynvvc_frames_to_nhwc(frames)

Return a stacked PyNvVideoCodec frame batch as contiguous NHWC.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
def _pynvvc_frames_to_nhwc(frames):
    """Return a stacked PyNvVideoCodec frame batch as contiguous NHWC."""
    if frames.shape[-1] != 3 and frames.shape[-3] == 3:
        frames = frames.permute(0, 2, 3, 1)
    return frames.contiguous()

_sample_nearest_keyframes(uniform_indices, key_frame_indices, total_frames)

Snap uniform sample indices to the nearest keyframes.

Keyframes decode without walking their GOP. The result stays distinct and chronological; the uniform indices are returned unchanged when there are fewer usable keyframes than targets.

Source code in vllm/multimodal/video_decoders/pynvvideocodec.py
def _sample_nearest_keyframes(
    uniform_indices: list[int],
    key_frame_indices: tuple[int, ...],
    total_frames: int,
) -> list[int]:
    """Snap uniform sample indices to the nearest keyframes.

    Keyframes decode without walking their GOP. The result stays distinct and
    chronological; the uniform indices are returned unchanged when there are
    fewer usable keyframes than targets.
    """
    keyframes = sorted(
        {index for index in key_frame_indices if 0 <= index < total_frames}
    )
    count = len(uniform_indices)
    if count == 0 or len(keyframes) < count:
        return uniform_indices

    costs: list[float] = [
        (keyframe - uniform_indices[0]) ** 2 for keyframe in keyframes
    ]
    predecessors: list[list[int]] = []
    for target in uniform_indices[1:]:
        next_costs = [float("inf")] * len(keyframes)
        previous = [-1] * len(keyframes)
        best_cost = float("inf")
        best_index = -1
        for index, keyframe in enumerate(keyframes):
            if index and costs[index - 1] < best_cost:
                best_cost = costs[index - 1]
                best_index = index - 1
            if best_index >= 0:
                next_costs[index] = best_cost + (keyframe - target) ** 2
                previous[index] = best_index
        costs = next_costs
        predecessors.append(previous)

    final_index = min(range(len(keyframes)), key=lambda index: costs[index])
    sampled = [keyframes[final_index]]
    for previous in reversed(predecessors):
        final_index = previous[final_index]
        sampled.append(keyframes[final_index])
    sampled.reverse()
    return sampled