1 /*
2  * Copyright (c) 2023, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 3-Clause Clear License
5  * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6  * License was not distributed with this source code in the LICENSE file, you
7  * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
8  * Alliance for Open Media Patent License 1.0 was not distributed with this
9  * source code in the PATENTS file, you can obtain it at
10  * www.aomedia.org/license/patent.
11  */
12 #include "iamf/cli/proto_conversion/proto_to_obu/ia_sequence_header_generator.h"
13 
14 #include <optional>
15 
16 #include "absl/log/log.h"
17 #include "absl/status/status.h"
18 #include "iamf/cli/proto/ia_sequence_header.pb.h"
19 #include "iamf/cli/proto_conversion/lookup_tables.h"
20 #include "iamf/cli/proto_conversion/proto_utils.h"
21 #include "iamf/common/utils/macros.h"
22 #include "iamf/common/utils/map_utils.h"
23 #include "iamf/obu/ia_sequence_header.h"
24 
25 namespace iamf_tools {
26 
27 namespace {
28 
ValidateProfileIsNotReserved(ProfileVersion obu_profile_version)29 absl::Status ValidateProfileIsNotReserved(ProfileVersion obu_profile_version) {
30   if (obu_profile_version == ProfileVersion::kIamfReserved255Profile) {
31     return absl::InvalidArgumentError(
32         "ProfileVersion::kIamfReserved255Profile is not supported.");
33   }
34   return absl::OkStatus();
35 }
36 
CopyProfileVersion(iamf_tools_cli_proto::ProfileVersion metadata_profile_version,ProfileVersion & obu_profile_version)37 absl::Status CopyProfileVersion(
38     iamf_tools_cli_proto::ProfileVersion metadata_profile_version,
39     ProfileVersion& obu_profile_version) {
40   static const auto kProtoToInternalProfileVersion =
41       BuildStaticMapFromPairs(LookupTables::kProtoAndInternalProfileVersions);
42 
43   RETURN_IF_NOT_OK(CopyFromMap(
44       *kProtoToInternalProfileVersion, metadata_profile_version,
45       "Internal version of proto `ProfileVersion`", obu_profile_version));
46 
47   // Only the test suite should use the reserved profile version.
48   MAYBE_RETURN_IF_NOT_OK(ValidateProfileIsNotReserved(obu_profile_version));
49   return absl::OkStatus();
50 }
51 
52 }  // namespace
53 
Generate(std::optional<IASequenceHeaderObu> & ia_sequence_header_obu) const54 absl::Status IaSequenceHeaderGenerator::Generate(
55     std::optional<IASequenceHeaderObu>& ia_sequence_header_obu) const {
56   // Skip generation if the `ia_sequence_header_metadata_` is not initialized.
57   if (!ia_sequence_header_metadata_.has_primary_profile() ||
58       !ia_sequence_header_metadata_.has_additional_profile()) {
59     return absl::OkStatus();
60   }
61 
62   // IA Sequence Header-related arguments.
63   ProfileVersion primary_profile;
64   RETURN_IF_NOT_OK(CopyProfileVersion(
65       ia_sequence_header_metadata_.primary_profile(), primary_profile));
66   ProfileVersion additional_profile;
67   RETURN_IF_NOT_OK(CopyProfileVersion(
68       ia_sequence_header_metadata_.additional_profile(), additional_profile));
69 
70   ia_sequence_header_obu.emplace(
71       GetHeaderFromMetadata(ia_sequence_header_metadata_.obu_header()),
72       ia_sequence_header_metadata_.ia_code(), primary_profile,
73       additional_profile);
74 
75   ia_sequence_header_obu->PrintObu();
76   return absl::OkStatus();
77 }
78 
79 }  // namespace iamf_tools
80