Skip to content

Support pix_fmt="cuda" in video codec context. #2199

Closed

Description

@caffeinism

In the current implementation, the following error occurs during the process of initializing the codec context to encode a frame in GPU memory using nvenc with pix_fmt=“cuda”.

cc = av.CodecContext.create("h264_nvenc", "w")
cc.width = 864
cc.height = 1904
cc.time_base = fractions.Fraction(1, 24)
cc.framerate = fractions.Fraction(24, 1)
cc.pix_fmt = "cuda"
cc.bit_rate = 6 * 1024 * 1024
y = torch.zeros((1904, 864), dtype=torch.uint8, device="cuda")
uv = torch.zeros((952, 432, 2), dtype=torch.uint8, device="cuda")
frame = av.VideoFrame.from_dlpack((y, uv))
cc.encode(frame)
hw_frames_ctx must be set when using GPU frames as input
Invalid argument: 'avcodec_open2("h264_nvenc", {})' returned 22

When pix_fmt=“nv12” is applied, it is presumed that the encoder copies the data to the CPU and then back to the GPU. To prevent this, I would like to modify the code so that when pix_fmt=“cuda”, it executes via the correct path.

Activity

  1. nagadomi commented on Mar 18, 2026

    @nagadomi

    As a possibly related issue, I have been experimenting with HWAccel recently and have run into a limitation with pix_fmt="cuda".
    With frame.format.name="cuda", it is not possible to determine the underlying format, such as nv12, yuv444p, or p010le.
    Should sw_format be exposed?
    This information should also be required when creating hwframes for encoding.

  2. added a commit that references this issue on Jun 24, 2026
    67af1be
  3. added a commit that references this issue on Jun 24, 2026
    42c1644
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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