1 /* 2 * Copyright (c) 2025, 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 #ifndef CLI_TEMPORAL_UNIT_VIEW_H_ 13 #define CLI_TEMPORAL_UNIT_VIEW_H_ 14 15 #include <sys/types.h> 16 17 #include <cstdint> 18 #include <vector> 19 20 #include "absl/status/statusor.h" 21 #include "absl/types/span.h" 22 #include "iamf/cli/audio_frame_with_data.h" 23 #include "iamf/cli/parameter_block_with_data.h" 24 #include "iamf/obu/arbitrary_obu.h" 25 #include "iamf/obu/types.h" 26 27 namespace iamf_tools { 28 29 /*!\brief A view of all OBUs in a single temporal unit as defined by the spec. 30 * 31 * A temporal unit is defined as a set of all audio frames with the same start 32 * timestamp and the same duration from all substreams and all parameter blocks 33 * with the start timestamp within the duration. 34 * 35 * This class provides functionality to create a view of the OBUs in a temporal 36 * unit. The factory function validates the input data is self consistent and 37 * could be valid in some IA Sequence. 38 */ 39 // TODO(b/397637224): Be stricter about the expected parameter block and 40 // substream IDs, or validate that in `ObuSequencerBase`. 41 struct TemporalUnitView { 42 /*!\brief Creates a `TemporalUnitView` from the input data. 43 * 44 * \param parameter_blocks Parameter blocks to include in the view. 45 * \param audio_frames Audio frames to include in the view. 46 * \param arbitrary_obus Arbitrary OBUs to include in the view. 47 * \return `TemporalUnitView` on success. A specific status on failure. 48 */ 49 static absl::StatusOr<TemporalUnitView> CreateFromPointers( 50 absl::Span<const ParameterBlockWithData* const> parameter_blocks, 51 absl::Span<const AudioFrameWithData* const> audio_frames, 52 absl::Span<const ArbitraryObu* const> arbitrary_obus); 53 54 /*!\brief Creates a `TemporalUnitView` from the input data. 55 * 56 * Adapter to the pointer-based `Create` function above, but usable with any 57 * container of input data. 58 * 59 * \param parameter_blocks Parameter blocks to include in the view. 60 * \param audio_frames Audio frames to include in the view. 61 * \param arbitrary_obus Arbitrary OBUs to include in the view. 62 * \return `TemporalUnitView` on success. A specific status on failure. 63 */ 64 template <typename ParameterBlockWithDataContainer, 65 typename AudioFrameWithDataContainer, 66 typename ArbitraryObuContainer> CreateTemporalUnitView67 static absl::StatusOr<TemporalUnitView> Create( 68 const ParameterBlockWithDataContainer& parameter_blocks, 69 const AudioFrameWithDataContainer& audio_frames, 70 const ArbitraryObuContainer& arbitrary_obus) { 71 std::vector<const ParameterBlockWithData*> parameter_blocks_ptrs; 72 parameter_blocks_ptrs.reserve(parameter_blocks.size()); 73 for (const auto& parameter_block : parameter_blocks) { 74 parameter_blocks_ptrs.push_back(¶meter_block); 75 } 76 77 std::vector<const AudioFrameWithData*> audio_frames_ptrs; 78 audio_frames_ptrs.reserve(audio_frames.size()); 79 for (const auto& audio_frame : audio_frames) { 80 audio_frames_ptrs.push_back(&audio_frame); 81 } 82 std::vector<const ArbitraryObu*> arbitrary_obus_ptrs; 83 arbitrary_obus_ptrs.reserve(arbitrary_obus.size()); 84 for (const auto& arbitrary_obu : arbitrary_obus) { 85 arbitrary_obus_ptrs.push_back(&arbitrary_obu); 86 } 87 return CreateFromPointers(absl::MakeConstSpan(parameter_blocks_ptrs), 88 absl::MakeConstSpan(audio_frames_ptrs), 89 absl::MakeConstSpan(arbitrary_obus_ptrs)); 90 } 91 92 const std::vector<const ParameterBlockWithData*> parameter_blocks_; 93 const std::vector<const AudioFrameWithData*> audio_frames_; 94 const std::vector<const ArbitraryObu*> arbitrary_obus_; 95 96 // Common statistics for this temporal unit. 97 const InternalTimestamp start_timestamp_; 98 const InternalTimestamp end_timestamp_; 99 const uint32_t num_samples_to_trim_at_start_; 100 const uint32_t num_untrimmed_samples_; 101 102 private: 103 /*!\brief Private constructor. 104 * 105 * Used only by the `Create` factory functions. 106 * 107 * \param parameter_blocks Parameter blocks to include in the view. 108 * \param audio_frames Audio frames to include in the view. 109 * \param arbitrary_obus Arbitrary OBUs to include in the view. 110 * \param start_timestamp Start timestamp of the temporal unit. 111 * \param end_timestamp End timestamp of the temporal unit. 112 * \param num_samples_to_trim_at_start Number of samples to trim at the start 113 * of the audio frames. 114 * \param num_untrimmed_samples Number of samples in the audio frames before 115 * trimming. 116 */ 117 TemporalUnitView( 118 std::vector<const ParameterBlockWithData*>&& parameter_blocks, 119 std::vector<const AudioFrameWithData*>&& audio_frames, 120 std::vector<const ArbitraryObu*>&& arbitrary_obus, 121 InternalTimestamp start_timestamp, InternalTimestamp end_timestamp, 122 uint32_t num_samples_to_trim_at_start, uint32_t num_untrimmed_samples); 123 }; 124 125 } // namespace iamf_tools 126 127 #endif // CLI_TEMPORAL_UNIT_VIEW_H_ 128