You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs

81 lines
2.7 KiB

#nullable disable
#pragma warning disable CS1591
using System;
using System.Linq;
6 years ago
namespace MediaBrowser.Model.Dlna
{
public static class ResolutionNormalizer
6 years ago
{
// Please note: all bitrate here are in the scale of SDR h264 bitrate at 30fps
private static readonly ResolutionConfiguration[] _configurations =
[
new ResolutionConfiguration(416, 365000),
new ResolutionConfiguration(640, 730000),
new ResolutionConfiguration(768, 1100000),
new ResolutionConfiguration(960, 3000000),
new ResolutionConfiguration(1280, 6000000),
new ResolutionConfiguration(1920, 13500000),
new ResolutionConfiguration(2560, 28000000),
new ResolutionConfiguration(3840, 50000000)
];
6 years ago
public static ResolutionOptions Normalize(
int? inputBitrate,
6 years ago
int outputBitrate,
int h264EquivalentOutputBitrate,
6 years ago
int? maxWidth,
int? maxHeight,
float? targetFps,
bool isHdr = false) // We are not doing HDR transcoding for now, leave for future use
6 years ago
{
// If the bitrate isn't changing, then don't downscale the resolution
6 years ago
if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
{
if (maxWidth.HasValue || maxHeight.HasValue)
{
return new ResolutionOptions
{
MaxWidth = maxWidth,
MaxHeight = maxHeight
};
}
}
6 years ago
var referenceBitrate = h264EquivalentOutputBitrate * (30.0f / (targetFps ?? 30.0f));
if (isHdr)
6 years ago
{
referenceBitrate *= 0.8f;
}
6 years ago
var resolutionConfig = GetResolutionConfiguration(Convert.ToInt32(referenceBitrate));
if (resolutionConfig is null)
{
return new ResolutionOptions { MaxWidth = maxWidth, MaxHeight = maxHeight };
}
var originWidthValue = maxWidth;
maxWidth = Math.Min(resolutionConfig.MaxWidth, maxWidth ?? resolutionConfig.MaxWidth);
if (!originWidthValue.HasValue || originWidthValue.Value != maxWidth.Value)
{
maxHeight = null;
6 years ago
}
return new ResolutionOptions
{
MaxWidth = maxWidth,
MaxHeight = maxHeight
};
}
private static ResolutionConfiguration GetResolutionConfiguration(int outputBitrate)
{
return _configurations.FirstOrDefault(config => outputBitrate <= config.MaxBitrate);
6 years ago
}
}
}