ResolutionNormalizer.cs 3.3 KB

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