2020-04-05 16:10:56 +00:00
|
|
|
#nullable disable
|
2020-02-04 00:49:27 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:02:23 +00:00
|
|
|
using System;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Dlna
|
|
|
|
{
|
2021-09-09 13:59:13 +00:00
|
|
|
public static class ResolutionNormalizer
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2018-12-27 21:43:48 +00:00
|
|
|
private static readonly ResolutionConfiguration[] Configurations =
|
|
|
|
new[]
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
new ResolutionConfiguration(426, 320000),
|
|
|
|
new ResolutionConfiguration(640, 400000),
|
|
|
|
new ResolutionConfiguration(720, 950000),
|
|
|
|
new ResolutionConfiguration(1280, 2500000),
|
|
|
|
new ResolutionConfiguration(1920, 4000000),
|
2020-11-19 15:56:04 +00:00
|
|
|
new ResolutionConfiguration(2560, 20000000),
|
2018-12-27 23:27:57 +00:00
|
|
|
new ResolutionConfiguration(3840, 35000000)
|
|
|
|
};
|
|
|
|
|
2020-04-05 16:10:56 +00:00
|
|
|
public static ResolutionOptions Normalize(
|
|
|
|
int? inputBitrate,
|
2018-12-27 21:43:48 +00:00
|
|
|
int outputBitrate,
|
2018-12-27 23:27:57 +00:00
|
|
|
int? maxWidth,
|
|
|
|
int? maxHeight)
|
|
|
|
{
|
2020-11-18 13:46:14 +00:00
|
|
|
// If the bitrate isn't changing, then don't downscale the resolution
|
2018-12-27 21:43:48 +00:00
|
|
|
if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
|
|
|
|
{
|
|
|
|
if (maxWidth.HasValue || maxHeight.HasValue)
|
|
|
|
{
|
|
|
|
return new ResolutionOptions
|
|
|
|
{
|
|
|
|
MaxWidth = maxWidth,
|
|
|
|
MaxHeight = maxHeight
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
var resolutionConfig = GetResolutionConfiguration(outputBitrate);
|
|
|
|
if (resolutionConfig != null)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static double GetVideoBitrateScaleFactor(string codec)
|
|
|
|
{
|
2020-11-11 09:08:50 +00:00
|
|
|
if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| string.Equals(codec, "vp9", StringComparison.OrdinalIgnoreCase))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-11-11 09:08:50 +00:00
|
|
|
return .6;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
2020-04-05 16:10:56 +00:00
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int ScaleBitrate(int bitrate, string inputVideoCodec, string outputVideoCodec)
|
|
|
|
{
|
|
|
|
var inputScaleFactor = GetVideoBitrateScaleFactor(inputVideoCodec);
|
|
|
|
var outputScaleFactor = GetVideoBitrateScaleFactor(outputVideoCodec);
|
2018-12-27 21:43:48 +00:00
|
|
|
var scaleFactor = outputScaleFactor / inputScaleFactor;
|
|
|
|
var newBitrate = scaleFactor * bitrate;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
return Convert.ToInt32(newBitrate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|