This commit is contained in:
knackebrot 2024-04-25 09:15:44 -04:00 committed by GitHub
commit 9d59a650f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 156 additions and 6 deletions

View File

@ -1053,6 +1053,16 @@ namespace MediaBrowser.Controller.MediaEncoding
}
args.Append(GetQsvDeviceArgs(QsvAlias));
if (string.Equals(vidEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) && options.EnableIntelLookAheadH264)
{
args.Append(" -extra_hw_frames=" + options.IntelLookAheadDepthH264);
}
else if (string.Equals(vidEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase) && options.EnableIntelLookAheadHevc)
{
args.Append(" -extra_hw_frames=" + options.IntelLookAheadDepthHevc);
}
var filterDevArgs = GetFilterHwDeviceArgs(QsvAlias);
// child device used by qsv.
if (_mediaEncoder.SupportsHwaccel("vaapi") || _mediaEncoder.SupportsHwaccel("d3d11va"))
@ -1799,7 +1809,61 @@ namespace MediaBrowser.Controller.MediaEncoding
// Only h264_qsv has look_ahead option
if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase))
{
param += " -look_ahead 0";
if (encodingOptions.EnableIntelLookAheadH264)
{
param += $" -look_ahead 1 -look_ahead_depth {encodingOptions.IntelLookAheadDepthH264}";
}
else
{
param += " -look_ahead 0";
}
if (encodingOptions.EnableIntelExtBrcH264)
{
param += " -extbrc 1";
}
if (encodingOptions.EnableIntelBPyramidH264)
{
param += " -b_strategy 1";
}
if (encodingOptions.EnableIntelAdaptiveIBFramesH264)
{
param += " -adaptive_i 1 -adaptive_b 1";
}
if (encodingOptions.EnableIntelTrellisH264)
{
param += " -trellis 1";
}
}
else if (string.Equals(videoEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase))
{
if (encodingOptions.EnableIntelLookAheadHevc)
{
param += $" -look_ahead_depth {encodingOptions.IntelLookAheadDepthHevc}";
}
if (encodingOptions.EnableIntelExtBrcHevc)
{
param += " -extbrc 1";
}
if (encodingOptions.EnableIntelBPyramidHevc)
{
param += " -b_strategy 1";
}
if (encodingOptions.EnableIntelAdaptiveIBFramesHevc)
{
param += " -adaptive_i 1 -adaptive_b 1";
}
if (encodingOptions.EnableIntelTrellisHevc)
{
param += " -trellis 1";
}
}
}
else if (string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) // h264 (h264_nvenc)
@ -4164,7 +4228,7 @@ namespace MediaBrowser.Controller.MediaEncoding
// qsv requires a fixed pool size.
// default to 64 otherwise it will fail on certain iGPU.
subFilters.Add("hwupload=derive_device=qsv:extra_hw_frames=64");
subFilters.Add($"hwupload=derive_device=qsv:extra_hw_frames={GetIntelExtraHwFrames(vidEncoder, options, 64)}");
var (overlayW, overlayH) = GetFixedOutputSize(inW, inH, reqW, reqH, reqMaxW, reqMaxH);
var overlaySize = (overlayW.HasValue && overlayH.HasValue)
@ -4271,7 +4335,7 @@ namespace MediaBrowser.Controller.MediaEncoding
// allocate extra pool sizes for vaapi vpp
if (!string.IsNullOrEmpty(hwScaleFilter) && isVaapiDecoder)
{
hwScaleFilter += ":extra_hw_frames=24";
hwScaleFilter += $":extra_hw_frames={GetIntelExtraHwFrames(vidEncoder, options, 24)}";
}
// hw scale
@ -4347,7 +4411,7 @@ namespace MediaBrowser.Controller.MediaEncoding
// OUTPUT qsv(nv12) surface(vram)
// reverse-mapping via qsv(vaapi)-opencl interop.
// add extra pool size to avoid the 'cannot allocate memory' error on hevc_qsv.
mainFilters.Add("hwmap=derive_device=qsv:reverse=1:extra_hw_frames=16");
mainFilters.Add($"hwmap=derive_device=qsv:reverse=1:extra_hw_frames={GetIntelExtraHwFrames(vidEncoder, options, 16)}");
mainFilters.Add("format=qsv");
}
else if (isVaapiDecoder)
@ -4385,7 +4449,7 @@ namespace MediaBrowser.Controller.MediaEncoding
// qsv requires a fixed pool size.
// default to 64 otherwise it will fail on certain iGPU.
subFilters.Add("hwupload=derive_device=qsv:extra_hw_frames=64");
subFilters.Add($"hwupload=derive_device=qsv:extra_hw_frames={GetIntelExtraHwFrames(vidEncoder, options, 64)}");
var (overlayW, overlayH) = GetFixedOutputSize(inW, inH, reqW, reqH, reqMaxW, reqMaxH);
var overlaySize = (overlayW.HasValue && overlayH.HasValue)
@ -4557,7 +4621,7 @@ namespace MediaBrowser.Controller.MediaEncoding
// allocate extra pool sizes for vaapi vpp
if (!string.IsNullOrEmpty(hwScaleFilter))
{
hwScaleFilter += ":extra_hw_frames=24";
hwScaleFilter += $":extra_hw_frames={GetIntelExtraHwFrames(vidEncoder, options, 24)}";
}
// hw scale
@ -7052,5 +7116,19 @@ namespace MediaBrowser.Controller.MediaEncoding
{
return string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase);
}
public int GetIntelExtraHwFrames(string vidEncoder, EncodingOptions options, int initialExtraHwFrames)
{
if (string.Equals(vidEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) && options.EnableIntelLookAheadH264)
{
initialExtraHwFrames += options.IntelLookAheadDepthH264;
}
else if (string.Equals(vidEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase) && options.EnableIntelLookAheadHevc)
{
initialExtraHwFrames += options.IntelLookAheadDepthHevc;
}
return initialExtraHwFrames;
}
}
}

