Index: webrtc/common_types.cc |
diff --git a/webrtc/common_types.cc b/webrtc/common_types.cc |
index e78a4aacca75f538e663dcf00732a691ad9570f8..991f0c29df92b464929b50d27b984368b6115fcb 100644 |
--- a/webrtc/common_types.cc |
+++ b/webrtc/common_types.cc |
@@ -11,8 +11,65 @@ |
#include "webrtc/base/checks.h" |
#include "webrtc/common_types.h" |
+#include <stdlib.h> |
#include <string.h> |
+namespace { |
+ |
+// For level_idc=11 and profile_idc=0x42, 0x4D, or 0x58, the constraint set3 |
+// flag specifies if level 1b or level 1.1 is used. |
+const uint8_t kConstraintSet3Flag = 0x10; |
+ |
+// Convert a string of 8 characters into a byte where the positions containing |
+// character c will have their bit set. For example, c = 'x', str = "x1xx0000" |
+// will return 0b10110000. constexpr is used so that the pattern table in |
+// kProfilePatterns is statically initialized. |
+constexpr uint8_t ByteMaskString(char c, const char str[]) { |
+ return (str[0] == c) << 7 |
hta-webrtc
2016/10/28 09:55:22
Can you insert a DCHECK that strlen(str) == 8?
Thi
magjed_webrtc
2016/10/28 14:59:27
It's not possible to call non-constexpr functions
tommi
2016/10/28 15:02:20
as one more try, sizeof() == 9? or even specify t
|
+ | (str[1] == c) << 6 |
+ | (str[2] == c) << 5 |
+ | (str[3] == c) << 4 |
+ | (str[4] == c) << 3 |
+ | (str[5] == c) << 2 |
+ | (str[6] == c) << 1 |
+ | (str[7] == c) << 0; |
+} |
+ |
+// Class for matching bit patterns such as "x1xx0000" where 'x' is allowed to be |
+// either 0 or 1. |
+class BitPattern { |
+ public: |
+ constexpr BitPattern(const char str[]) |
+ : mask_(~ByteMaskString('x', str)), |
+ masked_value_(ByteMaskString('1', str)) {} |
+ |
+ bool isMatch(uint8_t value) const { return masked_value_ == (value & mask_); } |
tommi
2016/10/28 10:09:47
nit: IsMatch
magjed_webrtc
2016/10/28 14:59:27
Done.
|
+ |
+ private: |
+ const uint8_t mask_; |
+ const uint8_t masked_value_; |
+}; |
+ |
+// Table for converting between profile_idc/profile_iop to H264Profile. |
+struct ProfilePattern { |
+ const uint8_t profile_idc; |
+ const BitPattern profile_iop; |
+ const webrtc::H264Profile profile; |
+}; |
+ |
+// This is from https://tools.ietf.org/html/rfc6184#section-8.1. |
+const ProfilePattern kProfilePatterns[] = { |
+ {0x42, BitPattern("x1xx0000"), webrtc::kProfileConstrainedBaseline}, |
+ {0x4D, BitPattern("1xxx0000"), webrtc::kProfileConstrainedBaseline}, |
+ {0x58, BitPattern("11xx0000"), webrtc::kProfileConstrainedBaseline}, |
+ {0x42, BitPattern("x0xx0000"), webrtc::kProfileBaseline}, |
+ {0x58, BitPattern("10xx0000"), webrtc::kProfileBaseline}, |
+ {0x4D, BitPattern("0x0x0000"), webrtc::kProfileMain}, |
+ {0x64, BitPattern("00000000"), webrtc::kProfileHigh}, |
+ {0x64, BitPattern("00001100"), webrtc::kProfileConstrainedHigh}}; |
+ |
+} // anonymous namespace |
+ |
namespace webrtc { |
StreamDataCounters::StreamDataCounters() : first_packet_time_ms(-1) {} |
@@ -91,4 +148,61 @@ const VideoCodecH264& VideoCodec::H264() const { |
return codecSpecific.H264; |
} |
+rtc::Optional<H264ProfileLevelId> ParseH264ProfileLevelId(const char* str) { |
hta-webrtc
2016/10/28 09:55:23
Same comment goes for this function.
magjed_webrtc
2016/10/28 14:59:27
I check the string length in this function and ret
|
+ // The string should consist of 3 bytes in hexadecimal format. |
+ if (strlen(str) != 6u) |
+ return rtc::Optional<H264ProfileLevelId>(); |
+ const long profile_level_id_numeric = strtol(str, nullptr, 16); |
+ if (profile_level_id_numeric == 0) |
+ return rtc::Optional<H264ProfileLevelId>(); |
+ |
+ // Separate into three bytes. |
+ const uint8_t level_idc = |
+ static_cast<uint8_t>(profile_level_id_numeric & 0xFF); |
+ const uint8_t profile_iop = |
+ static_cast<uint8_t>((profile_level_id_numeric >> 8) & 0xFF); |
+ const uint8_t profile_idc = |
+ static_cast<uint8_t>((profile_level_id_numeric >> 16) & 0xFF); |
+ |
+ // Parse level based on level_idc and constraint set 3 flag. |
+ H264Level level; |
+ switch (level_idc) { |
+ case kH264Level1_1: |
+ level = (profile_iop & kConstraintSet3Flag) != 0 ? kH264Level1_b |
+ : kH264Level1_1; |
+ break; |
+ case kH264Level1: |
+ case kH264Level1_2: |
+ case kH264Level1_3: |
+ case kH264Level2: |
+ case kH264Level2_1: |
+ case kH264Level2_2: |
+ case kH264Level3: |
+ case kH264Level3_1: |
+ case kH264Level3_2: |
+ case kH264Level4: |
+ case kH264Level4_1: |
+ case kH264Level4_2: |
+ case kH264Level5: |
+ case kH264Level5_1: |
+ case kH264Level5_2: |
+ level = static_cast<H264Level>(level_idc); |
+ break; |
+ default: |
+ // Unrecognized level_idc. |
+ return rtc::Optional<H264ProfileLevelId>(); |
+ } |
+ |
+ // Parse profile_idc/profile_iop into a H264Profile enum. |
+ for (const ProfilePattern& pattern : kProfilePatterns) { |
+ if (profile_idc == pattern.profile_idc && |
+ pattern.profile_iop.isMatch(profile_iop)) { |
+ return rtc::Optional<H264ProfileLevelId>({pattern.profile, level}); |
+ } |
+ } |
+ |
+ // Unrecognized profile_idc/profile_iop combination. |
+ return rtc::Optional<H264ProfileLevelId>(); |
+} |
+ |
} // namespace webrtc |