jellyfin/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs

98 lines
3 KiB
C#
Raw Normal View History

2014-10-07 01:58:46 +02:00
using System;
using System.Collections.Generic;
2016-08-14 00:27:14 +02:00
using MediaBrowser.Model.Extensions;
2014-10-07 01:58:46 +02:00
namespace MediaBrowser.Model.Dlna
{
public class ResolutionNormalizer
{
2017-08-19 21:43:35 +02:00
private static readonly ResolutionConfiguration[] Configurations =
new []
2014-10-07 01:58:46 +02:00
{
new ResolutionConfiguration(426, 320000),
new ResolutionConfiguration(640, 400000),
new ResolutionConfiguration(720, 950000),
2017-04-30 22:03:28 +02:00
new ResolutionConfiguration(1280, 2500000),
new ResolutionConfiguration(1920, 4000000),
new ResolutionConfiguration(3840, 35000000)
2014-10-07 01:58:46 +02:00
};
public static ResolutionOptions Normalize(int? inputBitrate,
int outputBitrate,
string inputCodec,
string outputCodec,
2014-10-07 01:58:46 +02:00
int? maxWidth,
int? maxHeight)
{
// If the bitrate isn't changing, then don't downlscale the resolution
if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
{
if (maxWidth.HasValue || maxHeight.HasValue)
{
return new ResolutionOptions
{
MaxWidth = maxWidth,
MaxHeight = maxHeight
};
}
}
2017-04-30 22:03:28 +02:00
var resolutionConfig = GetResolutionConfiguration(outputBitrate);
if (resolutionConfig != null)
2014-10-07 01:58:46 +02:00
{
2017-04-30 22:03:28 +02:00
var originvalValue = maxWidth;
2014-10-07 01:58:46 +02:00
2017-04-30 22:03:28 +02:00
maxWidth = Math.Min(resolutionConfig.MaxWidth, maxWidth ?? resolutionConfig.MaxWidth);
if (!originvalValue.HasValue || originvalValue.Value != maxWidth.Value)
{
maxHeight = null;
2014-10-07 01:58:46 +02:00
}
}
return new ResolutionOptions
{
MaxWidth = maxWidth,
MaxHeight = maxHeight
};
}
2017-04-30 22:03:28 +02:00
private static ResolutionConfiguration GetResolutionConfiguration(int outputBitrate)
{
2017-08-30 20:06:54 +02:00
ResolutionConfiguration previousOption = null;
2017-04-30 22:03:28 +02:00
foreach (var config in Configurations)
{
if (outputBitrate <= config.MaxBitrate)
{
2017-08-30 20:06:54 +02:00
return previousOption ?? config;
2017-04-30 22:03:28 +02:00
}
2017-08-30 20:06:54 +02:00
previousOption = config;
2017-04-30 22:03:28 +02:00
}
return null;
}
private static double GetVideoBitrateScaleFactor(string codec)
{
2016-08-14 00:27:14 +02:00
if (StringHelper.EqualsIgnoreCase(codec, "h265") ||
2016-11-30 08:49:32 +01:00
StringHelper.EqualsIgnoreCase(codec, "hevc") ||
StringHelper.EqualsIgnoreCase(codec, "vp9"))
{
return .5;
}
return 1;
}
public static int ScaleBitrate(int bitrate, string inputVideoCodec, string outputVideoCodec)
{
var inputScaleFactor = GetVideoBitrateScaleFactor(inputVideoCodec);
var outputScaleFactor = GetVideoBitrateScaleFactor(outputVideoCodec);
var scaleFactor = outputScaleFactor/inputScaleFactor;
var newBitrate = scaleFactor*bitrate;
return Convert.ToInt32(newBitrate);
}
2014-10-07 01:58:46 +02:00
}
}