jlennox / NvEncSharp

MIT License
20 stars 7 forks source link

Just Feature Detection / Linux Notes #8

Open mitchcapper opened 1 year ago

mitchcapper commented 1 year ago

There was a note about linux testing so I wanted to post some limited results. Wanted to try and figure out what nvidia options were avail on a machine to get it working I did:

export DOTNET_ROOT="/tmp/dotnet"
wget https://dot.net/v1/dotnet-install.sh && bash dotnet-install.sh --channel STS  --install-dir "${DOTNET_ROOT}"  && ln -s "${DOTNET_ROOT}/dotnet" /usr/bin/
dotnet new console
dotnet add package Lennox.NvEncSharp

Then for an app

var ret = LibNvEnc.TryInitialize(out var err);
if (ret != LibNcEncInitializeStatus.Success)
    throw new Exception($"Unable to initialize nvidia {ret} {err}");
LibCuda.Initialize();
var descriptions = CuDevice.GetDescriptions().ToArray();
if (descriptions.Length == 0)
    throw new Exception("Cannot find any nvidia cuda devices");
var first = descriptions.First();
var dev = CuDevice.GetDevice(first.Device.Handle);
using var context = dev.CreateContext();
var ps = new NvEncOpenEncodeSessionExParams {
    Version = LibNvEnc.NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER,
    ApiVersion = LibNvEnc.NVENCAPI_VERSION,
    Device = context.Handle,
    DeviceType = NvEncDeviceType.Cuda
};
using var encoder = LibNvEnc.OpenEncoder(ref ps);
var cap = new NvEncCapsParam { CapsToQuery = NvEncCaps.SupportMultipleRefFrames };
var ret = 0;
encoder.GetEncodeCaps(NvEncCodecGuids.Hevc, ref cap, ref ret);

finally after running dotnet build I needed to link in the system libraries to match the names .net looked for:

ln -s /usr/lib/x86_64-linux-gnu/libnvidia-encode.so bin/Debug/net7.0/libnvEncodeAPI64.dll.so
ln -s /usr/lib/x86_64-linux-gnu/libcuda.so bin/Debug/net7.0/nvcuda.dll.so

I am not sure if there is value in switching NvEncoder to be a class without a parameterless constructor to make it a bit more obvious you can't just init it that way.