diff options
| author | Andrew Rabert <ar@nullsum.net> | 2018-12-27 18:27:57 -0500 |
|---|---|---|
| committer | Andrew Rabert <ar@nullsum.net> | 2018-12-27 18:27:57 -0500 |
| commit | a86b71899ec52c44ddc6c3018e8cc5e9d7ff4d62 (patch) | |
| tree | a74f6ea4a8abfa1664a605d31d48bc38245ccf58 /MediaBrowser.Model/Dlna/ResolutionNormalizer.cs | |
| parent | 9bac3ac616b01f67db98381feb09d34ebe821f9a (diff) | |
Add GPL modules
Diffstat (limited to 'MediaBrowser.Model/Dlna/ResolutionNormalizer.cs')
| -rw-r--r-- | MediaBrowser.Model/Dlna/ResolutionNormalizer.cs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs b/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs new file mode 100644 index 000000000..4fdf4972f --- /dev/null +++ b/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using MediaBrowser.Model.Extensions; + +namespace MediaBrowser.Model.Dlna +{ + public class ResolutionNormalizer + { + private static readonly ResolutionConfiguration[] Configurations = + new [] + { + new ResolutionConfiguration(426, 320000), + new ResolutionConfiguration(640, 400000), + new ResolutionConfiguration(720, 950000), + new ResolutionConfiguration(1280, 2500000), + new ResolutionConfiguration(1920, 4000000), + new ResolutionConfiguration(3840, 35000000) + }; + + public static ResolutionOptions Normalize(int? inputBitrate, + int? unused1, + int? unused2, + int outputBitrate, + string inputCodec, + string outputCodec, + 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 + }; + } + } + + 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) + { + if (StringHelper.EqualsIgnoreCase(codec, "h265") || + 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); + } + } +} |
