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_ADM_TO_USER_METADATA_ADM_BW64_READER_H_ 14 #define CLI_ADM_TO_USER_METADATA_ADM_BW64_READER_H_ 15 16 #include <cstddef> 17 #include <cstdint> 18 #include <istream> 19 #include <string> 20 21 #include "absl/container/flat_hash_map.h" 22 #include "absl/status/statusor.h" 23 #include "absl/strings/string_view.h" 24 #include "iamf/cli/adm_to_user_metadata/adm/adm_elements.h" 25 #include "iamf/cli/adm_to_user_metadata/adm/format_info_chunk.h" 26 27 namespace iamf_tools { 28 namespace adm_to_user_metadata { 29 30 /*!\brief Indexes and extracts ADM information from a BW64 WAV file. 31 * 32 * This class processes Bw64 WAV files 33 * (https://adm.ebu.io/reference/excursions/bw64_and_adm.html). 34 * 35 * The class can be built from a stream which represents a valid RIFF WAV file 36 * with an `axml` chunk. 37 * 38 * This class provides information about the WAV file: 39 * - A index of the chunks within the WAV file (name, size, data). 40 * - A `FormatInfoChunk` associated with the WAV file. 41 * - An ADM structure associated with the `axml` chunk. 42 */ 43 44 class Bw64Reader { 45 public: 46 struct ChunkInfo { 47 size_t size, offset; 48 }; 49 typedef absl::flat_hash_map<std::string, ChunkInfo> ChunksOffsetMap; 50 51 // Each chunk has a 4 character code (e.g. "RIFF", "WAVE", "fmt ", "axml", 52 // etc.). 53 static constexpr int32_t kChunkNameSize = 4; 54 // Each chunk has a 4 byte length. 55 static constexpr int32_t kChunkLengthSize = 4; 56 // Offset of the chunk name and size. 57 static constexpr int32_t kChunkHeaderOffset = 58 kChunkNameSize + kChunkLengthSize; 59 60 /*!\brief Builds a `Bw64Reader` from a stream. 61 * 62 * \param importance_threshold Threshold below which the audio objects will be 63 * ignored. 64 * \param buffer Stream to consume. which represents a valid RIFF WAV file 65 * with an `axml` chunk. 66 * \return Initialized `Bw64Reader` or a specific error code on failure. 67 */ 68 static absl::StatusOr<Bw64Reader> BuildFromStream( 69 int32_t importance_threshold, std::istream& buffer); 70 71 /*!\brief Returns information about a chunk. 72 * 73 * \chunk_name Chunk name to retrieve. 74 * \return Chunk info or a `absl::FailedPreconditionError` error code if the 75 * chunk name is not present. 76 */ 77 absl::StatusOr<ChunkInfo> GetChunkInfo(absl::string_view chunk_name) const; 78 79 const ADM adm_; 80 const FormatInfoChunk format_info_; 81 82 private: 83 /*!\brief Constructor. 84 * 85 * \param adm ADM associated with the bw64 reader. 86 * \param format_info FormatInfoChunk associated with the stream. 87 * \param chunks_offset_map Chunk name to offset map associated with the 88 * stream. 89 */ Bw64Reader(const ADM & adm,const FormatInfoChunk & format_info,const ChunksOffsetMap & chunks_offset_map)90 Bw64Reader(const ADM& adm, const FormatInfoChunk& format_info, 91 const ChunksOffsetMap& chunks_offset_map) 92 : adm_(adm), 93 format_info_(format_info), 94 chunks_offset_map_(chunks_offset_map) {}; 95 96 const ChunksOffsetMap chunks_offset_map_; 97 }; 98 99 } // namespace adm_to_user_metadata 100 } // namespace iamf_tools 101 102 #endif // CLI_ADM_TO_USER_METADATA_ADM_BW64_READER_H_ 103