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 #ifndef CLI_AUDIO_FRAME_DECODER_H_ 13 #define CLI_AUDIO_FRAME_DECODER_H_ 14 15 #include <cstdint> 16 #include <memory> 17 #include <vector> 18 19 #include "absl/container/node_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/codec/decoder_base.h" 25 #include "iamf/obu/codec_config.h" 26 #include "iamf/obu/demixing_info_parameter_data.h" 27 #include "iamf/obu/recon_gain_info_parameter_data.h" 28 #include "iamf/obu/types.h" 29 30 namespace iamf_tools { 31 32 struct DecodedAudioFrame { 33 uint32_t substream_id; 34 InternalTimestamp start_timestamp; // Start time of this frame. Measured in 35 // ticks from the Global Timing Module. 36 InternalTimestamp end_timestamp; // End time of this frame. Measured in 37 // ticks from the Global Timing Module. 38 uint32_t samples_to_trim_at_end; 39 uint32_t samples_to_trim_at_start; 40 41 // Decoded samples. Includes any samples that will be trimmed in processing. 42 std::vector<std::vector<int32_t>> decoded_samples; 43 44 // Down-mixing parameters used to create this audio frame. 45 DownMixingParams down_mixing_params; 46 47 // Recon gain info parameter data used to adjust the gain of this audio frame. 48 ReconGainInfoParameterData recon_gain_info_parameter_data; 49 50 // The audio element with data associated with this frame. 51 const AudioElementWithData* audio_element_with_data; 52 }; 53 54 /*!\brief Decodes Audio Frame OBUs based on the associated codec. 55 * 56 * This class is related to the "Codec Decoder" as used in the IAMF 57 * specification. "The Codec Decoder for each Audio Substream outputs the 58 * decoded channels." 59 * 60 * This class manages the underlying codec decoders for all substreams. Codec 61 * decoders may be stateful; this class manages a one-to-one mapping between 62 * codec decoders and substream. 63 * 64 * Call `InitDecodersForSubstreams` with pairs of `SubstreamIdLabelsMap` and 65 * `CodecConfigObu`. This typically will require one call per Audio Element OBU. 66 * 67 * Then call `Decode` repeatedly with a list of `AudioFrameWithData`. There may 68 * be multiple `AudioFrameWithData`s in a single call to this function. Each 69 * substream in the list is assumed to be self-consistent in temporal order. It 70 * is permitted in any order relative to other substreams. 71 */ 72 class AudioFrameDecoder { 73 public: 74 /*!\brief Constructor. */ 75 AudioFrameDecoder() = default; 76 77 /*!\brief Initialize codec decoders for each substream. 78 * 79 * \param substream_id_to_labels Substreams and their associated labels to 80 * initialize. The number of channels is determined by the number of 81 * labels. 82 * \param codec_config Codec Config OBU to use for all substreams. 83 * \return `absl::OkStatus()` on success. A specific status on failure. 84 */ 85 absl::Status InitDecodersForSubstreams( 86 const SubstreamIdLabelsMap& substream_id_to_labels, 87 const CodecConfigObu& codec_config); 88 89 /*!\brief Decodes an Audio Frame OBU. 90 * 91 * \param encoded_audio_frame Input Audio Frame OBU. 92 * \return Decoded audio frame on success. A specific status on failure. 93 */ 94 absl::StatusOr<DecodedAudioFrame> Decode( 95 const AudioFrameWithData& encoded_audio_frame); 96 97 private: 98 // A map of substream IDs to the relevant decoder and codec config. This is 99 // necessary to process streams with stateful decoders correctly. 100 absl::node_hash_map<uint32_t, std::unique_ptr<DecoderBase>> 101 substream_id_to_decoder_; 102 }; 103 104 } // namespace iamf_tools 105 106 #endif // CLI_AUDIO_FRAME_DECODER_H_ 107