• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2021 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 #include "api/video_codecs/h264_profile_level_id.h"
12 
13 #include <cstdio>
14 #include <cstdlib>
15 #include <string>
16 
17 #include "rtc_base/arraysize.h"
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 
22 namespace {
23 
24 const char kProfileLevelId[] = "profile-level-id";
25 
26 // For level_idc=11 and profile_idc=0x42, 0x4D, or 0x58, the constraint set3
27 // flag specifies if level 1b or level 1.1 is used.
28 const uint8_t kConstraintSet3Flag = 0x10;
29 
30 // Convert a string of 8 characters into a byte where the positions containing
31 // character c will have their bit set. For example, c = 'x', str = "x1xx0000"
32 // will return 0b10110000. constexpr is used so that the pattern table in
33 // kProfilePatterns is statically initialized.
ByteMaskString(char c,const char (& str)[9])34 constexpr uint8_t ByteMaskString(char c, const char (&str)[9]) {
35   return (str[0] == c) << 7 | (str[1] == c) << 6 | (str[2] == c) << 5 |
36          (str[3] == c) << 4 | (str[4] == c) << 3 | (str[5] == c) << 2 |
37          (str[6] == c) << 1 | (str[7] == c) << 0;
38 }
39 
40 // Class for matching bit patterns such as "x1xx0000" where 'x' is allowed to be
41 // either 0 or 1.
42 class BitPattern {
43  public:
BitPattern(const char (& str)[9])44   explicit constexpr BitPattern(const char (&str)[9])
45       : mask_(~ByteMaskString('x', str)),
46         masked_value_(ByteMaskString('1', str)) {}
47 
IsMatch(uint8_t value) const48   bool IsMatch(uint8_t value) const { return masked_value_ == (value & mask_); }
49 
50  private:
51   const uint8_t mask_;
52   const uint8_t masked_value_;
53 };
54 
55 // Table for converting between profile_idc/profile_iop to H264Profile.
56 struct ProfilePattern {
57   const uint8_t profile_idc;
58   const BitPattern profile_iop;
59   const H264Profile profile;
60 };
61 
62 // This is from https://tools.ietf.org/html/rfc6184#section-8.1.
63 constexpr ProfilePattern kProfilePatterns[] = {
64     {0x42, BitPattern("x1xx0000"), H264Profile::kProfileConstrainedBaseline},
65     {0x4D, BitPattern("1xxx0000"), H264Profile::kProfileConstrainedBaseline},
66     {0x58, BitPattern("11xx0000"), H264Profile::kProfileConstrainedBaseline},
67     {0x42, BitPattern("x0xx0000"), H264Profile::kProfileBaseline},
68     {0x58, BitPattern("10xx0000"), H264Profile::kProfileBaseline},
69     {0x4D, BitPattern("0x0x0000"), H264Profile::kProfileMain},
70     {0x64, BitPattern("00000000"), H264Profile::kProfileHigh},
71     {0x64, BitPattern("00001100"), H264Profile::kProfileConstrainedHigh},
72     {0xF4, BitPattern("00000000"), H264Profile::kProfilePredictiveHigh444}};
73 
74 struct LevelConstraint {
75   const int max_macroblocks_per_second;
76   const int max_macroblock_frame_size;
77   const H264Level level;
78 };
79 
80 // This is from ITU-T H.264 (02/2016) Table A-1 – Level limits.
81 static constexpr LevelConstraint kLevelConstraints[] = {
82     {1485, 99, H264Level::kLevel1},
83     {1485, 99, H264Level::kLevel1_b},
84     {3000, 396, H264Level::kLevel1_1},
85     {6000, 396, H264Level::kLevel1_2},
86     {11880, 396, H264Level::kLevel1_3},
87     {11880, 396, H264Level::kLevel2},
88     {19800, 792, H264Level::kLevel2_1},
89     {20250, 1620, H264Level::kLevel2_2},
90     {40500, 1620, H264Level::kLevel3},
91     {108000, 3600, H264Level::kLevel3_1},
92     {216000, 5120, H264Level::kLevel3_2},
93     {245760, 8192, H264Level::kLevel4},
94     {245760, 8192, H264Level::kLevel4_1},
95     {522240, 8704, H264Level::kLevel4_2},
96     {589824, 22080, H264Level::kLevel5},
97     {983040, 36864, H264Level::kLevel5_1},
98     {2073600, 36864, H264Level::kLevel5_2},
99 };
100 
101 }  // anonymous namespace
102 
ParseH264ProfileLevelId(const char * str)103 absl::optional<H264ProfileLevelId> ParseH264ProfileLevelId(const char* str) {
104   // The string should consist of 3 bytes in hexadecimal format.
105   if (strlen(str) != 6u)
106     return absl::nullopt;
107   const uint32_t profile_level_id_numeric = strtol(str, nullptr, 16);
108   if (profile_level_id_numeric == 0)
109     return absl::nullopt;
110 
111   // Separate into three bytes.
112   const uint8_t level_idc =
113       static_cast<uint8_t>(profile_level_id_numeric & 0xFF);
114   const uint8_t profile_iop =
115       static_cast<uint8_t>((profile_level_id_numeric >> 8) & 0xFF);
116   const uint8_t profile_idc =
117       static_cast<uint8_t>((profile_level_id_numeric >> 16) & 0xFF);
118 
119   // Parse level based on level_idc and constraint set 3 flag.
120   H264Level level_casted = static_cast<H264Level>(level_idc);
121   H264Level level;
122 
123   switch (level_casted) {
124     case H264Level::kLevel1_1:
125       level = (profile_iop & kConstraintSet3Flag) != 0 ? H264Level::kLevel1_b
126                                                        : H264Level::kLevel1_1;
127       break;
128     case H264Level::kLevel1:
129     case H264Level::kLevel1_2:
130     case H264Level::kLevel1_3:
131     case H264Level::kLevel2:
132     case H264Level::kLevel2_1:
133     case H264Level::kLevel2_2:
134     case H264Level::kLevel3:
135     case H264Level::kLevel3_1:
136     case H264Level::kLevel3_2:
137     case H264Level::kLevel4:
138     case H264Level::kLevel4_1:
139     case H264Level::kLevel4_2:
140     case H264Level::kLevel5:
141     case H264Level::kLevel5_1:
142     case H264Level::kLevel5_2:
143       level = level_casted;
144       break;
145     default:
146       // Unrecognized level_idc.
147       return absl::nullopt;
148   }
149 
150   // Parse profile_idc/profile_iop into a Profile enum.
151   for (const ProfilePattern& pattern : kProfilePatterns) {
152     if (profile_idc == pattern.profile_idc &&
153         pattern.profile_iop.IsMatch(profile_iop)) {
154       return H264ProfileLevelId(pattern.profile, level);
155     }
156   }
157 
158   // Unrecognized profile_idc/profile_iop combination.
159   return absl::nullopt;
160 }
161 
H264SupportedLevel(int max_frame_pixel_count,float max_fps)162 absl::optional<H264Level> H264SupportedLevel(int max_frame_pixel_count,
163                                              float max_fps) {
164   static const int kPixelsPerMacroblock = 16 * 16;
165 
166   for (int i = arraysize(kLevelConstraints) - 1; i >= 0; --i) {
167     const LevelConstraint& level_constraint = kLevelConstraints[i];
168     if (level_constraint.max_macroblock_frame_size * kPixelsPerMacroblock <=
169             max_frame_pixel_count &&
170         level_constraint.max_macroblocks_per_second <=
171             max_fps * level_constraint.max_macroblock_frame_size) {
172       return level_constraint.level;
173     }
174   }
175 
176   // No level supported.
177   return absl::nullopt;
178 }
179 
ParseSdpForH264ProfileLevelId(const SdpVideoFormat::Parameters & params)180 absl::optional<H264ProfileLevelId> ParseSdpForH264ProfileLevelId(
181     const SdpVideoFormat::Parameters& params) {
182   // TODO(magjed): The default should really be kProfileBaseline and kLevel1
183   // according to the spec: https://tools.ietf.org/html/rfc6184#section-8.1. In
184   // order to not break backwards compatibility with older versions of WebRTC
185   // where external codecs don't have any parameters, use
186   // kProfileConstrainedBaseline kLevel3_1 instead. This workaround will only be
187   // done in an interim period to allow external clients to update their code.
188   // http://crbug/webrtc/6337.
189   static const H264ProfileLevelId kDefaultProfileLevelId(
190       H264Profile::kProfileConstrainedBaseline, H264Level::kLevel3_1);
191 
192   const auto profile_level_id_it = params.find(kProfileLevelId);
193   return (profile_level_id_it == params.end())
194              ? kDefaultProfileLevelId
195              : ParseH264ProfileLevelId(profile_level_id_it->second.c_str());
196 }
197 
H264ProfileLevelIdToString(const H264ProfileLevelId & profile_level_id)198 absl::optional<std::string> H264ProfileLevelIdToString(
199     const H264ProfileLevelId& profile_level_id) {
200   // Handle special case level == 1b.
201   if (profile_level_id.level == H264Level::kLevel1_b) {
202     switch (profile_level_id.profile) {
203       case H264Profile::kProfileConstrainedBaseline:
204         return {"42f00b"};
205       case H264Profile::kProfileBaseline:
206         return {"42100b"};
207       case H264Profile::kProfileMain:
208         return {"4d100b"};
209       // Level 1b is not allowed for other profiles.
210       default:
211         return absl::nullopt;
212     }
213   }
214 
215   const char* profile_idc_iop_string;
216   switch (profile_level_id.profile) {
217     case H264Profile::kProfileConstrainedBaseline:
218       profile_idc_iop_string = "42e0";
219       break;
220     case H264Profile::kProfileBaseline:
221       profile_idc_iop_string = "4200";
222       break;
223     case H264Profile::kProfileMain:
224       profile_idc_iop_string = "4d00";
225       break;
226     case H264Profile::kProfileConstrainedHigh:
227       profile_idc_iop_string = "640c";
228       break;
229     case H264Profile::kProfileHigh:
230       profile_idc_iop_string = "6400";
231       break;
232     case H264Profile::kProfilePredictiveHigh444:
233       profile_idc_iop_string = "f400";
234       break;
235     // Unrecognized profile.
236     default:
237       return absl::nullopt;
238   }
239 
240   char str[7];
241   snprintf(str, 7u, "%s%02x", profile_idc_iop_string, profile_level_id.level);
242   return {str};
243 }
244 
H264IsSameProfile(const SdpVideoFormat::Parameters & params1,const SdpVideoFormat::Parameters & params2)245 bool H264IsSameProfile(const SdpVideoFormat::Parameters& params1,
246                        const SdpVideoFormat::Parameters& params2) {
247   const absl::optional<H264ProfileLevelId> profile_level_id =
248       ParseSdpForH264ProfileLevelId(params1);
249   const absl::optional<H264ProfileLevelId> other_profile_level_id =
250       ParseSdpForH264ProfileLevelId(params2);
251   // Compare H264 profiles, but not levels.
252   return profile_level_id && other_profile_level_id &&
253          profile_level_id->profile == other_profile_level_id->profile;
254 }
255 
256 }  // namespace webrtc
257