Skip to content

Commit

Permalink
Merge pull request jellyfin#12270 from gnattu/sw-tonemap
Browse files Browse the repository at this point in the history
  • Loading branch information
crobibero authored Jul 15, 2024
2 parents eb45a36 + 518c166 commit 1eb2b6e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
44 changes: 40 additions & 4 deletions MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ private bool IsVideoToolboxFullSupported()
&& _mediaEncoder.SupportsFilter("scale_vt");
}

private bool IsSwTonemapAvailable(EncodingJobInfo state, EncodingOptions options)
{
if (state.VideoStream is null
|| !options.EnableTonemapping
|| GetVideoColorBitDepth(state) != 10
|| !_mediaEncoder.SupportsFilter("tonemapx")
|| !(string.Equals(state.VideoStream?.ColorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase) || string.Equals(state.VideoStream?.ColorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase)))
{
return false;
}

return state.VideoStream.VideoRange == VideoRange.HDR
&& state.VideoStream.VideoRangeType is VideoRangeType.HDR10 or VideoRangeType.HLG or VideoRangeType.DOVIWithHDR10 or VideoRangeType.DOVIWithHLG;
}

private bool IsHwTonemapAvailable(EncodingJobInfo state, EncodingOptions options)
{
if (state.VideoStream is null
Expand Down Expand Up @@ -3517,6 +3532,7 @@ public string GetLibplaceboFilter(
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 doToneMap = IsSwTonemapAvailable(state, options);

var hasSubs = state.SubtitleStream is not null && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;
var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream;
Expand All @@ -3525,7 +3541,7 @@ public string GetLibplaceboFilter(
/* Make main filters for video stream */
var mainFilters = new List<string>();

mainFilters.Add(GetOverwriteColorPropertiesParam(state, false));
mainFilters.Add(GetOverwriteColorPropertiesParam(state, doToneMap));

// INPUT sw surface(memory/copy-back from vram)
// sw deint
Expand All @@ -3548,11 +3564,31 @@ public string GetLibplaceboFilter(

// sw scale
mainFilters.Add(swScaleFilter);
mainFilters.Add("format=" + outFormat);

// sw tonemap <= TODO: finsh the fast tonemap filter
// sw tonemap <= TODO: finish dovi tone mapping

if (doToneMap)
{
var tonemapArgs = $"tonemapx=tonemap={options.TonemappingAlgorithm}:desat={options.TonemappingDesat}:peak={options.TonemappingPeak}:t=bt709:m=bt709:p=bt709:format={outFormat}";

if (options.TonemappingParam != 0)
{
tonemapArgs += $":param={options.TonemappingParam}";
}

// OUTPUT yuv420p/nv12 surface(memory)
if (string.Equals(options.TonemappingRange, "tv", StringComparison.OrdinalIgnoreCase)
|| string.Equals(options.TonemappingRange, "pc", StringComparison.OrdinalIgnoreCase))
{
tonemapArgs += $":range={options.TonemappingRange}";
}

mainFilters.Add(tonemapArgs);
}
else
{
// OUTPUT yuv420p/nv12 surface(memory)
mainFilters.Add("format=" + outFormat);
}

/* Make sub and overlay filters for subtitle stream */
var subFilters = new List<string>();
Expand Down
1 change: 1 addition & 0 deletions MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public partial class EncoderValidator
// sw
"alphasrc",
"zscale",
"tonemapx",
// qsv
"scale_qsv",
"vpp_qsv",
Expand Down
18 changes: 12 additions & 6 deletions MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,16 +708,22 @@ private async Task<string> ExtractImageInternal(string inputPath, string contain
filters.Add("thumbnail=n=" + (useLargerBatchSize ? "50" : "24"));
}

// Use SW tonemap on HDR10/HLG video stream only when the zscale filter is available.
// Use SW tonemap on HDR10/HLG video stream only when the zscale or tonemapx filter is available.
var enableHdrExtraction = false;

if ((string.Equals(videoStream?.ColorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase)
if (string.Equals(videoStream?.ColorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase)
|| string.Equals(videoStream?.ColorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
&& SupportsFilter("zscale"))
{
enableHdrExtraction = true;

filters.Add("zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0:peak=100,zscale=t=bt709:m=bt709,format=yuv420p");
if (SupportsFilter("tonemapx"))
{
enableHdrExtraction = true;
filters.Add("tonemapx=tonemap=bt2390:desat=0:peak=100:t=bt709:m=bt709:p=bt709:format=yuv420p");
}
else if (SupportsFilter("zscale"))
{
enableHdrExtraction = true;
filters.Add("zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0:peak=100,zscale=t=bt709:m=bt709,format=yuv420p");
}
}

var vf = string.Join(',', filters);
Expand Down

0 comments on commit 1eb2b6e

Please sign in to comment.