Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: webrtc/common_video/h264/profile_level_id.cc

Issue 2470133002: Add function for getting supported H264 level from max resolution and fps (Closed)
Patch Set: Change fps type to float Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/common_video/h264/profile_level_id.h" 11 #include "webrtc/common_video/h264/profile_level_id.h"
12 12
13 #include <cstdio> 13 #include <cstdio>
14 #include <cstdlib> 14 #include <cstdlib>
15 #include <cstring> 15 #include <cstring>
16 16
17 #include "webrtc/base/arraysize.h"
18
17 namespace { 19 namespace {
18 20
19 // For level_idc=11 and profile_idc=0x42, 0x4D, or 0x58, the constraint set3 21 // For level_idc=11 and profile_idc=0x42, 0x4D, or 0x58, the constraint set3
20 // flag specifies if level 1b or level 1.1 is used. 22 // flag specifies if level 1b or level 1.1 is used.
21 const uint8_t kConstraintSet3Flag = 0x10; 23 const uint8_t kConstraintSet3Flag = 0x10;
22 24
23 // Convert a string of 8 characters into a byte where the positions containing 25 // Convert a string of 8 characters into a byte where the positions containing
24 // character c will have their bit set. For example, c = 'x', str = "x1xx0000" 26 // character c will have their bit set. For example, c = 'x', str = "x1xx0000"
25 // will return 0b10110000. constexpr is used so that the pattern table in 27 // will return 0b10110000. constexpr is used so that the pattern table in
26 // kProfilePatterns is statically initialized. 28 // kProfilePatterns is statically initialized.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 constexpr ProfilePattern kProfilePatterns[] = { 63 constexpr ProfilePattern kProfilePatterns[] = {
62 {0x42, BitPattern("x1xx0000"), webrtc::H264::kProfileConstrainedBaseline}, 64 {0x42, BitPattern("x1xx0000"), webrtc::H264::kProfileConstrainedBaseline},
63 {0x4D, BitPattern("1xxx0000"), webrtc::H264::kProfileConstrainedBaseline}, 65 {0x4D, BitPattern("1xxx0000"), webrtc::H264::kProfileConstrainedBaseline},
64 {0x58, BitPattern("11xx0000"), webrtc::H264::kProfileConstrainedBaseline}, 66 {0x58, BitPattern("11xx0000"), webrtc::H264::kProfileConstrainedBaseline},
65 {0x42, BitPattern("x0xx0000"), webrtc::H264::kProfileBaseline}, 67 {0x42, BitPattern("x0xx0000"), webrtc::H264::kProfileBaseline},
66 {0x58, BitPattern("10xx0000"), webrtc::H264::kProfileBaseline}, 68 {0x58, BitPattern("10xx0000"), webrtc::H264::kProfileBaseline},
67 {0x4D, BitPattern("0x0x0000"), webrtc::H264::kProfileMain}, 69 {0x4D, BitPattern("0x0x0000"), webrtc::H264::kProfileMain},
68 {0x64, BitPattern("00000000"), webrtc::H264::kProfileHigh}, 70 {0x64, BitPattern("00000000"), webrtc::H264::kProfileHigh},
69 {0x64, BitPattern("00001100"), webrtc::H264::kProfileConstrainedHigh}}; 71 {0x64, BitPattern("00001100"), webrtc::H264::kProfileConstrainedHigh}};
70 72
73 struct LevelConstraint {
74 const int max_macroblocks_per_second;
75 const int max_macroblock_frame_size;
76 const webrtc::H264::Level level;
77 };
78
79 // This is from ITU-T H.264 (02/2016) Table A-1 – Level limits.
80 static constexpr LevelConstraint kLevelConstraints[] = {
81 {1485, 99, webrtc::H264::kLevel1},
82 {1485, 99, webrtc::H264::kLevel1_b},
83 {3000, 396, webrtc::H264::kLevel1_1},
84 {6000, 396, webrtc::H264::kLevel1_2},
85 {11880, 396, webrtc::H264::kLevel1_3},
86 {11880, 396, webrtc::H264::kLevel2},
87 {19800, 792, webrtc::H264::kLevel2_1},
88 {20250, 1620, webrtc::H264::kLevel2_2},
89 {40500, 1620, webrtc::H264::kLevel3},
90 {108000, 3600, webrtc::H264::kLevel3_1},
91 {216000, 5120, webrtc::H264::kLevel3_2},
92 {245760, 8192, webrtc::H264::kLevel4},
93 {245760, 8192, webrtc::H264::kLevel4_1},
94 {522240, 8704, webrtc::H264::kLevel4_2},
95 {589824, 22080, webrtc::H264::kLevel5},
96 {983040, 3684, webrtc::H264::kLevel5_1},
97 {2073600, 3684, webrtc::H264::kLevel5_2},
98 };
99
71 } // anonymous namespace 100 } // anonymous namespace
72 101
73 namespace webrtc { 102 namespace webrtc {
74 namespace H264 { 103 namespace H264 {
75 104
76 rtc::Optional<ProfileLevelId> ParseProfileLevelId(const char* str) { 105 rtc::Optional<ProfileLevelId> ParseProfileLevelId(const char* str) {
77 // The string should consist of 3 bytes in hexadecimal format. 106 // The string should consist of 3 bytes in hexadecimal format.
78 if (strlen(str) != 6u) 107 if (strlen(str) != 6u)
79 return rtc::Optional<ProfileLevelId>(); 108 return rtc::Optional<ProfileLevelId>();
80 const uint32_t profile_level_id_numeric = strtol(str, nullptr, 16); 109 const uint32_t profile_level_id_numeric = strtol(str, nullptr, 16);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 if (profile_idc == pattern.profile_idc && 151 if (profile_idc == pattern.profile_idc &&
123 pattern.profile_iop.IsMatch(profile_iop)) { 152 pattern.profile_iop.IsMatch(profile_iop)) {
124 return rtc::Optional<ProfileLevelId>({pattern.profile, level}); 153 return rtc::Optional<ProfileLevelId>({pattern.profile, level});
125 } 154 }
126 } 155 }
127 156
128 // Unrecognized profile_idc/profile_iop combination. 157 // Unrecognized profile_idc/profile_iop combination.
129 return rtc::Optional<ProfileLevelId>(); 158 return rtc::Optional<ProfileLevelId>();
130 } 159 }
131 160
161 rtc::Optional<Level> SupportedLevel(int max_frame_pixel_count, float max_fps) {
162 static const int kPixelsPerMacroblock = 16 * 16;
163
164 for (int i = arraysize(kLevelConstraints) - 1; i >= 0; --i) {
165 const LevelConstraint& level_constraint = kLevelConstraints[i];
166 if (level_constraint.max_macroblock_frame_size * kPixelsPerMacroblock <=
167 max_frame_pixel_count &&
168 level_constraint.max_macroblocks_per_second <=
169 max_fps * level_constraint.max_macroblock_frame_size) {
170 return rtc::Optional<Level>(level_constraint.level);
171 }
172 }
173
174 // No level supported.
175 return rtc::Optional<Level>();
176 }
177
132 rtc::Optional<std::string> ProfileLevelIdToString( 178 rtc::Optional<std::string> ProfileLevelIdToString(
133 const ProfileLevelId& profile_level_id) { 179 const ProfileLevelId& profile_level_id) {
134 // Handle special case level == 1b. 180 // Handle special case level == 1b.
135 if (profile_level_id.level == kLevel1_b) { 181 if (profile_level_id.level == kLevel1_b) {
136 switch (profile_level_id.profile) { 182 switch (profile_level_id.profile) {
137 case kProfileConstrainedBaseline: 183 case kProfileConstrainedBaseline:
138 return rtc::Optional<std::string>("42f00b"); 184 return rtc::Optional<std::string>("42f00b");
139 case kProfileBaseline: 185 case kProfileBaseline:
140 return rtc::Optional<std::string>("42100b"); 186 return rtc::Optional<std::string>("42100b");
141 case kProfileMain: 187 case kProfileMain:
(...skipping 26 matching lines...) Expand all
168 return rtc::Optional<std::string>(); 214 return rtc::Optional<std::string>();
169 } 215 }
170 216
171 char str[7]; 217 char str[7];
172 snprintf(str, 7u, "%s%02x", profile_idc_iop_string, profile_level_id.level); 218 snprintf(str, 7u, "%s%02x", profile_idc_iop_string, profile_level_id.level);
173 return rtc::Optional<std::string>(str); 219 return rtc::Optional<std::string>(str);
174 } 220 }
175 221
176 } // namespace H264 222 } // namespace H264
177 } // namespace webrtc 223 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/common_video/h264/profile_level_id.h ('k') | webrtc/common_video/h264/profile_level_id_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698