Skip to content

nvencoder: Add support for dynamic GPU device selection

Seungha Yang requested to merge seungha.yang/gstreamer:nvenc-dyn-device into main
    cuda: Protect gst_cuda_ensure_element_context() with lock

    Serialize the order of context setup since it can be called
    from multiple threads
    nvencoder: Add support for dynamic GPU device selection

    Adding nvautogpu{h264,h265}enc class which will accept upstream logical
    GPU device object (GstCudaContext or GstD3D11Device) instead of
    using pre-assigned GPU instance.

    If upstream logical GPU device object is not NVENC compatible
    (e.g., D3D11 device of non-NVIDIA GPU) or it's system memory,
    then user specified "cuda-device-id" or "adapter-luid" property
    will be used for GPU device selection.

Reason why adding new class?

Existing hw elements prepare GPU resources via GstContext during null -> ready state change And it's designed to accept only pre-defined GPU instance. The GstContext will then be shared by elements in pipeline.

But a scenario where encoder uses upstream GPU device, the encoder element does not know which GPU would be selected and therefore prepared context during null -> ready may not be used (it will waste GPU memory).

To address it, this new element will delay opening GPU resource until the first buffer and

  • Use GPU object (GstCudaContext or GstD3D11Device) attached on the first buffer
  • Or select user specified GPU instance (system memory case)

One limitation of this approach (accept upstream GPU object) is that explicit load balancing is difficult. For example:

d3d11h264dec - tee + nvd3d11h264enc
                   + nvd3d11h265device1enc

the above pipeline using old elements will encode stream using each specified GPU (although copying happens on the second branch)

d3d11h264dec - tee + nvautogpuh264enc
                   + nvautogpuh265enc

but new element will use only single GPU by design. That might not be something wanted by application from load balancing point of view

thus separate encoder class is proposed in this MR (I do prefer defining new class over adding noisy properties to existing elements in this case)

Edited by Seungha Yang

Merge request reports