From 0fc288936d10afc146780d118361f2e722768ee6 Mon Sep 17 00:00:00 2001 From: gnattu Date: Mon, 9 Dec 2024 16:17:49 +0800 Subject: Enable VideoToolbox AV1 decode This decoder differs from others provided by VideoToolbox in that it lacks any software fallback. To achieve consistent behavior with other VideoToolbox decoders, this PR implemented additional checking on the server to simulate the software fallback provided by VideoToolbox. The current fallback checking mechanism is a temporary solution. In the long term, it should be replaced with a more capable hardware capability checking system. --- .../Encoder/ApplePlatformHelper.cs | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs (limited to 'MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs') diff --git a/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs b/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs new file mode 100644 index 000000000..ea2289bd7 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs @@ -0,0 +1,85 @@ +#pragma warning disable CA1031 + +using System; +using System.Linq; +using System.Runtime.InteropServices; +using Microsoft.Extensions.Logging; + +namespace MediaBrowser.MediaEncoding.Encoder; + +/// +/// Helper class for Apple platform specific operations. +/// +public static class ApplePlatformHelper +{ + private static readonly string[] _av1DecodeBlacklistedCpuClass = ["M1", "M2"]; + + private static string GetSysctlValue(string name) + { + IntPtr length = IntPtr.Zero; + // Get length of the value + int osStatus = SysctlByName(name, IntPtr.Zero, ref length, IntPtr.Zero, 0); + + if (osStatus != 0) + { + throw new NotSupportedException($"Failed to get sysctl value for {name} with error {osStatus}"); + } + + IntPtr buffer = Marshal.AllocHGlobal(length.ToInt32()); + try + { + osStatus = SysctlByName(name, buffer, ref length, IntPtr.Zero, 0); + if (osStatus != 0) + { + throw new NotSupportedException($"Failed to get sysctl value for {name} with error {osStatus}"); + } + + return Marshal.PtrToStringAnsi(buffer) ?? string.Empty; + } + finally + { + Marshal.FreeHGlobal(buffer); + } + } + + private static int SysctlByName(string name, IntPtr oldp, ref IntPtr oldlenp, IntPtr newp, uint newlen) + { + return NativeMethods.SysctlByName(System.Text.Encoding.ASCII.GetBytes(name), oldp, ref oldlenp, newp, newlen); + } + + /// + /// Check if the current system has hardware acceleration for AV1 decoding. + /// + /// The logger used for error logging. + /// Boolean indicates the hwaccel support. + public static bool HasAv1HardwareAccel(ILogger logger) + { + if (!RuntimeInformation.OSArchitecture.Equals(Architecture.Arm64)) + { + return false; + } + + try + { + string cpuBrandString = GetSysctlValue("machdep.cpu.brand_string"); + return !_av1DecodeBlacklistedCpuClass.Any(blacklistedCpuClass => cpuBrandString.Contains(blacklistedCpuClass, StringComparison.OrdinalIgnoreCase)); + } + catch (NotSupportedException e) + { + logger.LogError("Error getting CPU brand string: {Message}", e.Message); + } + catch (Exception e) + { + logger.LogError("Unknown error occured: {Exception}", e); + } + + return false; + } + + private static class NativeMethods + { + [DllImport("libc", EntryPoint = "sysctlbyname", SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] + internal static extern int SysctlByName(byte[] name, IntPtr oldp, ref IntPtr oldlenp, IntPtr newp, uint newlen); + } +} -- cgit v1.2.3 From 384134fd25fec22792733ccaa9f10a23d5c98eac Mon Sep 17 00:00:00 2001 From: gnattu Date: Fri, 28 Mar 2025 21:22:00 +0800 Subject: Use string literal --- MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs') diff --git a/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs b/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs index ea2289bd7..76fc5f695 100644 --- a/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs +++ b/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs @@ -14,7 +14,7 @@ public static class ApplePlatformHelper { private static readonly string[] _av1DecodeBlacklistedCpuClass = ["M1", "M2"]; - private static string GetSysctlValue(string name) + private static string GetSysctlValue(ReadOnlySpan name) { IntPtr length = IntPtr.Zero; // Get length of the value @@ -22,7 +22,7 @@ public static class ApplePlatformHelper if (osStatus != 0) { - throw new NotSupportedException($"Failed to get sysctl value for {name} with error {osStatus}"); + throw new NotSupportedException($"Failed to get sysctl value for {System.Text.Encoding.UTF8.GetString(name)} with error {osStatus}"); } IntPtr buffer = Marshal.AllocHGlobal(length.ToInt32()); @@ -31,7 +31,7 @@ public static class ApplePlatformHelper osStatus = SysctlByName(name, buffer, ref length, IntPtr.Zero, 0); if (osStatus != 0) { - throw new NotSupportedException($"Failed to get sysctl value for {name} with error {osStatus}"); + throw new NotSupportedException($"Failed to get sysctl value for {System.Text.Encoding.UTF8.GetString(name)} with error {osStatus}"); } return Marshal.PtrToStringAnsi(buffer) ?? string.Empty; @@ -42,9 +42,9 @@ public static class ApplePlatformHelper } } - private static int SysctlByName(string name, IntPtr oldp, ref IntPtr oldlenp, IntPtr newp, uint newlen) + private static int SysctlByName(ReadOnlySpan name, IntPtr oldp, ref IntPtr oldlenp, IntPtr newp, uint newlen) { - return NativeMethods.SysctlByName(System.Text.Encoding.ASCII.GetBytes(name), oldp, ref oldlenp, newp, newlen); + return NativeMethods.SysctlByName(name.ToArray(), oldp, ref oldlenp, newp, newlen); } /// @@ -61,7 +61,7 @@ public static class ApplePlatformHelper try { - string cpuBrandString = GetSysctlValue("machdep.cpu.brand_string"); + string cpuBrandString = GetSysctlValue("machdep.cpu.brand_string"u8); return !_av1DecodeBlacklistedCpuClass.Any(blacklistedCpuClass => cpuBrandString.Contains(blacklistedCpuClass, StringComparison.OrdinalIgnoreCase)); } catch (NotSupportedException e) -- cgit v1.2.3 From c69e9d8f2cac26a832f0ee09bed2809aae1872bf Mon Sep 17 00:00:00 2001 From: gnattu Date: Fri, 28 Mar 2025 21:30:39 +0800 Subject: Gate the macOS only functions --- MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs | 2 ++ MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs | 2 ++ 2 files changed, 4 insertions(+) (limited to 'MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs') diff --git a/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs b/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs index 76fc5f695..a8ff58b09 100644 --- a/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs +++ b/MediaBrowser.MediaEncoding/Encoder/ApplePlatformHelper.cs @@ -3,6 +3,7 @@ using System; using System.Linq; using System.Runtime.InteropServices; +using System.Runtime.Versioning; using Microsoft.Extensions.Logging; namespace MediaBrowser.MediaEncoding.Encoder; @@ -10,6 +11,7 @@ namespace MediaBrowser.MediaEncoding.Encoder; /// /// Helper class for Apple platform specific operations. /// +[SupportedOSPlatform("macos")] public static class ApplePlatformHelper { private static readonly string[] _av1DecodeBlacklistedCpuClass = ["M1", "M2"]; diff --git a/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs b/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs index 776b2ab42..54d0eb4b5 100644 --- a/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs +++ b/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; +using System.Runtime.Versioning; using System.Text.RegularExpressions; using Microsoft.Extensions.Logging; @@ -437,6 +438,7 @@ namespace MediaBrowser.MediaEncoding.Encoder } } + [SupportedOSPlatform("macos")] public bool CheckIsVideoToolboxAv1DecodeAvailable() { return ApplePlatformHelper.HasAv1HardwareAccel(_logger); -- cgit v1.2.3