| | 1 | | #nullable disable |
| | 2 | | #pragma warning disable CS1591 |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | namespace MediaBrowser.Model.Dlna |
| | 7 | | { |
| | 8 | | public static class ResolutionNormalizer |
| | 9 | | { |
| 0 | 10 | | private static readonly ResolutionConfiguration[] Configurations = |
| 0 | 11 | | new[] |
| 0 | 12 | | { |
| 0 | 13 | | new ResolutionConfiguration(426, 320000), |
| 0 | 14 | | new ResolutionConfiguration(640, 400000), |
| 0 | 15 | | new ResolutionConfiguration(720, 950000), |
| 0 | 16 | | new ResolutionConfiguration(1280, 2500000), |
| 0 | 17 | | new ResolutionConfiguration(1920, 4000000), |
| 0 | 18 | | new ResolutionConfiguration(2560, 20000000), |
| 0 | 19 | | new ResolutionConfiguration(3840, 35000000) |
| 0 | 20 | | }; |
| | 21 | |
|
| | 22 | | public static ResolutionOptions Normalize( |
| | 23 | | int? inputBitrate, |
| | 24 | | int outputBitrate, |
| | 25 | | int? maxWidth, |
| | 26 | | int? maxHeight) |
| | 27 | | { |
| | 28 | | // If the bitrate isn't changing, then don't downscale the resolution |
| 0 | 29 | | if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value) |
| | 30 | | { |
| 0 | 31 | | if (maxWidth.HasValue || maxHeight.HasValue) |
| | 32 | | { |
| 0 | 33 | | return new ResolutionOptions |
| 0 | 34 | | { |
| 0 | 35 | | MaxWidth = maxWidth, |
| 0 | 36 | | MaxHeight = maxHeight |
| 0 | 37 | | }; |
| | 38 | | } |
| | 39 | | } |
| | 40 | |
|
| 0 | 41 | | var resolutionConfig = GetResolutionConfiguration(outputBitrate); |
| 0 | 42 | | if (resolutionConfig is not null) |
| | 43 | | { |
| 0 | 44 | | var originvalValue = maxWidth; |
| | 45 | |
|
| 0 | 46 | | maxWidth = Math.Min(resolutionConfig.MaxWidth, maxWidth ?? resolutionConfig.MaxWidth); |
| 0 | 47 | | if (!originvalValue.HasValue || originvalValue.Value != maxWidth.Value) |
| | 48 | | { |
| 0 | 49 | | maxHeight = null; |
| | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| 0 | 53 | | return new ResolutionOptions |
| 0 | 54 | | { |
| 0 | 55 | | MaxWidth = maxWidth, |
| 0 | 56 | | MaxHeight = maxHeight |
| 0 | 57 | | }; |
| | 58 | | } |
| | 59 | |
|
| | 60 | | private static ResolutionConfiguration GetResolutionConfiguration(int outputBitrate) |
| | 61 | | { |
| 0 | 62 | | ResolutionConfiguration previousOption = null; |
| | 63 | |
|
| 0 | 64 | | foreach (var config in Configurations) |
| | 65 | | { |
| 0 | 66 | | if (outputBitrate <= config.MaxBitrate) |
| | 67 | | { |
| 0 | 68 | | return previousOption ?? config; |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | previousOption = config; |
| | 72 | | } |
| | 73 | |
|
| 0 | 74 | | return null; |
| | 75 | | } |
| | 76 | | } |
| | 77 | | } |