jellyfin/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs

78 lines
2.4 KiB
C#
Raw Normal View History

#nullable disable
2020-02-03 19:49:27 -05:00
#pragma warning disable CS1591
using System;
2018-12-27 18:27:57 -05:00
namespace MediaBrowser.Model.Dlna
{
2021-09-09 09:59:13 -04:00
public static class ResolutionNormalizer
2018-12-27 18:27:57 -05:00
{
2018-12-27 16:43:48 -05:00
private static readonly ResolutionConfiguration[] Configurations =
new[]
2018-12-27 18:27:57 -05:00
{
new ResolutionConfiguration(426, 320000),
new ResolutionConfiguration(640, 400000),
new ResolutionConfiguration(720, 950000),
new ResolutionConfiguration(1280, 2500000),
new ResolutionConfiguration(1920, 4000000),
new ResolutionConfiguration(2560, 20000000),
2018-12-27 18:27:57 -05:00
new ResolutionConfiguration(3840, 35000000)
};
public static ResolutionOptions Normalize(
int? inputBitrate,
2018-12-27 16:43:48 -05:00
int outputBitrate,
2018-12-27 18:27:57 -05:00
int? maxWidth,
int? maxHeight)
{
2020-11-18 08:46:14 -05:00
// If the bitrate isn't changing, then don't downscale the resolution
2018-12-27 16:43:48 -05:00
if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
{
if (maxWidth.HasValue || maxHeight.HasValue)
{
return new ResolutionOptions
{
MaxWidth = maxWidth,
MaxHeight = maxHeight
};
}
}
2018-12-27 18:27:57 -05:00
var resolutionConfig = GetResolutionConfiguration(outputBitrate);
2022-12-05 09:01:13 -05:00
if (resolutionConfig is not null)
2018-12-27 18:27:57 -05:00
{
var originvalValue = maxWidth;
maxWidth = Math.Min(resolutionConfig.MaxWidth, maxWidth ?? resolutionConfig.MaxWidth);
if (!originvalValue.HasValue || originvalValue.Value != maxWidth.Value)
{
maxHeight = null;
}
}
return new ResolutionOptions
{
MaxWidth = maxWidth,
MaxHeight = maxHeight
};
}
private static ResolutionConfiguration GetResolutionConfiguration(int outputBitrate)
{
ResolutionConfiguration previousOption = null;
foreach (var config in Configurations)
{
if (outputBitrate <= config.MaxBitrate)
{
return previousOption ?? config;
}
previousOption = config;
}
return null;
}
}
}