View File

@ -47,7 +47,19 @@ public class EncodingOptions
EnableEnhancedNvdecDecoder = true;
PreferSystemNativeHwDecoder = true;
EnableIntelLowPowerH264HwEncoder = false;
EnableIntelLookAheadH264 = false;
IntelLookAheadDepthH264 = 60;
EnableIntelExtBrcH264 = false;
EnableIntelBPyramidH264 = false;
EnableIntelAdaptiveIBFramesH264 = false;
EnableIntelTrellisH264 = false;
EnableIntelLowPowerHevcHwEncoder = false;
EnableIntelLookAheadHevc = false;
IntelLookAheadDepthHevc = 60;
EnableIntelExtBrcHevc = false;
EnableIntelBPyramidHevc = false;
EnableIntelAdaptiveIBFramesHevc = false;
EnableIntelTrellisHevc = false;
EnableHardwareEncoding = true;
AllowHevcEncoding = false;
AllowAv1Encoding = false;
@ -241,11 +253,71 @@ public class EncodingOptions
/// </summary>
public bool EnableIntelLowPowerH264HwEncoder { get; set; }
/// <summary>
/// Gets or sets a value indicating whether LookAhead should be used for Intel H264 encoder.
/// </summary>
public bool EnableIntelLookAheadH264 { get; set; }
/// <summary>
/// Gets or sets a value indicating the number of frames that should be used for LookAhead with Intel H264 encoder.
/// </summary>
public int IntelLookAheadDepthH264 { get; set; }
/// <summary>
/// Gets or sets a value indicating whether ExtBrc should be used for Intel H264 encoder.
/// </summary>
public bool EnableIntelExtBrcH264 { get; set; }
/// <summary>
/// Gets or sets a value indicating whether B-Pyramid should be used for Intel H264 encoder.
/// </summary>
public bool EnableIntelBPyramidH264 { get; set; }
/// <summary>
/// Gets or sets a value indicating whether Adaptive I/B frames should be used for Intel H264 encoder.
/// </summary>
public bool EnableIntelAdaptiveIBFramesH264 { get; set; }
/// <summary>
/// Gets or sets a value indicating whether Trellis quantization should be used for Intel H264 encoder.
/// </summary>
public bool EnableIntelTrellisH264 { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the Intel HEVC low-power hardware encoder should be used.
/// </summary>
public bool EnableIntelLowPowerHevcHwEncoder { get; set; }
/// <summary>
/// Gets or sets a value indicating whether LookAhead should be used for Intel HEVC encoder.
/// </summary>
public bool EnableIntelLookAheadHevc { get; set; }
/// <summary>
/// Gets or sets a value indicating the number of frames that should be used for LookAhead with Intel HEVC encoder.
/// </summary>
public int IntelLookAheadDepthHevc { get; set; }
/// <summary>
/// Gets or sets a value indicating whether ExtBrc should be used for Intel HEVC encoder.
/// </summary>
public bool EnableIntelExtBrcHevc { get; set; }
/// <summary>
/// Gets or sets a value indicating whether B-Pyramid should be used for Intel HEVC encoder.
/// </summary>
public bool EnableIntelBPyramidHevc { get; set; }
/// <summary>
/// Gets or sets a value indicating whether Adaptive I/B frames should be used for Intel HEVC encoder.
/// </summary>
public bool EnableIntelAdaptiveIBFramesHevc { get; set; }
/// <summary>
/// Gets or sets a value indicating whether Trellis quantization should be used for Intel HEVC encoder.
/// </summary>
public bool EnableIntelTrellisHevc { get; set; }
/// <summary>
/// Gets or sets a value indicating whether hardware encoding is enabled.
/// </summary>