ResolutionNormalizer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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(2560, 20000000),
  17. new ResolutionConfiguration(3840, 35000000)
  18. };
  19. public static ResolutionOptions Normalize(
  20. int? inputBitrate,
  21. int? unused1,
  22. int? unused2,
  23. int outputBitrate,
  24. string inputCodec,
  25. string outputCodec,
  26. int? maxWidth,
  27. int? maxHeight)
  28. {
  29. // If the bitrate isn't changing, then don't downscale the resolution
  30. if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
  31. {
  32. if (maxWidth.HasValue || maxHeight.HasValue)
  33. {
  34. return new ResolutionOptions
  35. {
  36. MaxWidth = maxWidth,
  37. MaxHeight = maxHeight
  38. };
  39. }
  40. }
  41. var resolutionConfig = GetResolutionConfiguration(outputBitrate);
  42. if (resolutionConfig != null)
  43. {
  44. var originvalValue = maxWidth;
  45. maxWidth = Math.Min(resolutionConfig.MaxWidth, maxWidth ?? resolutionConfig.MaxWidth);
  46. if (!originvalValue.HasValue || originvalValue.Value != maxWidth.Value)
  47. {
  48. maxHeight = null;
  49. }
  50. }
  51. return new ResolutionOptions
  52. {
  53. MaxWidth = maxWidth,
  54. MaxHeight = maxHeight
  55. };
  56. }
  57. private static ResolutionConfiguration GetResolutionConfiguration(int outputBitrate)
  58. {
  59. ResolutionConfiguration previousOption = null;
  60. foreach (var config in Configurations)
  61. {
  62. if (outputBitrate <= config.MaxBitrate)
  63. {
  64. return previousOption ?? config;
  65. }
  66. previousOption = config;
  67. }
  68. return null;
  69. }
  70. private static double GetVideoBitrateScaleFactor(string codec)
  71. {
  72. if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase)
  73. || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase)
  74. || string.Equals(codec, "vp9", StringComparison.OrdinalIgnoreCase))
  75. {
  76. return .6;
  77. }
  78. return 1;
  79. }
  80. public static int ScaleBitrate(int bitrate, string inputVideoCodec, string outputVideoCodec)
  81. {
  82. var inputScaleFactor = GetVideoBitrateScaleFactor(inputVideoCodec);
  83. var outputScaleFactor = GetVideoBitrateScaleFactor(outputVideoCodec);
  84. var scaleFactor = outputScaleFactor / inputScaleFactor;
  85. var newBitrate = scaleFactor * bitrate;
  86. return Convert.ToInt32(newBitrate);
  87. }
  88. }
  89. }