h264_profile_level_id.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_
  11. #define MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_
  12. #include <map>
  13. #include <string>
  14. #include "absl/types/optional.h"
  15. #include "rtc_base/system/rtc_export.h"
  16. namespace webrtc {
  17. namespace H264 {
  18. enum Profile {
  19. kProfileConstrainedBaseline,
  20. kProfileBaseline,
  21. kProfileMain,
  22. kProfileConstrainedHigh,
  23. kProfileHigh,
  24. };
  25. // Map containting SDP codec parameters.
  26. typedef std::map<std::string, std::string> CodecParameterMap;
  27. // All values are equal to ten times the level number, except level 1b which is
  28. // special.
  29. enum Level {
  30. kLevel1_b = 0,
  31. kLevel1 = 10,
  32. kLevel1_1 = 11,
  33. kLevel1_2 = 12,
  34. kLevel1_3 = 13,
  35. kLevel2 = 20,
  36. kLevel2_1 = 21,
  37. kLevel2_2 = 22,
  38. kLevel3 = 30,
  39. kLevel3_1 = 31,
  40. kLevel3_2 = 32,
  41. kLevel4 = 40,
  42. kLevel4_1 = 41,
  43. kLevel4_2 = 42,
  44. kLevel5 = 50,
  45. kLevel5_1 = 51,
  46. kLevel5_2 = 52
  47. };
  48. struct ProfileLevelId {
  49. constexpr ProfileLevelId(Profile profile, Level level)
  50. : profile(profile), level(level) {}
  51. Profile profile;
  52. Level level;
  53. };
  54. // Parse profile level id that is represented as a string of 3 hex bytes.
  55. // Nothing will be returned if the string is not a recognized H264
  56. // profile level id.
  57. absl::optional<ProfileLevelId> ParseProfileLevelId(const char* str);
  58. // Parse profile level id that is represented as a string of 3 hex bytes
  59. // contained in an SDP key-value map. A default profile level id will be
  60. // returned if the profile-level-id key is missing. Nothing will be returned if
  61. // the key is present but the string is invalid.
  62. RTC_EXPORT absl::optional<ProfileLevelId> ParseSdpProfileLevelId(
  63. const CodecParameterMap& params);
  64. // Given that a decoder supports up to a given frame size (in pixels) at up to a
  65. // given number of frames per second, return the highest H.264 level where it
  66. // can guarantee that it will be able to support all valid encoded streams that
  67. // are within that level.
  68. RTC_EXPORT absl::optional<Level> SupportedLevel(int max_frame_pixel_count,
  69. float max_fps);
  70. // Returns canonical string representation as three hex bytes of the profile
  71. // level id, or returns nothing for invalid profile level ids.
  72. RTC_EXPORT absl::optional<std::string> ProfileLevelIdToString(
  73. const ProfileLevelId& profile_level_id);
  74. // Generate codec parameters that will be used as answer in an SDP negotiation
  75. // based on local supported parameters and remote offered parameters. Both
  76. // |local_supported_params|, |remote_offered_params|, and |answer_params|
  77. // represent sendrecv media descriptions, i.e they are a mix of both encode and
  78. // decode capabilities. In theory, when the profile in |local_supported_params|
  79. // represent a strict superset of the profile in |remote_offered_params|, we
  80. // could limit the profile in |answer_params| to the profile in
  81. // |remote_offered_params|. However, to simplify the code, each supported H264
  82. // profile should be listed explicitly in the list of local supported codecs,
  83. // even if they are redundant. Then each local codec in the list should be
  84. // tested one at a time against the remote codec, and only when the profiles are
  85. // equal should this function be called. Therefore, this function does not need
  86. // to handle profile intersection, and the profile of |local_supported_params|
  87. // and |remote_offered_params| must be equal before calling this function. The
  88. // parameters that are used when negotiating are the level part of
  89. // profile-level-id and level-asymmetry-allowed.
  90. void GenerateProfileLevelIdForAnswer(
  91. const CodecParameterMap& local_supported_params,
  92. const CodecParameterMap& remote_offered_params,
  93. CodecParameterMap* answer_params);
  94. // Returns true if the parameters have the same H264 profile, i.e. the same
  95. // H264::Profile (Baseline, High, etc).
  96. bool IsSameH264Profile(const CodecParameterMap& params1,
  97. const CodecParameterMap& params2);
  98. } // namespace H264
  99. } // namespace webrtc
  100. #endif // MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_