Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
soultaco83 committed Sep 23, 2024
2 parents 5938954 + cb8f010 commit 3b598cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
35 changes: 26 additions & 9 deletions MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public partial class EncodingHelper
private readonly Version _minFFmpegAlteredVaVkInterop = new Version(7, 0, 1);
private readonly Version _minFFmpegQsvVppTonemapOption = new Version(7, 0, 1);
private readonly Version _minFFmpegQsvVppOutRangeOption = new Version(7, 0, 1);
private readonly Version _minFFmpegVaapiDeviceVendorId = new Version(7, 0, 1);

private static readonly Regex _validationRegex = new(ValidationRegex, RegexOptions.Compiled);

Expand Down Expand Up @@ -208,6 +209,14 @@ private string GetMjpegEncoder(EncodingJobInfo state, EncodingOptions encodingOp
{
var hwType = encodingOptions.HardwareAccelerationType;

// Only Intel has VA-API MJPEG encoder
if (hwType == HardwareAccelerationType.vaapi
&& !(_mediaEncoder.IsVaapiDeviceInteliHD
|| _mediaEncoder.IsVaapiDeviceInteli965))
{
return _defaultMjpegEncoder;
}

if (hwType != HardwareAccelerationType.none
&& encodingOptions.EnableHardwareEncoding
&& _mjpegCodecMap.TryGetValue(hwType, out var preferredEncoder)
Expand Down Expand Up @@ -872,13 +881,15 @@ private string GetD3d11vaDeviceArgs(int deviceIndex, string deviceVendorId, stri
options);
}

private string GetVaapiDeviceArgs(string renderNodePath, string driver, string kernelDriver, string srcDeviceAlias, string alias)
private string GetVaapiDeviceArgs(string renderNodePath, string driver, string kernelDriver, string vendorId, string srcDeviceAlias, string alias)
{
alias ??= VaapiAlias;
var haveVendorId = !string.IsNullOrEmpty(vendorId)
&& _mediaEncoder.EncoderVersion >= _minFFmpegVaapiDeviceVendorId;

// 'renderNodePath' has higher priority than 'kernelDriver'
// Priority: 'renderNodePath' > 'vendorId' > 'kernelDriver'
var driverOpts = string.IsNullOrEmpty(renderNodePath)
? (string.IsNullOrEmpty(kernelDriver) ? string.Empty : ",kernel_driver=" + kernelDriver)
? (haveVendorId ? $",vendor_id={vendorId}" : (string.IsNullOrEmpty(kernelDriver) ? string.Empty : $",kernel_driver={kernelDriver}"))
: renderNodePath;

// 'driver' behaves similarly to env LIBVA_DRIVER_NAME
Expand Down Expand Up @@ -913,7 +924,7 @@ private string GetQsvDeviceArgs(string renderNodePath, string alias)
if (OperatingSystem.IsLinux())
{
// derive qsv from vaapi device
return GetVaapiDeviceArgs(renderNodePath, "iHD", "i915", null, VaapiAlias) + arg + "@" + VaapiAlias;
return GetVaapiDeviceArgs(renderNodePath, "iHD", "i915", "0x8086", null, VaapiAlias) + arg + "@" + VaapiAlias;
}

if (OperatingSystem.IsWindows())
Expand Down Expand Up @@ -1008,14 +1019,14 @@ public string GetInputVideoHwaccelArgs(EncodingJobInfo state, EncodingOptions op

if (_mediaEncoder.IsVaapiDeviceInteliHD)
{
args.Append(GetVaapiDeviceArgs(options.VaapiDevice, "iHD", null, null, VaapiAlias));
args.Append(GetVaapiDeviceArgs(options.VaapiDevice, "iHD", null, null, null, VaapiAlias));
}
else if (_mediaEncoder.IsVaapiDeviceInteli965)
{
// Only override i965 since it has lower priority than iHD in libva lookup.
Environment.SetEnvironmentVariable("LIBVA_DRIVER_NAME", "i965");
Environment.SetEnvironmentVariable("LIBVA_DRIVER_NAME_JELLYFIN", "i965");
args.Append(GetVaapiDeviceArgs(options.VaapiDevice, "i965", null, null, VaapiAlias));
args.Append(GetVaapiDeviceArgs(options.VaapiDevice, "i965", null, null, null, VaapiAlias));
}

var filterDevArgs = string.Empty;
Expand All @@ -1039,15 +1050,15 @@ public string GetInputVideoHwaccelArgs(EncodingJobInfo state, EncodingOptions op
&& Environment.OSVersion.Version >= _minKernelVersionAmdVkFmtModifier)
{
args.Append(GetDrmDeviceArgs(options.VaapiDevice, DrmAlias));
args.Append(GetVaapiDeviceArgs(null, null, null, DrmAlias, VaapiAlias));
args.Append(GetVaapiDeviceArgs(null, null, null, null, DrmAlias, VaapiAlias));
args.Append(GetVulkanDeviceArgs(0, null, DrmAlias, VulkanAlias));

// libplacebo wants an explicitly set vulkan filter device.
filterDevArgs = GetFilterHwDeviceArgs(VulkanAlias);
}
else
{
args.Append(GetVaapiDeviceArgs(options.VaapiDevice, null, null, null, VaapiAlias));
args.Append(GetVaapiDeviceArgs(options.VaapiDevice, null, null, null, null, VaapiAlias));
filterDevArgs = GetFilterHwDeviceArgs(VaapiAlias);

if (doOclTonemap)
Expand Down Expand Up @@ -1441,7 +1452,13 @@ private string GetEncoderParam(EncoderPreset? preset, EncoderPreset defaultPrese
var encoderPreset = preset ?? defaultPreset;
if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) || isLibX265)
{
param += " -preset " + encoderPreset.ToString().ToLowerInvariant();
var presetString = encoderPreset switch
{
EncoderPreset.auto => EncoderPreset.veryfast.ToString().ToLowerInvariant(),
_ => encoderPreset.ToString().ToLowerInvariant()
};

param += " -preset " + presetString;

int encodeCrf = encodingOptions.H264Crf;
if (isLibX265)
Expand Down
6 changes: 5 additions & 1 deletion MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,11 @@ public string EscapeSubtitleFilterPath(string path)
// https://ffmpeg.org/ffmpeg-filters.html#Notes-on-filtergraph-escaping
// We need to double escape

return path.Replace('\\', '/').Replace(":", "\\:", StringComparison.Ordinal).Replace("'", @"'\\\''", StringComparison.Ordinal);
return path
.Replace('\\', '/')
.Replace(":", "\\:", StringComparison.Ordinal)
.Replace("'", @"'\\\''", StringComparison.Ordinal)
.Replace("\"", "\\\"", StringComparison.Ordinal);
}

/// <inheritdoc />
Expand Down

0 comments on commit 3b598cf

Please sign in to comment.