blob: 65a6cf0cc913140c2a5cfbc6404b53c7af0d697b [file] [log] [blame]
magjedfffc1e52016-10-31 05:55:57 -07001/*
zhihuang38ede132017-06-15 12:52:32 -07002 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
magjedfffc1e52016-10-31 05:55:57 -07003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "media/base/h264_profile_level_id.h"
magjedfffc1e52016-10-31 05:55:57 -070012
magjedb92184b2016-11-03 06:50:28 -070013#include <cstdio>
magjedfffc1e52016-10-31 05:55:57 -070014#include <cstdlib>
15#include <cstring>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/arraysize.h"
magjeda92704e2016-11-08 02:57:51 -080018
magjed59be5f72016-11-10 01:57:37 -080019namespace webrtc {
20namespace H264 {
21
magjedfffc1e52016-10-31 05:55:57 -070022namespace {
23
magjed59be5f72016-11-10 01:57:37 -080024const char kProfileLevelId[] = "profile-level-id";
25const char kLevelAsymmetryAllowed[] = "level-asymmetry-allowed";
26
magjedfffc1e52016-10-31 05:55:57 -070027// 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.
29const 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.
35constexpr uint8_t ByteMaskString(char c, const char (&str)[9]) {
zhihuang38ede132017-06-15 12:52:32 -070036 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;
magjedfffc1e52016-10-31 05:55:57 -070039}
40
41// Class for matching bit patterns such as "x1xx0000" where 'x' is allowed to be
42// either 0 or 1.
43class BitPattern {
44 public:
oprypin60c56682017-03-24 03:22:49 -070045 explicit constexpr BitPattern(const char (&str)[9])
magjedfffc1e52016-10-31 05:55:57 -070046 : 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.
57struct ProfilePattern {
58 const uint8_t profile_idc;
59 const BitPattern profile_iop;
magjed59be5f72016-11-10 01:57:37 -080060 const Profile profile;
magjedfffc1e52016-10-31 05:55:57 -070061};
62
63// This is from https://tools.ietf.org/html/rfc6184#section-8.1.
64constexpr ProfilePattern kProfilePatterns[] = {
magjed59be5f72016-11-10 01:57:37 -080065 {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.
75bool 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
83Level Min(Level a, Level b) {
84 return IsLess(a, b) ? a : b;
85}
86
87bool IsLevelAsymmetryAllowed(const CodecParameterMap& params) {
88 const auto it = params.find(kLevelAsymmetryAllowed);
89 return it != params.end() && strcmp(it->second.c_str(), "1") == 0;
90}
magjedfffc1e52016-10-31 05:55:57 -070091
magjeda92704e2016-11-08 02:57:51 -080092struct 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.
99static 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 Yaroshevich61363142018-04-20 20:54:39 +0300115 {983040, 36864, webrtc::H264::kLevel5_1},
116 {2073600, 36864, webrtc::H264::kLevel5_2},
magjeda92704e2016-11-08 02:57:51 -0800117};
118
magjedfffc1e52016-10-31 05:55:57 -0700119} // anonymous namespace
120
Danil Chapovalov00c71832018-06-15 15:58:38 +0200121absl::optional<ProfileLevelId> ParseProfileLevelId(const char* str) {
magjedfffc1e52016-10-31 05:55:57 -0700122 // The string should consist of 3 bytes in hexadecimal format.
123 if (strlen(str) != 6u)
Danil Chapovalov00c71832018-06-15 15:58:38 +0200124 return absl::nullopt;
magjedfffc1e52016-10-31 05:55:57 -0700125 const uint32_t profile_level_id_numeric = strtol(str, nullptr, 16);
126 if (profile_level_id_numeric == 0)
Danil Chapovalov00c71832018-06-15 15:58:38 +0200127 return absl::nullopt;
magjedfffc1e52016-10-31 05:55:57 -0700128
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 Chapovalov00c71832018-06-15 15:58:38 +0200162 return absl::nullopt;
magjedfffc1e52016-10-31 05:55:57 -0700163 }
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 Sundbom78807582017-11-16 11:09:55 +0100169 return ProfileLevelId(pattern.profile, level);
magjedfffc1e52016-10-31 05:55:57 -0700170 }
171 }
172
173 // Unrecognized profile_idc/profile_iop combination.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200174 return absl::nullopt;
magjedfffc1e52016-10-31 05:55:57 -0700175}
176
Danil Chapovalov00c71832018-06-15 15:58:38 +0200177absl::optional<Level> SupportedLevel(int max_frame_pixel_count, float max_fps) {
magjeda92704e2016-11-08 02:57:51 -0800178 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 Sundbom78807582017-11-16 11:09:55 +0100186 return level_constraint.level;
magjeda92704e2016-11-08 02:57:51 -0800187 }
188 }
189
190 // No level supported.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200191 return absl::nullopt;
magjeda92704e2016-11-08 02:57:51 -0800192}
193
Danil Chapovalov00c71832018-06-15 15:58:38 +0200194absl::optional<ProfileLevelId> ParseSdpProfileLevelId(
magjed59be5f72016-11-10 01:57:37 -0800195 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 Sundbom78807582017-11-16 11:09:55 +0100208 ? kDefaultProfileLevelId
magjed59be5f72016-11-10 01:57:37 -0800209 : ParseProfileLevelId(profile_level_id_it->second.c_str());
210}
211
Danil Chapovalov00c71832018-06-15 15:58:38 +0200212absl::optional<std::string> ProfileLevelIdToString(
magjedb92184b2016-11-03 06:50:28 -0700213 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 Sundbom78807582017-11-16 11:09:55 +0100218 return {"42f00b"};
magjedb92184b2016-11-03 06:50:28 -0700219 case kProfileBaseline:
Oskar Sundbom78807582017-11-16 11:09:55 +0100220 return {"42100b"};
magjedb92184b2016-11-03 06:50:28 -0700221 case kProfileMain:
Oskar Sundbom78807582017-11-16 11:09:55 +0100222 return {"4d100b"};
magjedb92184b2016-11-03 06:50:28 -0700223 // Level 1b is not allowed for other profiles.
224 default:
Danil Chapovalov00c71832018-06-15 15:58:38 +0200225 return absl::nullopt;
magjedb92184b2016-11-03 06:50:28 -0700226 }
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:
magjedf2360332016-11-04 06:25:32 -0700238 profile_idc_iop_string = "4d00";
magjedb92184b2016-11-03 06:50:28 -0700239 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 Chapovalov00c71832018-06-15 15:58:38 +0200248 return absl::nullopt;
magjedb92184b2016-11-03 06:50:28 -0700249 }
250
251 char str[7];
252 snprintf(str, 7u, "%s%02x", profile_idc_iop_string, profile_level_id.level);
Oskar Sundbom78807582017-11-16 11:09:55 +0100253 return {str};
magjedb92184b2016-11-03 06:50:28 -0700254}
255
magjed59be5f72016-11-10 01:57:37 -0800256// Set level according to https://tools.ietf.org/html/rfc6184#section-8.2.2.
257void GenerateProfileLevelIdForAnswer(
258 const CodecParameterMap& local_supported_params,
259 const CodecParameterMap& remote_offered_params,
260 CodecParameterMap* answer_params) {
magjedf823ede2016-11-12 09:53:04 -0800261 // 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
magjed59be5f72016-11-10 01:57:37 -0800269 // Parse profile-level-ids.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200270 const absl::optional<ProfileLevelId> local_profile_level_id =
magjed59be5f72016-11-10 01:57:37 -0800271 ParseSdpProfileLevelId(local_supported_params);
Danil Chapovalov00c71832018-06-15 15:58:38 +0200272 const absl::optional<ProfileLevelId> remote_profile_level_id =
magjed59be5f72016-11-10 01:57:37 -0800273 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 Jedvert523589d2017-11-23 13:24:53 +0100298bool IsSameH264Profile(const CodecParameterMap& params1,
299 const CodecParameterMap& params2) {
Danil Chapovalov00c71832018-06-15 15:58:38 +0200300 const absl::optional<webrtc::H264::ProfileLevelId> profile_level_id =
Magnus Jedvert523589d2017-11-23 13:24:53 +0100301 webrtc::H264::ParseSdpProfileLevelId(params1);
Danil Chapovalov00c71832018-06-15 15:58:38 +0200302 const absl::optional<webrtc::H264::ProfileLevelId> other_profile_level_id =
Magnus Jedvert523589d2017-11-23 13:24:53 +0100303 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
magjedfffc1e52016-10-31 05:55:57 -0700309} // namespace H264
310} // namespace webrtc