1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #nullable disable
- #pragma warning disable CS1591
- using System;
- namespace MediaBrowser.Model.Dlna
- {
- public static 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(2560, 20000000),
- new ResolutionConfiguration(3840, 35000000)
- };
- public static ResolutionOptions Normalize(
- int? inputBitrate,
- int outputBitrate,
- int? maxWidth,
- int? maxHeight)
- {
- // If the bitrate isn't changing, then don't downscale 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 is not 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;
- }
- }
- }
|