Skip to content

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

@caffeinism

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.

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