Skip to content

Beta CUDA backend fails to decode MPEG-4 Part 2 videos #1340

@ladhiet

Description

@ladhiet

🐛 Describe the bug

Description

VideoDecoder with set_cuda_backend("beta") fails immediately on any MPEG-4 Part 2 (mpeg4) video with "Requested next frame while there are no more frames left to decode" on the very first get_frame_at() call. H.264 videos work fine with the same setup. The "ffmpeg" CUDA backend and CPU decoding both handle MPEG-4 videos correctly.

Minimal reproducible example

import subprocess
import tempfile
from pathlib import Path

import torch
import torchcodec.decoders

# Generate a 1-second mpeg4 test video
tmpdir = tempfile.mkdtemp()
path = Path(tmpdir) / "test.mp4"
subprocess.run([
    "ffmpeg", "-y", "-f", "lavfi",
    "-i", "testsrc=duration=1:size=320x240:rate=30",
    "-c:v", "mpeg4", "-pix_fmt", "yuv420p", str(path),
], capture_output=True, check=True)

# CPU: works
dec = torchcodec.decoders.VideoDecoder(path.as_posix(), device="cpu")
dec.get_frame_at(0)  # OK

# Beta CUDA: fails
with torchcodec.decoders.set_cuda_backend("beta"):
    dec = torchcodec.decoders.VideoDecoder(path.as_posix(), device="cuda")
dec.get_frame_at(0)  # RuntimeError: no more frames left to decode

# FFmpeg CUDA: works
with torchcodec.decoders.set_cuda_backend("ffmpeg"):
    dec = torchcodec.decoders.VideoDecoder(path.as_posix(), device="cuda")
dec.get_frame_at(0)  # OK

Environment

Tested on two configurations, both reproduce the issue:

  • torchcodec 0.11.1+cu128 / torch 2.11.0+cu128
  • torchcodec 0.11.1+cu126 / torch 2.11.0+cu126
  • GPU: NVIDIA GeForce RTX 4090
  • OS: Linux

I was unable to test on CUDA 13 so this is probably the first thing to do with the above example.

From what I can see, the issue seems related to the flush that occurs on the first frame access. We noticed that BetaCudaDeviceInterface::flush() sends CUVID_PKT_ENDOFSTREAM to the NVCUVID parser before any data packets have been sent. For H.264, the h264_mp4toannexb bitstream filter injects SPS/PPS headers inline so the parser can recover after the flush. MPEG-4 Part 2 doesn't have an equivalent mechanism — its codec configuration (VOS/VOL headers) lives only in extradata and is never embedded in the bitstream, so the parser can't re-initialize.

Versions

PyTorch version: 2.11.0+cu128
Is debug build: False
CUDA used to build PyTorch: 12.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.1 LTS (x86_64)
GCC version: (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Clang version: Could not collect
CMake version: version 3.28.3
Libc version: glibc-2.39

Python version: 3.12.11 (main, Apr 8 2026, 13:55:11) [GCC 13.2.0] (64-bit runtime)
Python platform: Linux-6.8.0-59-generic-x86_64-with-glibc2.39
Is CUDA available: True
CUDA runtime version: 12.6.85
CUDA_MODULE_LOADING set to:
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 4090
Nvidia driver version: 570.133.20
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.9.5.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.5.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.5.1
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.5.1
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.5.1
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.5.1
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.5.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.5.1
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 48 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 32
On-line CPU(s) list: 0-31
Vendor ID: AuthenticAMD
Model name: AMD Ryzen 9 7950X 16-Core Processor
CPU family: 25
Model: 97
Thread(s) per core: 2
Core(s) per socket: 16
Socket(s): 1
Stepping: 2
Microcode version: 0xa601206
CPU(s) scaling MHz: 63%
CPU max MHz: 5881.0000
CPU min MHz: 400.0000
BogoMIPS: 8983.98
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd cppc amd_ibpb_ret arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid overflow_recov succor smca fsrm flush_l1d
Virtualization: AMD-V
L1d cache: 512 KiB (16 instances)
L1i cache: 512 KiB (16 instances)
L2 cache: 16 MiB (16 instances)
L3 cache: 64 MiB (2 instances)
NUMA node(s): 1
NUMA node0 CPU(s): 0-31
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec rstack overflow: Mitigation; Safe RET
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] numpy==2.4.3
[pip3] nvidia-cublas-cu12==12.8.4.1
[pip3] nvidia-cuda-cupti-cu12==12.8.90
[pip3] nvidia-cuda-nvrtc-cu12==12.8.93
[pip3] nvidia-cuda-runtime-cu12==12.8.90
[pip3] nvidia-cudnn-cu12==9.19.0.56
[pip3] nvidia-cufft-cu12==11.3.3.83
[pip3] nvidia-curand-cu12==10.3.9.90
[pip3] nvidia-cusolver-cu12==11.7.3.90
[pip3] nvidia-cusparse-cu12==12.5.8.93
[pip3] nvidia-cusparselt-cu12==0.7.1
[pip3] nvidia-nccl-cu12==2.28.9
[pip3] nvidia-nvjitlink-cu12==12.8.93
[pip3] nvidia-nvtx-cu12==12.8.90
[pip3] torch==2.11.0+cu128
[pip3] torchcodec==0.11.1+cu128
[pip3] torchvision==0.26.0+cu128
[pip3] triton==3.6.0
[conda] Could not collect

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions