• 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_WAV_SAMPLE_PROVIDER_H_
14 #define CLI_WAV_SAMPLE_PROVIDER_H_
15 
16 #include <cstdint>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/status/status.h"
23 #include "absl/status/statusor.h"
24 #include "absl/strings/string_view.h"
25 #include "iamf/cli/audio_element_with_data.h"
26 #include "iamf/cli/channel_label.h"
27 #include "iamf/cli/demixing_module.h"
28 #include "iamf/cli/proto/audio_frame.pb.h"
29 #include "iamf/cli/wav_reader.h"
30 #include "iamf/obu/types.h"
31 #include "src/google/protobuf/repeated_ptr_field.h"
32 
33 namespace iamf_tools {
34 
35 class WavSampleProvider {
36  public:
37   /*!\brief Factory function.
38    *
39    * \param audio_frame_metadata Input audio frame metadata.
40    * \param input_wav_directory Directory containing the input WAV files.
41    * \param audio_elements Input Audio Element OBUs with data.
42    * \return `absl::OkStatus()` on success. A specific status on failure.
43    */
44   static absl::StatusOr<WavSampleProvider> Create(
45       const ::google::protobuf::RepeatedPtrField<
46           iamf_tools_cli_proto::AudioFrameObuMetadata>& audio_frame_metadata,
47       absl::string_view input_wav_directory,
48       const absl::flat_hash_map<DecodedUleb128, AudioElementWithData>&
49           audio_elements);
50 
51   /*!\brief Read frames from WAV files corresponding to an Audio Element.
52    *
53    * \param audio_element_id ID of the Audio Element whose corresponding frames
54    *        are to be read from WAV files.
55    * \param labeled_samples Output samples organized by their channel labels.
56    * \param finished_reading Whether the reading is finished.
57    * \return `absl::OkStatus()` on success. A specific status on failure.
58    */
59   absl::Status ReadFrames(DecodedUleb128 audio_element_id,
60                           LabelSamplesMap& labeled_samples,
61                           bool& finished_reading);
62 
63  private:
64   /*!\brief Constructor.
65    *
66    * Used only by factory function. Moves from all input arguments.
67    *
68    * \param wav_readers Mapping from Audio Element ID to `WavReader`.
69    * \param audio_element_id_to_channel_ids Mapping from Audio Element ID to
70    *        channel IDs.
71    * \param audio_element_id_to_labels Mapping from Audio Element ID to channel
72    *        labels.
73    */
74   WavSampleProvider(
75       absl::flat_hash_map<DecodedUleb128, WavReader>&& wav_readers,
76       absl::flat_hash_map<DecodedUleb128, std::vector<uint32_t>>&&
77           audio_element_id_to_channel_ids,
78       absl::flat_hash_map<DecodedUleb128, std::vector<ChannelLabel::Label>>&&
79           audio_element_id_to_labels);
80 
81   // Mapping from Audio Element ID to `WavReader`.
82   absl::flat_hash_map<DecodedUleb128, WavReader> wav_readers_;
83 
84   // Mapping from Audio Element ID to channel IDs.
85   const absl::flat_hash_map<DecodedUleb128, std::vector<uint32_t>>
86       audio_element_id_to_channel_ids_;
87 
88   // Mapping from Audio Element ID to channel labels.
89   const absl::flat_hash_map<DecodedUleb128, std::vector<ChannelLabel::Label>>
90       audio_element_id_to_labels_;
91 };
92 
93 }  // namespace iamf_tools
94 
95 #endif  // CLI_WAV_SAMPLE_PROVIDER_H_
96