Skip to content

Commit

Permalink
Update VideoToolbox pipeline for jellyfin-ffmpeg7 (jellyfin#12380)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnattu authored Aug 5, 2024
1 parent bbf3a21 commit 7ea91df
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public partial class EncodingHelper
private readonly Version _minFFmpegSvtAv1Params = new Version(5, 1);
private readonly Version _minFFmpegVaapiH26xEncA53CcSei = new Version(6, 0);
private readonly Version _minFFmpegReadrateOption = new Version(5, 0);
private readonly Version _minFFmpegWorkingVtHwSurface = new Version(7, 0, 1);

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

Expand Down Expand Up @@ -5166,12 +5167,14 @@ public string GetLibplaceboFilter(
var threeDFormat = state.MediaSource.Video3DFormat;

var isVtEncoder = vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase);
var isVtDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase);

var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true);
var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true);
var doDeintH2645 = doDeintH264 || doDeintHevc;
var doVtTonemap = IsVideoToolboxTonemapAvailable(state, options);
var doMetalTonemap = !doVtTonemap && IsHwTonemapAvailable(state, options);
var usingHwSurface = isVtDecoder && (_mediaEncoder.EncoderVersion >= _minFFmpegWorkingVtHwSurface);

var scaleFormat = string.Empty;
// Use P010 for Metal tone mapping, otherwise force an 8bit output.
Expand Down Expand Up @@ -5259,23 +5262,25 @@ public string GetLibplaceboFilter(
subFilters.Add(subTextSubtitlesFilter);
}

subFilters.Add("hwupload=derive_device=videotoolbox");
subFilters.Add("hwupload");
overlayFilters.Add("overlay_videotoolbox=eof_action=pass:repeatlast=0");
}

if (usingHwSurface)
{
return (mainFilters, subFilters, overlayFilters);
}

// For old jellyfin-ffmpeg that has broken hwsurface, add a hwupload
var needFiltering = mainFilters.Any(f => !string.IsNullOrEmpty(f)) ||
subFilters.Any(f => !string.IsNullOrEmpty(f)) ||
overlayFilters.Any(f => !string.IsNullOrEmpty(f));

// This is a workaround for ffmpeg's hwupload implementation
// For VideoToolbox encoders, a hwupload without a valid filter actually consuming its frame
// will cause the encoder to produce incorrect frames.
if (needFiltering)
{
// INPUT videotoolbox/memory surface(vram/uma)
// this will pass-through automatically if in/out format matches.
mainFilters.Insert(0, "format=nv12|p010le|videotoolbox_vld");
mainFilters.Insert(0, "hwupload=derive_device=videotoolbox");
mainFilters.Insert(0, "hwupload");
}

return (mainFilters, subFilters, overlayFilters);
Expand Down Expand Up @@ -6283,22 +6288,20 @@ public string GetVideotoolboxVidDecoder(EncodingJobInfo state, EncodingOptions o
|| string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase);
var is8_10bitSwFormatsVt = is8bitSwFormatsVt || string.Equals("yuv420p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase);

// VideoToolbox's Hardware surface in ffmpeg is not only slower than hwupload, but also breaks HDR in many cases.
// For example: https://trac.ffmpeg.org/ticket/10884
// Disable it for now.
const bool UseHwSurface = false;
// The related patches make videotoolbox hardware surface working is only available in jellyfin-ffmpeg 7.0.1 at the moment.
bool useHwSurface = (_mediaEncoder.EncoderVersion >= _minFFmpegWorkingVtHwSurface) && IsVideoToolboxFullSupported();

if (is8bitSwFormatsVt)
{
if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase)
|| string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase))
{
return GetHwaccelType(state, options, "h264", bitDepth, UseHwSurface);
return GetHwaccelType(state, options, "h264", bitDepth, useHwSurface);
}

if (string.Equals("vp8", videoStream.Codec, StringComparison.OrdinalIgnoreCase))
{
return GetHwaccelType(state, options, "vp8", bitDepth, UseHwSurface);
return GetHwaccelType(state, options, "vp8", bitDepth, useHwSurface);
}
}

Expand All @@ -6307,12 +6310,12 @@ public string GetVideotoolboxVidDecoder(EncodingJobInfo state, EncodingOptions o
if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase)
|| string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase))
{
return GetHwaccelType(state, options, "hevc", bitDepth, UseHwSurface);
return GetHwaccelType(state, options, "hevc", bitDepth, useHwSurface);
}

if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase))
{
return GetHwaccelType(state, options, "vp9", bitDepth, UseHwSurface);
return GetHwaccelType(state, options, "vp9", bitDepth, useHwSurface);
}
}

Expand Down

0 comments on commit 7ea91df

Please sign in to comment.