ResolutionNormalizer.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Model.Dlna
  4. {
  5. public class ResolutionNormalizer
  6. {
  7. private static readonly List<ResolutionConfiguration> Configurations =
  8. new List<ResolutionConfiguration>
  9. {
  10. new ResolutionConfiguration(426, 320000),
  11. new ResolutionConfiguration(640, 400000),
  12. new ResolutionConfiguration(720, 950000),
  13. new ResolutionConfiguration(1280, 2500000)
  14. };
  15. public static ResolutionOptions Normalize(int? inputBitrate,
  16. int outputBitrate,
  17. string inputCodec,
  18. string outputCodec,
  19. int? maxWidth,
  20. int? maxHeight)
  21. {
  22. // If the bitrate isn't changing, then don't downlscale the resolution
  23. if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
  24. {
  25. if (maxWidth.HasValue || maxHeight.HasValue)
  26. {
  27. return new ResolutionOptions
  28. {
  29. MaxWidth = maxWidth,
  30. MaxHeight = maxHeight
  31. };
  32. }
  33. }
  34. foreach (var config in Configurations)
  35. {
  36. if (outputBitrate <= config.MaxBitrate)
  37. {
  38. var originvalValue = maxWidth;
  39. maxWidth = Math.Min(config.MaxWidth, maxWidth ?? config.MaxWidth);
  40. if (!originvalValue.HasValue || originvalValue.Value != maxWidth.Value)
  41. {
  42. maxHeight = null;
  43. }
  44. break;
  45. }
  46. }
  47. return new ResolutionOptions
  48. {
  49. MaxWidth = maxWidth,
  50. MaxHeight = maxHeight
  51. };
  52. }
  53. }
  54. }