Joe Downing | 87bcc1c | 2022-06-09 15:56:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 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/av1_profile.h" |
| 12 | |
| 13 | #include <map> |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "rtc_base/string_to_number.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | // Parameter name in the format parameter map for AV1 video. |
| 21 | const char kAV1FmtpProfile[] = "profile"; |
| 22 | |
| 23 | absl::string_view AV1ProfileToString(AV1Profile profile) { |
| 24 | switch (profile) { |
| 25 | case AV1Profile::kProfile0: |
| 26 | return "0"; |
| 27 | case AV1Profile::kProfile1: |
| 28 | return "1"; |
| 29 | case AV1Profile::kProfile2: |
| 30 | return "2"; |
| 31 | } |
| 32 | return "0"; |
| 33 | } |
| 34 | |
| 35 | absl::optional<AV1Profile> StringToAV1Profile(absl::string_view str) { |
| 36 | const absl::optional<int> i = rtc::StringToNumber<int>(str); |
| 37 | if (!i.has_value()) |
| 38 | return absl::nullopt; |
| 39 | |
| 40 | switch (i.value()) { |
| 41 | case 0: |
| 42 | return AV1Profile::kProfile0; |
| 43 | case 1: |
| 44 | return AV1Profile::kProfile1; |
| 45 | case 2: |
| 46 | return AV1Profile::kProfile2; |
| 47 | default: |
| 48 | return absl::nullopt; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | absl::optional<AV1Profile> ParseSdpForAV1Profile( |
| 53 | const SdpVideoFormat::Parameters& params) { |
| 54 | const auto profile_it = params.find(kAV1FmtpProfile); |
| 55 | if (profile_it == params.end()) |
| 56 | return AV1Profile::kProfile0; |
| 57 | const std::string& profile_str = profile_it->second; |
| 58 | return StringToAV1Profile(profile_str); |
| 59 | } |
| 60 | |
| 61 | bool AV1IsSameProfile(const SdpVideoFormat::Parameters& params1, |
| 62 | const SdpVideoFormat::Parameters& params2) { |
| 63 | const absl::optional<AV1Profile> profile = ParseSdpForAV1Profile(params1); |
| 64 | const absl::optional<AV1Profile> other_profile = |
| 65 | ParseSdpForAV1Profile(params2); |
| 66 | return profile && other_profile && profile == other_profile; |
| 67 | } |
| 68 | |
| 69 | } // namespace webrtc |