Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Adds experimental AMD HEVC support
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cordeiro committed Dec 29, 2023
1 parent 62019a1 commit 292674c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Wasari.Cli/Commands/DownloadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public DownloadCommand(EnvironmentService environmentService, ILogger<DownloadCo

[CommandOption("nvenc", Description = "Use NVENC encoding for FFmpeg encoding (Nvidia only)")]
public bool UseNvenc { get; init; } = true;

[CommandOption("amf", Description = "Use AMF encoding for FFmpeg encoding (AMD only)")]
public bool UseAmf { get; init; } = true;

[CommandOption("dubs", Description = "Include all available dubs for each episode")]
public bool IncludeDubs { get; init; } = false;
Expand Down Expand Up @@ -157,6 +160,7 @@ public async ValueTask ExecuteAsync(IConsole console)
{
o.UseHevc = UseHevc;
o.UseNvidiaAcceleration = UseNvenc;
o.UseAmdAcceleration = UseAmf;
o.UseTemporaryEncodingPath = UseTemporaryEncodingPath;
o.Shaders = Shaders;
o.Resolution = Resolution;
Expand Down
1 change: 1 addition & 0 deletions Wasari.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using CliFx;
using Figgle;
using LibreHardwareMonitor.Hardware;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Wasari.Anime4k;
Expand Down
49 changes: 47 additions & 2 deletions Wasari.Environment/EnvironmentFeatureFinder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using CliWrap;
using CliWrap.Buffered;
using LibreHardwareMonitor.Hardware;
using Wasari.App.Abstractions;

namespace WasariEnvironment;
Expand Down Expand Up @@ -96,6 +98,49 @@ private static IEnumerable<EnvironmentFeatureModule> ParseFfmpegModules(string i
}
}

private static async Task<ICollection<EnvironmentFeature>> FindGpus()
{
var computer = new Computer
{
IsGpuEnabled = true
};

try
{
var features = new HashSet<EnvironmentFeatureType>();

computer.Open();

foreach (var hardware in computer.Hardware)
{
switch (hardware.HardwareType)
{
case HardwareType.GpuNvidia:
if (await IsProgramAvailable("nvidia-smi", null).DefaultIfFailed())
features.Add(EnvironmentFeatureType.NvidiaGpu);

break;
case HardwareType.GpuAmd:
features.Add(EnvironmentFeatureType.AmdGpu);
break;
}
}

return features
.Select(i => new EnvironmentFeature(i, null, null, string.Empty))
.ToHashSet();
}
catch (Exception e)
{
Debug.WriteLine("Failed to open LibreHardwareMonitor: {0}", e);
return Array.Empty<EnvironmentFeature>();
}
finally
{
computer.Close();
}
}

public static async IAsyncEnumerable<EnvironmentFeature> GetEnvironmentFeatures()
{
if (await GetProgramWithVersion(Environment.GetEnvironmentVariable("YTDLP") ?? "yt-dlp", "--version",
Expand All @@ -113,7 +158,7 @@ public static async IAsyncEnumerable<EnvironmentFeature> GetEnvironmentFeatures(
yield return new EnvironmentFeature(EnvironmentFeatureType.FfmpegLibPlacebo, null, null, string.Empty);
}

if (await IsProgramAvailable("nvidia-smi", null).DefaultIfFailed())
yield return new EnvironmentFeature(EnvironmentFeatureType.NvidiaGpu, null, null, string.Empty);
foreach (var gpuFeature in await FindGpus())
yield return gpuFeature;
}
}
3 changes: 2 additions & 1 deletion Wasari.Environment/EnvironmentFeatureType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public enum EnvironmentFeatureType
YtDlp,
Ffmpeg,
FfmpegLibPlacebo,
NvidiaGpu
NvidiaGpu,
AmdGpu
}
1 change: 1 addition & 0 deletions Wasari.Environment/Wasari.Environment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="CliWrap" Version="3.6.4"/>
<PackageReference Include="LibreHardwareMonitorLib" Version="0.9.2" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1"/>
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Wasari.FFmpeg/FFmpegOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public record FFmpegOptions
public FFmpegResolution? Resolution { get; set; }

public bool UseNvidiaAcceleration { get; set; }

public bool UseAmdAcceleration { get; set; }

public bool UseTemporaryEncodingPath { get; set; }

Expand Down
5 changes: 5 additions & 0 deletions Wasari.FFmpeg/FFmpegService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,13 @@ private async IAsyncEnumerable<string> BuildArgumentsForEpisode(IWasariEpisode e

if (Options.Value.UseHevc)
{
if(Options.Value is { UseNvidiaAcceleration: true, UseAmdAcceleration: true } && EnvironmentService.IsFeatureAvailable(EnvironmentFeatureType.NvidiaGpu, EnvironmentFeatureType.AmdGpu))
throw new MultipleEncodersException("Cannot use both Nvidia and AMD acceleration at the same time");

if (Options.Value.UseNvidiaAcceleration && EnvironmentService.IsFeatureAvailable(EnvironmentFeatureType.NvidiaGpu))
yield return "-c:v hevc_nvenc -rc vbr -cq 24 -qmin 24 -qmax 24 -profile:v main10 -pix_fmt p010le";
else if(Options.Value.UseAmdAcceleration && EnvironmentService.IsFeatureAvailable(EnvironmentFeatureType.AmdGpu))
yield return "-c:v hevc_amf -rc cbr -qmin 24 -qmax 24 -pix_fmt p010le";
else
yield return "-crf 20 -pix_fmt yuv420p10le -c:v libx265 -tune animation -x265-params profile=main10";
}
Expand Down
8 changes: 8 additions & 0 deletions Wasari.FFmpeg/MultipleEncodersException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Wasari.FFmpeg;

public class MultipleEncodersException : Exception
{
public MultipleEncodersException(string message) : base(message)
{
}
}

0 comments on commit 292674c

Please sign in to comment.