fix nvidia in docker; minimize nvenc sessions (#2460)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
namespace ErsatzTV.FFmpeg.Capabilities.Nvidia;
|
||||
|
||||
public record CudaDevice(int Handle, string Model, Version Version);
|
||||
public record CudaDevice(int Handle, string Model, Version Version, IReadOnlyList<CudaCodec> Codecs);
|
||||
|
||||
public record CudaCodec(string Name, Guid CodecGuid, IReadOnlyList<Guid> ProfileGuids, IReadOnlyList<int> BitDepths);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using ErsatzTV.FFmpeg.Format;
|
||||
@@ -62,19 +63,71 @@ internal static class CudaHelper
|
||||
|
||||
foreach (var description in CuDevice.GetDescriptions())
|
||||
{
|
||||
var device = description.Device;
|
||||
|
||||
string name = device.GetName();
|
||||
int nullIndex = name.IndexOf('\0');
|
||||
if (nullIndex > 0)
|
||||
try
|
||||
{
|
||||
name = name[..nullIndex];
|
||||
var device = description.Device;
|
||||
|
||||
string name = device.GetName();
|
||||
int nullIndex = name.IndexOf('\0');
|
||||
if (nullIndex > 0)
|
||||
{
|
||||
name = name[..nullIndex];
|
||||
}
|
||||
|
||||
int major = device.GetAttribute(CuDeviceAttribute.ComputeCapabilityMajor);
|
||||
int minor = device.GetAttribute(CuDeviceAttribute.ComputeCapabilityMinor);
|
||||
|
||||
using var context = device.CreateContext();
|
||||
var sessionParams = new NvEncOpenEncodeSessionExParams
|
||||
{
|
||||
Version = LibNvEnc.NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER,
|
||||
ApiVersion = LibNvEnc.NVENCAPI_VERSION,
|
||||
Device = context.Handle,
|
||||
DeviceType = NvEncDeviceType.Cuda
|
||||
};
|
||||
|
||||
var codecs = new List<CudaCodec>();
|
||||
var encoder = LibNvEnc.OpenEncoder(ref sessionParams);
|
||||
try
|
||||
{
|
||||
IReadOnlyList<Guid> codecGuids = encoder.GetEncodeGuids();
|
||||
foreach ((string codecName, Guid codecGuid) in AllCodecs)
|
||||
{
|
||||
if (codecGuids.Contains(codecGuid))
|
||||
{
|
||||
IReadOnlyList<Guid> codecProfileGuids = encoder.GetEncodeProfileGuids(codecGuid);
|
||||
|
||||
var bitDepths = new List<int> { 8 };
|
||||
|
||||
var cap = new NvEncCapsParam { CapsToQuery = NvEncCaps.Support10bitEncode };
|
||||
var capsVal = 0;
|
||||
encoder.GetEncodeCaps(codecGuid, ref cap, ref capsVal);
|
||||
if (capsVal > 0)
|
||||
{
|
||||
bitDepths.Add(10);
|
||||
}
|
||||
|
||||
var cudaCodec = new CudaCodec(
|
||||
codecName,
|
||||
codecGuid,
|
||||
codecProfileGuids,
|
||||
bitDepths.ToImmutableList());
|
||||
|
||||
codecs.Add(cudaCodec);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
encoder.DestroyEncoder();
|
||||
}
|
||||
|
||||
result.Add(new CudaDevice(device.Handle, name, new Version(major, minor), codecs));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
int major = device.GetAttribute(CuDeviceAttribute.ComputeCapabilityMajor);
|
||||
int minor = device.GetAttribute(CuDeviceAttribute.ComputeCapabilityMinor);
|
||||
|
||||
result.Add(new CudaDevice(device.Handle, name, new Version(major, minor)));
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -84,47 +137,15 @@ internal static class CudaHelper
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
try
|
||||
sb.AppendLine(" Encoding:");
|
||||
foreach (CudaCodec cudaCodec in device.Codecs)
|
||||
{
|
||||
var dev = CuDevice.GetDevice(device.Handle);
|
||||
using var context = dev.CreateContext();
|
||||
var sessionParams = new NvEncOpenEncodeSessionExParams
|
||||
{
|
||||
Version = LibNvEnc.NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER,
|
||||
ApiVersion = LibNvEnc.NVENCAPI_VERSION,
|
||||
Device = context.Handle,
|
||||
DeviceType = NvEncDeviceType.Cuda
|
||||
};
|
||||
sb.AppendLine(CultureInfo.InvariantCulture, $" - Supports {cudaCodec.Name} 8-bit");
|
||||
|
||||
var encoder = LibNvEnc.OpenEncoder(ref sessionParams);
|
||||
try
|
||||
if (cudaCodec.BitDepths.Contains(10))
|
||||
{
|
||||
sb.AppendLine(" Encoding:");
|
||||
IReadOnlyList<Guid> codecGuids = encoder.GetEncodeGuids();
|
||||
foreach ((string codecName, Guid codecGuid) in AllCodecs)
|
||||
{
|
||||
if (codecGuids.Contains(codecGuid))
|
||||
{
|
||||
sb.AppendLine(CultureInfo.InvariantCulture, $" - Supports {codecName} 8-bit");
|
||||
|
||||
var cap = new NvEncCapsParam { CapsToQuery = NvEncCaps.Support10bitEncode };
|
||||
var capsVal = 0;
|
||||
encoder.GetEncodeCaps(codecGuid, ref cap, ref capsVal);
|
||||
if (capsVal > 0)
|
||||
{
|
||||
sb.AppendLine(CultureInfo.InvariantCulture, $" - Supports {codecName} 10-bit");
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.AppendLine(CultureInfo.InvariantCulture, $" - Supports {cudaCodec.Name} 10-bit");
|
||||
}
|
||||
finally
|
||||
{
|
||||
encoder.DestroyEncoder();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
|
||||
@@ -15,7 +15,7 @@ public static class NvEncSharpRedirector
|
||||
if (libraryName.Equals("nvEncodeAPI64.dll", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return NativeLibrary.Load("libnvidia-encode.so", assembly, searchPath);
|
||||
return NativeLibrary.Load("libnvidia-encode.so.1", assembly, searchPath);
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return NativeLibrary.Load("nvEncodeAPI64.dll", assembly, searchPath);
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public static class NvEncSharpRedirector
|
||||
if (libraryName.Equals("nvEncodeAPI.dll", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return NativeLibrary.Load("libnvidia-encode.so", assembly, searchPath);
|
||||
return NativeLibrary.Load("libnvidia-encode.so.1", assembly, searchPath);
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return NativeLibrary.Load("nvEncodeAPI.dll", assembly, searchPath);
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public static class NvEncSharpRedirector
|
||||
if (libraryName.Equals("nvcuda.dll", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return NativeLibrary.Load("libcuda.so", assembly, searchPath);
|
||||
return NativeLibrary.Load("libcuda.so.1", assembly, searchPath);
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return NativeLibrary.Load("nvcuda.dll", assembly, searchPath);
|
||||
}
|
||||
|
||||
@@ -95,92 +95,55 @@ public class NvidiaHardwareCapabilities : IHardwareCapabilities
|
||||
{
|
||||
int bitDepth = maybePixelFormat.Map(pf => pf.BitDepth).IfNone(8);
|
||||
|
||||
try
|
||||
_logger.LogDebug(
|
||||
"Checking NvEnc {Format} / {Profile} / {BitDepth}-bit",
|
||||
videoFormat,
|
||||
videoProfile,
|
||||
bitDepth);
|
||||
|
||||
var codec = _cudaDevice.Codecs.FirstOrDefault(c => c.Name.Equals(
|
||||
videoFormat,
|
||||
StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (codec == null)
|
||||
{
|
||||
var dev = CuDevice.GetDevice(0);
|
||||
using var context = dev.CreateContext();
|
||||
var sessionParams = new NvEncOpenEncodeSessionExParams
|
||||
{
|
||||
Version = LibNvEnc.NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER,
|
||||
ApiVersion = LibNvEnc.NVENCAPI_VERSION,
|
||||
Device = context.Handle,
|
||||
DeviceType = NvEncDeviceType.Cuda
|
||||
};
|
||||
|
||||
var encoder = LibNvEnc.OpenEncoder(ref sessionParams);
|
||||
try
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"Checking NvEnc {Format} / {Profile} / {BitDepth}-bit",
|
||||
videoFormat,
|
||||
videoProfile,
|
||||
bitDepth);
|
||||
|
||||
var codecGuid = videoFormat switch
|
||||
{
|
||||
VideoFormat.Hevc => NvEncCodecGuids.Hevc,
|
||||
_ => NvEncCodecGuids.H264
|
||||
};
|
||||
|
||||
IReadOnlyList<Guid> codecGuids = encoder.GetEncodeGuids();
|
||||
if (!codecGuids.Contains(codecGuid))
|
||||
{
|
||||
_logger.LogWarning("NvEnc {Format} is not supported; will use software encode", videoFormat);
|
||||
return FFmpegCapability.Software;
|
||||
}
|
||||
|
||||
var profileGuid = (videoFormat, videoProfile.IfNone(string.Empty), bitDepth) switch
|
||||
{
|
||||
(VideoFormat.Hevc, _, 8) => NvEncProfileGuids.HevcMain,
|
||||
(VideoFormat.Hevc, _, 10) => NvEncProfileGuids.HevcMain10,
|
||||
|
||||
(VideoFormat.H264, _, 10) => NvEncProfileGuids.H264High444,
|
||||
(VideoFormat.H264, VideoProfile.High, _) => NvEncProfileGuids.H264High,
|
||||
// high10 is for libx264, nvenc needs high444
|
||||
(VideoFormat.H264, VideoProfile.High10, _) => NvEncProfileGuids.H264High444,
|
||||
|
||||
_ => NvEncProfileGuids.H264Main
|
||||
};
|
||||
|
||||
IReadOnlyList<Guid> profileGuids = encoder.GetEncodeProfileGuids(codecGuid);
|
||||
if (!profileGuids.Contains(profileGuid))
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"NvEnc {Format} / {Profile} is not supported; will use software encode",
|
||||
videoFormat,
|
||||
videoProfile);
|
||||
return FFmpegCapability.Software;
|
||||
}
|
||||
|
||||
if (bitDepth == 10)
|
||||
{
|
||||
var cap = new NvEncCapsParam { CapsToQuery = NvEncCaps.Support10bitEncode };
|
||||
var capsVal = 0;
|
||||
encoder.GetEncodeCaps(codecGuid, ref cap, ref capsVal);
|
||||
if (capsVal == 0)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"NvEnc {Format} / {Profile} / {BitDepth}-bit is not supported; will use software encode",
|
||||
videoFormat,
|
||||
videoProfile,
|
||||
bitDepth);
|
||||
return FFmpegCapability.Software;
|
||||
}
|
||||
}
|
||||
|
||||
return FFmpegCapability.Hardware;
|
||||
}
|
||||
finally
|
||||
{
|
||||
encoder.DestroyEncoder();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Unexpected error checking NvEnc capabilities; falling back to software");
|
||||
_logger.LogWarning("NvEnc {Format} is not supported; will use software encode", videoFormat);
|
||||
return FFmpegCapability.Software;
|
||||
}
|
||||
|
||||
return FFmpegCapability.Software;
|
||||
var profileGuid = (videoFormat, videoProfile.IfNone(string.Empty), bitDepth) switch
|
||||
{
|
||||
(VideoFormat.Hevc, _, 8) => NvEncProfileGuids.HevcMain,
|
||||
(VideoFormat.Hevc, _, 10) => NvEncProfileGuids.HevcMain10,
|
||||
|
||||
(VideoFormat.H264, _, 10) => NvEncProfileGuids.H264High444,
|
||||
(VideoFormat.H264, VideoProfile.High, _) => NvEncProfileGuids.H264High,
|
||||
// high10 is for libx264, nvenc needs high444
|
||||
(VideoFormat.H264, VideoProfile.High10, _) => NvEncProfileGuids.H264High444,
|
||||
|
||||
_ => NvEncProfileGuids.H264Main
|
||||
};
|
||||
|
||||
if (!codec.ProfileGuids.Contains(profileGuid))
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"NvEnc {Format} / {Profile} is not supported; will use software encode",
|
||||
videoFormat,
|
||||
videoProfile);
|
||||
return FFmpegCapability.Software;
|
||||
}
|
||||
|
||||
if (!codec.BitDepths.Contains(bitDepth))
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"NvEnc {Format} / {Profile} / {BitDepth}-bit is not supported; will use software encode",
|
||||
videoFormat,
|
||||
videoProfile,
|
||||
bitDepth);
|
||||
return FFmpegCapability.Software;
|
||||
}
|
||||
|
||||
return FFmpegCapability.Hardware;
|
||||
}
|
||||
|
||||
public Option<RateControlMode> GetRateControlMode(string videoFormat, Option<IPixelFormat> maybePixelFormat) =>
|
||||
|
||||
Reference in New Issue
Block a user