blob: b4ff88369c61489f4f851b95c546c271a9c0fdf8 [file] [log] [blame]
zhihuang38ede132017-06-15 12:52:32 -07001/*
2 * Copyright (c) 2017 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_
12#define MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_
zhihuang38ede132017-06-15 12:52:32 -070013
14#include <map>
15#include <string>
16
Danil Chapovalov00c71832018-06-15 15:58:38 +020017#include "absl/types/optional.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
zhihuang38ede132017-06-15 12:52:32 -070019
20namespace webrtc {
21namespace H264 {
22
23// Map containting SDP codec parameters.
24typedef std::map<std::string, std::string> CodecParameterMap;
25
26// All values are equal to ten times the level number, except level 1b which is
27// special.
28enum Level {
29 kLevel1_b = 0,
30 kLevel1 = 10,
31 kLevel1_1 = 11,
32 kLevel1_2 = 12,
33 kLevel1_3 = 13,
34 kLevel2 = 20,
35 kLevel2_1 = 21,
36 kLevel2_2 = 22,
37 kLevel3 = 30,
38 kLevel3_1 = 31,
39 kLevel3_2 = 32,
40 kLevel4 = 40,
41 kLevel4_1 = 41,
42 kLevel4_2 = 42,
43 kLevel5 = 50,
44 kLevel5_1 = 51,
45 kLevel5_2 = 52
46};
47
48struct ProfileLevelId {
Yura Yaroshevich1d871482018-05-02 13:06:34 +030049 constexpr ProfileLevelId(Profile profile, Level level)
zhihuang38ede132017-06-15 12:52:32 -070050 : profile(profile), level(level) {}
51 Profile profile;
52 Level level;
53};
54
55// Parse profile level id that is represented as a string of 3 hex bytes.
56// Nothing will be returned if the string is not a recognized H264
57// profile level id.
Danil Chapovalov00c71832018-06-15 15:58:38 +020058absl::optional<ProfileLevelId> ParseProfileLevelId(const char* str);
zhihuang38ede132017-06-15 12:52:32 -070059
60// Parse profile level id that is represented as a string of 3 hex bytes
61// contained in an SDP key-value map. A default profile level id will be
62// returned if the profile-level-id key is missing. Nothing will be returned if
63// the key is present but the string is invalid.
Danil Chapovalov00c71832018-06-15 15:58:38 +020064absl::optional<ProfileLevelId> ParseSdpProfileLevelId(
zhihuang38ede132017-06-15 12:52:32 -070065 const CodecParameterMap& params);
66
67// Given that a decoder supports up to a given frame size (in pixels) at up to a
68// given number of frames per second, return the highest H.264 level where it
69// can guarantee that it will be able to support all valid encoded streams that
70// are within that level.
Danil Chapovalov00c71832018-06-15 15:58:38 +020071absl::optional<Level> SupportedLevel(int max_frame_pixel_count, float max_fps);
zhihuang38ede132017-06-15 12:52:32 -070072
73// Returns canonical string representation as three hex bytes of the profile
74// level id, or returns nothing for invalid profile level ids.
Danil Chapovalov00c71832018-06-15 15:58:38 +020075absl::optional<std::string> ProfileLevelIdToString(
zhihuang38ede132017-06-15 12:52:32 -070076 const ProfileLevelId& profile_level_id);
77
78// Generate codec parameters that will be used as answer in an SDP negotiation
79// based on local supported parameters and remote offered parameters. Both
80// |local_supported_params|, |remote_offered_params|, and |answer_params|
81// represent sendrecv media descriptions, i.e they are a mix of both encode and
82// decode capabilities. In theory, when the profile in |local_supported_params|
83// represent a strict superset of the profile in |remote_offered_params|, we
84// could limit the profile in |answer_params| to the profile in
85// |remote_offered_params|. However, to simplify the code, each supported H264
86// profile should be listed explicitly in the list of local supported codecs,
87// even if they are redundant. Then each local codec in the list should be
88// tested one at a time against the remote codec, and only when the profiles are
89// equal should this function be called. Therefore, this function does not need
90// to handle profile intersection, and the profile of |local_supported_params|
91// and |remote_offered_params| must be equal before calling this function. The
92// parameters that are used when negotiating are the level part of
93// profile-level-id and level-asymmetry-allowed.
94void GenerateProfileLevelIdForAnswer(
95 const CodecParameterMap& local_supported_params,
96 const CodecParameterMap& remote_offered_params,
97 CodecParameterMap* answer_params);
98
Magnus Jedvert523589d2017-11-23 13:24:53 +010099// Returns true if the parameters have the same H264 profile, i.e. the same
100// H264::Profile (Baseline, High, etc).
101bool IsSameH264Profile(const CodecParameterMap& params1,
102 const CodecParameterMap& params2);
103
zhihuang38ede132017-06-15 12:52:32 -0700104} // namespace H264
105} // namespace webrtc
106
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200107#endif // MEDIA_BASE_H264_PROFILE_LEVEL_ID_H_