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 11 #ifndef MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_ 12 #define MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_ 13 14 #include <map> 15 #include <string> 16 17 #include "absl/types/optional.h" 18 #include "common_types.h" // NOLINT(build/include) 19 #include "rtc_base/system/rtc_export.h" 20 21 namespace webrtc { 22 namespace H264 { 23 24 // Map containting SDP codec parameters. 25 typedef std::map<std::string, std::string> CodecParameterMap; 26 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 49 struct ProfileLevelId { ProfileLevelIdProfileLevelId50 constexpr ProfileLevelId(Profile profile, Level level) 51 : profile(profile), level(level) {} 52 Profile profile; 53 Level level; 54 }; 55 56 // Parse profile level id that is represented as a string of 3 hex bytes. 57 // Nothing will be returned if the string is not a recognized H264 58 // profile level id. 59 absl::optional<ProfileLevelId> ParseProfileLevelId(const char* str); 60 61 // Parse profile level id that is represented as a string of 3 hex bytes 62 // contained in an SDP key-value map. A default profile level id will be 63 // returned if the profile-level-id key is missing. Nothing will be returned if 64 // the key is present but the string is invalid. 65 RTC_EXPORT absl::optional<ProfileLevelId> ParseSdpProfileLevelId( 66 const CodecParameterMap& params); 67 68 // Given that a decoder supports up to a given frame size (in pixels) at up to a 69 // given number of frames per second, return the highest H.264 level where it 70 // can guarantee that it will be able to support all valid encoded streams that 71 // are within that level. 72 RTC_EXPORT absl::optional<Level> SupportedLevel(int max_frame_pixel_count, 73 float max_fps); 74 75 // Returns canonical string representation as three hex bytes of the profile 76 // level id, or returns nothing for invalid profile level ids. 77 RTC_EXPORT absl::optional<std::string> ProfileLevelIdToString( 78 const ProfileLevelId& profile_level_id); 79 80 // Generate codec parameters that will be used as answer in an SDP negotiation 81 // based on local supported parameters and remote offered parameters. Both 82 // |local_supported_params|, |remote_offered_params|, and |answer_params| 83 // represent sendrecv media descriptions, i.e they are a mix of both encode and 84 // decode capabilities. In theory, when the profile in |local_supported_params| 85 // represent a strict superset of the profile in |remote_offered_params|, we 86 // could limit the profile in |answer_params| to the profile in 87 // |remote_offered_params|. However, to simplify the code, each supported H264 88 // profile should be listed explicitly in the list of local supported codecs, 89 // even if they are redundant. Then each local codec in the list should be 90 // tested one at a time against the remote codec, and only when the profiles are 91 // equal should this function be called. Therefore, this function does not need 92 // to handle profile intersection, and the profile of |local_supported_params| 93 // and |remote_offered_params| must be equal before calling this function. The 94 // parameters that are used when negotiating are the level part of 95 // profile-level-id and level-asymmetry-allowed. 96 void GenerateProfileLevelIdForAnswer( 97 const CodecParameterMap& local_supported_params, 98 const CodecParameterMap& remote_offered_params, 99 CodecParameterMap* answer_params); 100 101 // Returns true if the parameters have the same H264 profile, i.e. the same 102 // H264::Profile (Baseline, High, etc). 103 bool IsSameH264Profile(const CodecParameterMap& params1, 104 const CodecParameterMap& params2); 105 106 } // namespace H264 107 } // namespace webrtc 108 109 #endif // MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_ 110