H264ProfileLevelId.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "pch.h"
  2. #include <rtc_base/arraysize.h>
  3. #include "H264ProfileLevelId.h"
  4. using namespace ::webrtc;
  5. namespace unity
  6. {
  7. namespace webrtc
  8. {
  9. struct LevelConstraint
  10. {
  11. const int max_macroblocks_per_second;
  12. const int max_macroblock_frame_size;
  13. const int max_video_bitrate;
  14. const H264Level level;
  15. };
  16. static constexpr LevelConstraint kLevelConstraints[] = {
  17. { 1485, 99, 64, H264Level::kLevel1 },
  18. { 1485, 99, 128, H264Level::kLevel1_b },
  19. { 3000, 396, 192, H264Level::kLevel1_1 },
  20. { 6000, 396, 384, H264Level::kLevel1_2 },
  21. { 11880, 396, 768, H264Level::kLevel1_3 },
  22. { 11880, 396, 2000, H264Level::kLevel2 },
  23. { 19800, 792, 4000, H264Level::kLevel2_1 },
  24. { 20250, 1620, 4000, H264Level::kLevel2_2 },
  25. { 40500, 1620, 10000, H264Level::kLevel3 },
  26. { 108000, 3600, 14000, H264Level::kLevel3_1 },
  27. { 216000, 5120, 20000, H264Level::kLevel3_2 },
  28. { 245760, 8192, 20000, H264Level::kLevel4 },
  29. { 245760, 8192, 50000, H264Level::kLevel4_1 },
  30. { 522240, 8704, 50000, H264Level::kLevel4_2 },
  31. { 589824, 22080, 135000, H264Level::kLevel5 },
  32. { 983040, 36864, 240000, H264Level::kLevel5_1 },
  33. { 2073600, 36864, 240000, H264Level::kLevel5_2 },
  34. };
  35. static const int kPixelsPerMacroblock = 16 * 16;
  36. static const int kUnitMaxBRWithNAL = 1200;
  37. absl::optional<webrtc::H264Level> H264SupportedLevel(int maxFramePixelCount, int maxFramerate, int maxBitrate)
  38. {
  39. if (maxFramePixelCount <= 0 || maxFramerate <= 0 || maxBitrate <= 0)
  40. return absl::nullopt;
  41. for (size_t i = 0; i < arraysize(kLevelConstraints); i++)
  42. {
  43. const LevelConstraint& level_constraint = kLevelConstraints[i];
  44. if (level_constraint.max_macroblock_frame_size * kPixelsPerMacroblock >= maxFramePixelCount &&
  45. level_constraint.max_macroblocks_per_second >= maxFramerate * maxFramePixelCount / kPixelsPerMacroblock &&
  46. level_constraint.max_video_bitrate * kUnitMaxBRWithNAL >= maxBitrate)
  47. {
  48. return level_constraint.level;
  49. }
  50. }
  51. // No level supported.
  52. return absl::nullopt;
  53. }
  54. int SupportedMaxFramerate(H264Level level, int maxFramePixelCount)
  55. {
  56. for (size_t i = 0; i < arraysize(kLevelConstraints); i++)
  57. {
  58. const LevelConstraint& level_constraint = kLevelConstraints[i];
  59. if (level_constraint.level == level)
  60. {
  61. return level_constraint.max_macroblocks_per_second * kPixelsPerMacroblock / maxFramePixelCount;
  62. }
  63. }
  64. // target level not found.
  65. return 0;
  66. }
  67. }
  68. }