magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 1 | /* |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "media/base/h264_profile_level_id.h" |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 12 | |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 13 | #include <cstdio> |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 14 | #include <cstdlib> |
| 15 | #include <cstring> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/arraysize.h" |
magjed | a92704e | 2016-11-08 02:57:51 -0800 | [diff] [blame] | 18 | |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | namespace H264 { |
| 21 | |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 24 | const char kProfileLevelId[] = "profile-level-id"; |
| 25 | const char kLevelAsymmetryAllowed[] = "level-asymmetry-allowed"; |
| 26 | |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 27 | // For level_idc=11 and profile_idc=0x42, 0x4D, or 0x58, the constraint set3 |
| 28 | // flag specifies if level 1b or level 1.1 is used. |
| 29 | const uint8_t kConstraintSet3Flag = 0x10; |
| 30 | |
| 31 | // Convert a string of 8 characters into a byte where the positions containing |
| 32 | // character c will have their bit set. For example, c = 'x', str = "x1xx0000" |
| 33 | // will return 0b10110000. constexpr is used so that the pattern table in |
| 34 | // kProfilePatterns is statically initialized. |
| 35 | constexpr uint8_t ByteMaskString(char c, const char (&str)[9]) { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 36 | return (str[0] == c) << 7 | (str[1] == c) << 6 | (str[2] == c) << 5 | |
| 37 | (str[3] == c) << 4 | (str[4] == c) << 3 | (str[5] == c) << 2 | |
| 38 | (str[6] == c) << 1 | (str[7] == c) << 0; |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Class for matching bit patterns such as "x1xx0000" where 'x' is allowed to be |
| 42 | // either 0 or 1. |
| 43 | class BitPattern { |
| 44 | public: |
oprypin | 60c5668 | 2017-03-24 03:22:49 -0700 | [diff] [blame] | 45 | explicit constexpr BitPattern(const char (&str)[9]) |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 46 | : mask_(~ByteMaskString('x', str)), |
| 47 | masked_value_(ByteMaskString('1', str)) {} |
| 48 | |
| 49 | bool IsMatch(uint8_t value) const { return masked_value_ == (value & mask_); } |
| 50 | |
| 51 | private: |
| 52 | const uint8_t mask_; |
| 53 | const uint8_t masked_value_; |
| 54 | }; |
| 55 | |
| 56 | // Table for converting between profile_idc/profile_iop to H264::Profile. |
| 57 | struct ProfilePattern { |
| 58 | const uint8_t profile_idc; |
| 59 | const BitPattern profile_iop; |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 60 | const Profile profile; |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // This is from https://tools.ietf.org/html/rfc6184#section-8.1. |
| 64 | constexpr ProfilePattern kProfilePatterns[] = { |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 65 | {0x42, BitPattern("x1xx0000"), kProfileConstrainedBaseline}, |
| 66 | {0x4D, BitPattern("1xxx0000"), kProfileConstrainedBaseline}, |
| 67 | {0x58, BitPattern("11xx0000"), kProfileConstrainedBaseline}, |
| 68 | {0x42, BitPattern("x0xx0000"), kProfileBaseline}, |
| 69 | {0x58, BitPattern("10xx0000"), kProfileBaseline}, |
| 70 | {0x4D, BitPattern("0x0x0000"), kProfileMain}, |
| 71 | {0x64, BitPattern("00000000"), kProfileHigh}, |
| 72 | {0x64, BitPattern("00001100"), kProfileConstrainedHigh}}; |
| 73 | |
| 74 | // Compare H264 levels and handle the level 1b case. |
| 75 | bool IsLess(Level a, Level b) { |
| 76 | if (a == kLevel1_b) |
| 77 | return b != kLevel1 && b != kLevel1_b; |
| 78 | if (b == kLevel1_b) |
| 79 | return a == kLevel1; |
| 80 | return a < b; |
| 81 | } |
| 82 | |
| 83 | Level Min(Level a, Level b) { |
| 84 | return IsLess(a, b) ? a : b; |
| 85 | } |
| 86 | |
| 87 | bool IsLevelAsymmetryAllowed(const CodecParameterMap& params) { |
| 88 | const auto it = params.find(kLevelAsymmetryAllowed); |
| 89 | return it != params.end() && strcmp(it->second.c_str(), "1") == 0; |
| 90 | } |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 91 | |
magjed | a92704e | 2016-11-08 02:57:51 -0800 | [diff] [blame] | 92 | struct LevelConstraint { |
| 93 | const int max_macroblocks_per_second; |
| 94 | const int max_macroblock_frame_size; |
| 95 | const webrtc::H264::Level level; |
| 96 | }; |
| 97 | |
| 98 | // This is from ITU-T H.264 (02/2016) Table A-1 – Level limits. |
| 99 | static constexpr LevelConstraint kLevelConstraints[] = { |
| 100 | {1485, 99, webrtc::H264::kLevel1}, |
| 101 | {1485, 99, webrtc::H264::kLevel1_b}, |
| 102 | {3000, 396, webrtc::H264::kLevel1_1}, |
| 103 | {6000, 396, webrtc::H264::kLevel1_2}, |
| 104 | {11880, 396, webrtc::H264::kLevel1_3}, |
| 105 | {11880, 396, webrtc::H264::kLevel2}, |
| 106 | {19800, 792, webrtc::H264::kLevel2_1}, |
| 107 | {20250, 1620, webrtc::H264::kLevel2_2}, |
| 108 | {40500, 1620, webrtc::H264::kLevel3}, |
| 109 | {108000, 3600, webrtc::H264::kLevel3_1}, |
| 110 | {216000, 5120, webrtc::H264::kLevel3_2}, |
| 111 | {245760, 8192, webrtc::H264::kLevel4}, |
| 112 | {245760, 8192, webrtc::H264::kLevel4_1}, |
| 113 | {522240, 8704, webrtc::H264::kLevel4_2}, |
| 114 | {589824, 22080, webrtc::H264::kLevel5}, |
Yura Yaroshevich | 6136314 | 2018-04-20 20:54:39 +0300 | [diff] [blame] | 115 | {983040, 36864, webrtc::H264::kLevel5_1}, |
| 116 | {2073600, 36864, webrtc::H264::kLevel5_2}, |
magjed | a92704e | 2016-11-08 02:57:51 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 119 | } // anonymous namespace |
| 120 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 121 | absl::optional<ProfileLevelId> ParseProfileLevelId(const char* str) { |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 122 | // The string should consist of 3 bytes in hexadecimal format. |
| 123 | if (strlen(str) != 6u) |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 124 | return absl::nullopt; |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 125 | const uint32_t profile_level_id_numeric = strtol(str, nullptr, 16); |
| 126 | if (profile_level_id_numeric == 0) |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 127 | return absl::nullopt; |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 128 | |
| 129 | // Separate into three bytes. |
| 130 | const uint8_t level_idc = |
| 131 | static_cast<uint8_t>(profile_level_id_numeric & 0xFF); |
| 132 | const uint8_t profile_iop = |
| 133 | static_cast<uint8_t>((profile_level_id_numeric >> 8) & 0xFF); |
| 134 | const uint8_t profile_idc = |
| 135 | static_cast<uint8_t>((profile_level_id_numeric >> 16) & 0xFF); |
| 136 | |
| 137 | // Parse level based on level_idc and constraint set 3 flag. |
| 138 | Level level; |
| 139 | switch (level_idc) { |
| 140 | case kLevel1_1: |
| 141 | level = (profile_iop & kConstraintSet3Flag) != 0 ? kLevel1_b : kLevel1_1; |
| 142 | break; |
| 143 | case kLevel1: |
| 144 | case kLevel1_2: |
| 145 | case kLevel1_3: |
| 146 | case kLevel2: |
| 147 | case kLevel2_1: |
| 148 | case kLevel2_2: |
| 149 | case kLevel3: |
| 150 | case kLevel3_1: |
| 151 | case kLevel3_2: |
| 152 | case kLevel4: |
| 153 | case kLevel4_1: |
| 154 | case kLevel4_2: |
| 155 | case kLevel5: |
| 156 | case kLevel5_1: |
| 157 | case kLevel5_2: |
| 158 | level = static_cast<Level>(level_idc); |
| 159 | break; |
| 160 | default: |
| 161 | // Unrecognized level_idc. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 162 | return absl::nullopt; |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Parse profile_idc/profile_iop into a Profile enum. |
| 166 | for (const ProfilePattern& pattern : kProfilePatterns) { |
| 167 | if (profile_idc == pattern.profile_idc && |
| 168 | pattern.profile_iop.IsMatch(profile_iop)) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 169 | return ProfileLevelId(pattern.profile, level); |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | // Unrecognized profile_idc/profile_iop combination. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 174 | return absl::nullopt; |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 177 | absl::optional<Level> SupportedLevel(int max_frame_pixel_count, float max_fps) { |
magjed | a92704e | 2016-11-08 02:57:51 -0800 | [diff] [blame] | 178 | static const int kPixelsPerMacroblock = 16 * 16; |
| 179 | |
| 180 | for (int i = arraysize(kLevelConstraints) - 1; i >= 0; --i) { |
| 181 | const LevelConstraint& level_constraint = kLevelConstraints[i]; |
| 182 | if (level_constraint.max_macroblock_frame_size * kPixelsPerMacroblock <= |
| 183 | max_frame_pixel_count && |
| 184 | level_constraint.max_macroblocks_per_second <= |
| 185 | max_fps * level_constraint.max_macroblock_frame_size) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 186 | return level_constraint.level; |
magjed | a92704e | 2016-11-08 02:57:51 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | // No level supported. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 191 | return absl::nullopt; |
magjed | a92704e | 2016-11-08 02:57:51 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 194 | absl::optional<ProfileLevelId> ParseSdpProfileLevelId( |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 195 | const CodecParameterMap& params) { |
| 196 | // TODO(magjed): The default should really be kProfileBaseline and kLevel1 |
| 197 | // according to the spec: https://tools.ietf.org/html/rfc6184#section-8.1. In |
| 198 | // order to not break backwards compatibility with older versions of WebRTC |
| 199 | // where external codecs don't have any parameters, use |
| 200 | // kProfileConstrainedBaseline kLevel3_1 instead. This workaround will only be |
| 201 | // done in an interim period to allow external clients to update their code. |
| 202 | // http://crbug/webrtc/6337. |
| 203 | static const ProfileLevelId kDefaultProfileLevelId( |
| 204 | kProfileConstrainedBaseline, kLevel3_1); |
| 205 | |
| 206 | const auto profile_level_id_it = params.find(kProfileLevelId); |
| 207 | return (profile_level_id_it == params.end()) |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 208 | ? kDefaultProfileLevelId |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 209 | : ParseProfileLevelId(profile_level_id_it->second.c_str()); |
| 210 | } |
| 211 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 212 | absl::optional<std::string> ProfileLevelIdToString( |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 213 | const ProfileLevelId& profile_level_id) { |
| 214 | // Handle special case level == 1b. |
| 215 | if (profile_level_id.level == kLevel1_b) { |
| 216 | switch (profile_level_id.profile) { |
| 217 | case kProfileConstrainedBaseline: |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 218 | return {"42f00b"}; |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 219 | case kProfileBaseline: |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 220 | return {"42100b"}; |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 221 | case kProfileMain: |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 222 | return {"4d100b"}; |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 223 | // Level 1b is not allowed for other profiles. |
| 224 | default: |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 225 | return absl::nullopt; |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | const char* profile_idc_iop_string; |
| 230 | switch (profile_level_id.profile) { |
| 231 | case kProfileConstrainedBaseline: |
| 232 | profile_idc_iop_string = "42e0"; |
| 233 | break; |
| 234 | case kProfileBaseline: |
| 235 | profile_idc_iop_string = "4200"; |
| 236 | break; |
| 237 | case kProfileMain: |
magjed | f236033 | 2016-11-04 06:25:32 -0700 | [diff] [blame] | 238 | profile_idc_iop_string = "4d00"; |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 239 | break; |
| 240 | case kProfileConstrainedHigh: |
| 241 | profile_idc_iop_string = "640c"; |
| 242 | break; |
| 243 | case kProfileHigh: |
| 244 | profile_idc_iop_string = "6400"; |
| 245 | break; |
| 246 | // Unrecognized profile. |
| 247 | default: |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 248 | return absl::nullopt; |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | char str[7]; |
| 252 | snprintf(str, 7u, "%s%02x", profile_idc_iop_string, profile_level_id.level); |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 253 | return {str}; |
magjed | b92184b | 2016-11-03 06:50:28 -0700 | [diff] [blame] | 254 | } |
| 255 | |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 256 | // Set level according to https://tools.ietf.org/html/rfc6184#section-8.2.2. |
| 257 | void GenerateProfileLevelIdForAnswer( |
| 258 | const CodecParameterMap& local_supported_params, |
| 259 | const CodecParameterMap& remote_offered_params, |
| 260 | CodecParameterMap* answer_params) { |
magjed | f823ede | 2016-11-12 09:53:04 -0800 | [diff] [blame] | 261 | // If both local and remote haven't set profile-level-id, they are both using |
| 262 | // the default profile. In this case, don't set profile-level-id in answer |
| 263 | // either. |
| 264 | if (!local_supported_params.count(kProfileLevelId) && |
| 265 | !remote_offered_params.count(kProfileLevelId)) { |
| 266 | return; |
| 267 | } |
| 268 | |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 269 | // Parse profile-level-ids. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 270 | const absl::optional<ProfileLevelId> local_profile_level_id = |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 271 | ParseSdpProfileLevelId(local_supported_params); |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 272 | const absl::optional<ProfileLevelId> remote_profile_level_id = |
magjed | 59be5f7 | 2016-11-10 01:57:37 -0800 | [diff] [blame] | 273 | ParseSdpProfileLevelId(remote_offered_params); |
| 274 | // The local and remote codec must have valid and equal H264 Profiles. |
| 275 | RTC_DCHECK(local_profile_level_id); |
| 276 | RTC_DCHECK(remote_profile_level_id); |
| 277 | RTC_DCHECK_EQ(local_profile_level_id->profile, |
| 278 | remote_profile_level_id->profile); |
| 279 | |
| 280 | // Parse level information. |
| 281 | const bool level_asymmetry_allowed = |
| 282 | IsLevelAsymmetryAllowed(local_supported_params) && |
| 283 | IsLevelAsymmetryAllowed(remote_offered_params); |
| 284 | const Level local_level = local_profile_level_id->level; |
| 285 | const Level remote_level = remote_profile_level_id->level; |
| 286 | const Level min_level = Min(local_level, remote_level); |
| 287 | |
| 288 | // Determine answer level. When level asymmetry is not allowed, level upgrade |
| 289 | // is not allowed, i.e., the level in the answer must be equal to or lower |
| 290 | // than the level in the offer. |
| 291 | const Level answer_level = level_asymmetry_allowed ? local_level : min_level; |
| 292 | |
| 293 | // Set the resulting profile-level-id in the answer parameters. |
| 294 | (*answer_params)[kProfileLevelId] = *ProfileLevelIdToString( |
| 295 | ProfileLevelId(local_profile_level_id->profile, answer_level)); |
| 296 | } |
| 297 | |
Magnus Jedvert | 523589d | 2017-11-23 13:24:53 +0100 | [diff] [blame] | 298 | bool IsSameH264Profile(const CodecParameterMap& params1, |
| 299 | const CodecParameterMap& params2) { |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 300 | const absl::optional<webrtc::H264::ProfileLevelId> profile_level_id = |
Magnus Jedvert | 523589d | 2017-11-23 13:24:53 +0100 | [diff] [blame] | 301 | webrtc::H264::ParseSdpProfileLevelId(params1); |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 302 | const absl::optional<webrtc::H264::ProfileLevelId> other_profile_level_id = |
Magnus Jedvert | 523589d | 2017-11-23 13:24:53 +0100 | [diff] [blame] | 303 | webrtc::H264::ParseSdpProfileLevelId(params2); |
| 304 | // Compare H264 profiles, but not levels. |
| 305 | return profile_level_id && other_profile_level_id && |
| 306 | profile_level_id->profile == other_profile_level_id->profile; |
| 307 | } |
| 308 | |
magjed | fffc1e5 | 2016-10-31 05:55:57 -0700 | [diff] [blame] | 309 | } // namespace H264 |
| 310 | } // namespace webrtc |