• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024, 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 
13 #ifndef CLI_OBU_WITH_DATA_GENERATOR_H_
14 #define CLI_OBU_WITH_DATA_GENERATOR_H_
15 
16 #include <memory>
17 #include <vector>
18 
19 #include "absl/container/flat_hash_map.h"
20 #include "absl/status/status.h"
21 #include "absl/status/statusor.h"
22 #include "iamf/cli/audio_element_with_data.h"
23 #include "iamf/cli/audio_frame_with_data.h"
24 #include "iamf/cli/global_timing_module.h"
25 #include "iamf/cli/parameter_block_with_data.h"
26 #include "iamf/cli/parameters_manager.h"
27 #include "iamf/obu/audio_element.h"
28 #include "iamf/obu/audio_frame.h"
29 #include "iamf/obu/codec_config.h"
30 #include "iamf/obu/param_definitions.h"
31 #include "iamf/obu/parameter_block.h"
32 #include "iamf/obu/types.h"
33 
34 namespace iamf_tools {
35 
36 /*!\brief A collection of utility functions to generate OBUs with data.
37  */
38 class ObuWithDataGenerator {
39  public:
40   /*!\brief Creates a map of `AudioElementWithData` instances.
41    *
42    * \param codec_config_obus Map of Codec Config OBUs.
43    * \param audio_element_obus Map of Audio Element OBUs used to generate the
44    *        result. OBU ownership is transferred to the returned map and
45    *        `audio_element_obus` is cleared upon success.
46    * \return `absl::OkStatus()` if the process is successful. A specific status
47    *         on failure.
48    */
49   static absl::StatusOr<
50       absl::flat_hash_map<DecodedUleb128, AudioElementWithData>>
51   GenerateAudioElementsWithData(
52       const absl::flat_hash_map<DecodedUleb128, CodecConfigObu>&
53           codec_config_obus,
54       absl::flat_hash_map<DecodedUleb128, AudioElementObu>& audio_element_obus);
55 
56   /*!\brief Creates an `AudioFrameWithData` instance.
57    *
58    * \param audio_element_with_data `AudioElementWithData` associated to the
59    *        audio frame.
60    * \param audio_frame_obu Input audio frame OBU.
61    * \param global_timing_module Module to keep track of frame-by-frame
62    *        timestamps.
63    * \param parameters_manager Maintains the state of the parameters.
64    * \return Audio frame with data if the process is successful. A specific
65    *         status on failure.
66    */
67   static absl::StatusOr<AudioFrameWithData> GenerateAudioFrameWithData(
68       const AudioElementWithData& audio_element_with_data,
69       const AudioFrameObu& audio_frame_obu,
70       GlobalTimingModule& global_timing_module,
71       ParametersManager& parameters_manager);
72 
73   /*!\brief Creates a `ParameterBlockWithData` instance.
74    *
75    * \param input_start_timestamp Expected start timestamp of this parameter
76    *        block to check that there is no gap in the parameter substream.
77    * \param global_timing_module Module to keep track of frame-by-frame
78    *        timestamps.
79    * \param parameter_block_obu Unique pointer to a parameter block OBU.
80    *        Ownership will be transferred to the output argument.
81    * \return Parameter block with data if the process is successful. A specific
82    *         status on failure.
83    */
84   static absl::StatusOr<ParameterBlockWithData> GenerateParameterBlockWithData(
85       InternalTimestamp input_start_timestamp,
86       GlobalTimingModule& global_timing_module,
87       std::unique_ptr<ParameterBlockObu> parameter_block_obu);
88 
89   /*!\brief Populates metadata about the layout config into the output params.
90    *
91    * \param audio_substream_ids Ordered list of substream IDs in the OBU.
92    * \param config Scalable channel layout config to process.
93    * \param substream_id_to_labels `audio_substream_id` to output label map.
94    * \param label_to_output_gain Output param populated by this function.
95    * \param channel_numbers_for_layers Output param populated by this function.
96    *
97    * \return `absl::OkStatus()` on success. A specific status on failure.
98    */
99   static absl::Status FinalizeScalableChannelLayoutConfig(
100       const std::vector<DecodedUleb128>& audio_substream_ids,
101       const ScalableChannelLayoutConfig& config,
102       SubstreamIdLabelsMap& substream_id_to_labels,
103       LabelGainMap& label_to_output_gain,
104       std::vector<ChannelNumbers>& channel_numbers_for_layers);
105 
106   /*!\brief Populates substream_id_to_labels for the ambisonics config.
107    *
108    * \param audio_element_obu Mono ambisonics config OBU to process.
109    * \param substream_id_to_labels Output map of substream IDs to labels.
110    * \return `absl::OkStatus()` on success. An error if the input OBU is not an
111    *         ambisonics config. A specific status on failure.
112    */
113   static absl::Status FinalizeAmbisonicsConfig(
114       const AudioElementObu& audio_element_obu,
115       SubstreamIdLabelsMap& substream_id_to_labels);
116 };
117 }  // namespace iamf_tools
118 
119 #endif  // CLI_OBU_WITH_DATA_GENERATOR_H_
120