ResolutionNormalizer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. namespace MediaBrowser.Model.Dlna
  5. {
  6. public class ResolutionNormalizer
  7. {
  8. private static readonly ResolutionConfiguration[] Configurations =
  9. new[]
  10. {
  11. new ResolutionConfiguration(426, 320000),
  12. new ResolutionConfiguration(640, 400000),
  13. new ResolutionConfiguration(720, 950000),
  14. new ResolutionConfiguration(1280, 2500000),
  15. new ResolutionConfiguration(1920, 4000000),
  16. new ResolutionConfiguration(3840, 35000000)
  17. };
  18. public static ResolutionOptions Normalize(
  19. int? inputBitrate,
  20. int? unused1,
  21. int? unused2,
  22. int outputBitrate,
  23. string inputCodec,
  24. string outputCodec,
  25. int? maxWidth,
  26. int? maxHeight)
  27. {
  28. // If the bitrate isn't changing, then don't downlscale the resolution
  29. if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
  30. {
  31. if (maxWidth.HasValue || maxHeight.HasValue)
  32. {
  33. return new ResolutionOptions
  34. {
  35. MaxWidth = maxWidth,
  36. MaxHeight = maxHeight
  37. };
  38. }
  39. }
  40. var resolutionConfig = GetResolutionConfiguration(outputBitrate);
  41. if (resolutionConfig != null)
  42. {
  43. var originvalValue = maxWidth;
  44. maxWidth = Math.Min(resolutionConfig.MaxWidth, maxWidth ?? resolutionConfig.MaxWidth);
  45. if (!originvalValue.HasValue || originvalValue.Value != maxWidth.Value)
  46. {
  47. maxHeight = null;
  48. }
  49. }
  50. return new ResolutionOptions
  51. {
  52. MaxWidth = maxWidth,
  53. MaxHeight = maxHeight
  54. };
  55. }
  56. private static ResolutionConfiguration GetResolutionConfiguration(int outputBitrate)
  57. {
  58. ResolutionConfiguration previousOption = null;
  59. foreach (var config in Configurations)
  60. {
  61. if (outputBitrate <= config.MaxBitrate)
  62. {
  63. return previousOption ?? config;
  64. }
  65. previousOption = config;
  66. }
  67. return null;
  68. }
  69. private static double GetVideoBitrateScaleFactor(string codec)
  70. {
  71. if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase) ||
  72. string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase) ||
  73. string.Equals(codec, "vp9", StringComparison.OrdinalIgnoreCase))
  74. {
  75. return .5;
  76. }
  77. return 1;
  78. }
  79. public static int ScaleBitrate(int bitrate, string inputVideoCodec, string outputVideoCodec)
  80. {
  81. var inputScaleFactor = GetVideoBitrateScaleFactor(inputVideoCodec);
  82. var outputScaleFactor = GetVideoBitrateScaleFactor(outputVideoCodec);
  83. var scaleFactor = outputScaleFactor / inputScaleFactor;
  84. var newBitrate = scaleFactor * bitrate;
  85. return Convert.ToInt32(newBitrate);
  86. }
  87. }
  88. }