• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 #ifndef CLI_GLOBAL_TIMING_MODULE_H_
14 #define CLI_GLOBAL_TIMING_MODULE_H_
15 
16 #include <cstdint>
17 #include <memory>
18 #include <optional>
19 #include <utility>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/status/status.h"
23 #include "iamf/cli/audio_element_with_data.h"
24 #include "iamf/obu/param_definition_variant.h"
25 #include "iamf/obu/types.h"
26 
27 namespace iamf_tools {
28 
29 class GlobalTimingModule {
30  public:
31   /*!\brief Creates a Global Timing Module.
32    *
33    * \param audio_elements Audio Element OBUs with data to search for sample
34    *        rates.
35    * \param param_definition_variants Parameter definitions keyed by parameter
36    *        IDs.
37    * \return `GlobalTimingModule` on success. Null pointer on failure.
38    */
39   static std::unique_ptr<GlobalTimingModule> Create(
40       const absl::flat_hash_map<DecodedUleb128, AudioElementWithData>&
41           audio_elements,
42       const absl::flat_hash_map<DecodedUleb128, ParamDefinitionVariant>&
43           param_definition_variants);
44 
45   /*!\brief Gets the start and end timestamps of the next Audio Frame.
46    *
47    * \param audio_substream_id Substream ID of the Audio Frame.
48    * \param duration Duration of this frame measured in ticks.
49    * \param start_timestamp Output start timestamp.
50    * \param end_timestamp Output end timestamp.
51    * \return `absl::OkStatus()` on success. A specific status on failure.
52    */
53   absl::Status GetNextAudioFrameTimestamps(DecodedUleb128 audio_substream_id,
54                                            uint32_t duration,
55                                            InternalTimestamp& start_timestamp,
56                                            InternalTimestamp& end_timestamp);
57 
58   /*!\brief Gets the start and end timestamps of the next Parameter Block.
59    *
60    * \param parameter_id ID of the Parameter Block.
61    * \param input_start_timestamp Start timestamp specified by the user. Will be
62    *        used to check if there are gaps.
63    * \param duration Duration of this Parameter Block measured in ticks.
64    * \param start_timestamp Output start timestamp.
65    * \param end_timestamp Output end timestamp.
66    * \return `absl::OkStatus()` on success. A specific status on failure.
67    */
68   absl::Status GetNextParameterBlockTimestamps(
69       DecodedUleb128 parameter_id, InternalTimestamp input_start_timestamp,
70       uint32_t duration, InternalTimestamp& start_timestamp,
71       InternalTimestamp& end_timestamp);
72 
73   // TODO(b/291732058): Bring back parameter block coverage validation.
74 
75   /*!\brief Gets the global timestamp shared by all current Audio Frames.
76    *
77    * May fill in `std::nullopt` if some Audio Frames have different timestamps
78    * (typically because the current temporal unit has not been processed fully).
79    *
80    * \param global_timestamp Global timestamp if it's shared by the current
81    *        Audio Frames; `std::nullopt` if not.
82    * \return `absl::OkStatus()` on success. A specific status on failure.
83    */
84   absl::Status GetGlobalAudioFrameTimestamp(
85       std::optional<InternalTimestamp>& global_timestamp) const;
86 
87  private:
88   struct TimingData {
89     // Ticks per second, used by audio sample rate and parameter rate.
90     const uint32_t rate;
91 
92     // Measured in ticks implied by `rate`.
93     InternalTimestamp timestamp;
94   };
95 
96   /*!\brief Constructor.
97    *
98    * Used only by `Create()`.
99    *
100    * \param audio_frame_timing_data Timing data for Audio Frames keyed by
101    *        substream ID.
102    * \param parameter_block_timing_data Timing data for Parameter Blocks keyed
103    *        by parameter ID.
104    */
GlobalTimingModule(absl::flat_hash_map<DecodedUleb128,TimingData> && audio_frame_timing_data,absl::flat_hash_map<DecodedUleb128,TimingData> && parameter_block_timing_data)105   GlobalTimingModule(
106       absl::flat_hash_map<DecodedUleb128, TimingData>&& audio_frame_timing_data,
107       absl::flat_hash_map<DecodedUleb128, TimingData>&&
108           parameter_block_timing_data)
109       : audio_frame_timing_data_(std::move(audio_frame_timing_data)),
110         parameter_block_timing_data_(std::move(parameter_block_timing_data)) {}
111 
112   absl::Status GetTimestampsForId(
113       DecodedUleb128 id, uint32_t duration,
114       absl::flat_hash_map<DecodedUleb128, TimingData>& id_to_timing_data,
115       InternalTimestamp& start_timestamp, InternalTimestamp& end_timestamp);
116 
117   absl::flat_hash_map<DecodedUleb128, TimingData> audio_frame_timing_data_;
118   absl::flat_hash_map<DecodedUleb128, TimingData> parameter_block_timing_data_;
119 };
120 
121 }  // namespace iamf_tools
122 
123 #endif  // CLI_GLOBAL_TIMING_MODULE_H_
124