Skip to content

Commit

Permalink
add nvidia hw accel on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Mantas-2155X committed Nov 4, 2022
1 parent 0d2783a commit e904f85
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 14 deletions.
4 changes: 2 additions & 2 deletions HardSubberGUI/HardSubberGUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<TrimMode>copyused</TrimMode>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<PackageVersion>1.0.1</PackageVersion>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
Expand Down
105 changes: 94 additions & 11 deletions HardSubberGUI/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class Tools
public static readonly List<string> SupportedVideoFormats = new () { ".mp4", ".m4v", ".mkv", ".avi" };
public static readonly List<Process> Processes = new ();

public static readonly GPU CurrentGPU = GetCurrentGPU();
public static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static string FfmpegPath = "";

Expand Down Expand Up @@ -233,12 +234,37 @@ public static string Getlspci()
var str = "";
while (!process.StandardOutput.EndOfStream)
{
str = process.StandardOutput.ReadLine();
str += process.StandardOutput.ReadLine();
}

return str ?? "";
}

public static string GetVideoController()
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
Arguments = "PATH Win32_videocontroller GET description",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};

process.StartInfo.FileName = "wmic";
process.Start();

var str = "";
while (!process.StandardOutput.EndOfStream)
{
str += process.StandardOutput.ReadLine();
}

return str ?? "";
}

public static bool IsOutdated()
{
const string lookFor = "https://github.com/Mantas-2155X/HardSubberGUI/releases/tag/";
Expand All @@ -260,14 +286,19 @@ public static bool IsOutdated()

return MainWindowViewModel.Version != s.Substring(tagStartIndex, tagEndIndex - tagStartIndex);
}
public static bool IsHardwareAccelerationSupported()

public static GPU GetCurrentGPU()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return false;
var data = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Getlspci() : GetVideoController();
Console.WriteLine("GetCurrentGPU: " + data);

var lspci = Getlspci();
return lspci.Contains("AMD");
if (data.Contains("AMD", StringComparison.InvariantCultureIgnoreCase) || data.Contains("Radeon", StringComparison.InvariantCultureIgnoreCase))
return GPU.AMD;

if (data.Contains("NVIDIA", StringComparison.InvariantCultureIgnoreCase))
return GPU.NVIDIA;

return GPU.None;
}

//https://dotnetcodr.com/2015/11/03/divide-an-integer-into-groups-with-c/
Expand Down Expand Up @@ -352,7 +383,35 @@ public static void ActFile(string file, string output, bool processVideo,
process.StartInfo.Arguments += "-hide_banner -loglevel warning -stats ";

if (hwaccel)
process.StartInfo.Arguments += "-vaapi_device /dev/dri/renderD128 ";
{
switch (CurrentGPU)
{
case GPU.AMD:
{
if (IsWindows)
{
// needs testing
}
else
{
process.StartInfo.Arguments += "-vaapi_device /dev/dri/renderD128 ";
}
break;
}
case GPU.NVIDIA:
{
if (IsWindows)
{
process.StartInfo.Arguments += "-vsync 0 ";
}
else
{
// needs testing
}
break;
}
}
}

process.StartInfo.Arguments += $"-i '{info.FullName}' ";
if (processVideo)
Expand All @@ -364,8 +423,25 @@ public static void ActFile(string file, string output, bool processVideo,

if (hwaccel)
{
process.StartInfo.Arguments += $"-filter_complex {scaleString}subtitles={escaped}:stream_index={subtitleIndex},format=nv12,hwupload ";
process.StartInfo.Arguments += "-c:v h264_vaapi ";
switch (CurrentGPU)
{
case GPU.AMD:
{
process.StartInfo.Arguments += $"-filter_complex {scaleString}subtitles={escaped}:stream_index={subtitleIndex},format=nv12,hwupload ";
process.StartInfo.Arguments += "-c:v h264_vaapi ";
break;
}
case GPU.NVIDIA:
{
process.StartInfo.Arguments += $"-filter_complex {scaleString}subtitles={escaped}:stream_index={subtitleIndex},format=nv12,hwupload_cuda ";
process.StartInfo.Arguments += "-c:v h264_nvenc ";
break;
}
case GPU.None:
{
throw new Exception("ERR_HWACCEL_NOTSUPPORTED");
}
}
}
else
{
Expand Down Expand Up @@ -408,7 +484,7 @@ public static void ActFile(string file, string output, bool processVideo,

process.StartInfo.Arguments += "-strict -2 ";
process.StartInfo.Arguments += $"'{output}/{shortName}{SupportedVideoFormats[format]}'";

if (!IsWindows)
{
var args = process.StartInfo.Arguments;
Expand All @@ -433,5 +509,12 @@ public static void ActFile(string file, string output, bool processVideo,
process.Start();
process.WaitForExit();
}

public enum GPU
{
None,
NVIDIA,
AMD
}
}
}
2 changes: 1 addition & 1 deletion HardSubberGUI/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public MainWindow()
if (args.Length == 2)
InputControl.Text = args[1];

HardwareAccelerationControl.IsEnabled = Tools.IsHardwareAccelerationSupported();
HardwareAccelerationControl.IsEnabled = Tools.CurrentGPU != Tools.GPU.None;

BackgroundTasks();
}
Expand Down

0 comments on commit e904f85

Please sign in to comment.