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 OBU_DECODER_CONFIG_OPUS_DECODER_CONFIG_H_ 13 #define OBU_DECODER_CONFIG_OPUS_DECODER_CONFIG_H_ 14 15 #include <cstdint> 16 17 #include "absl/status/status.h" 18 #include "absl/status/statusor.h" 19 #include "iamf/common/read_bit_buffer.h" 20 #include "iamf/common/write_bit_buffer.h" 21 22 namespace iamf_tools { 23 24 /*!\brief The `CodecConfig` `decoder_config` field for Opus.*/ 25 class OpusDecoderConfig { 26 public: 27 static constexpr uint8_t kOutputChannelCount = 2; 28 static constexpr int16_t kOutputGain = 0; 29 static constexpr uint8_t kMappingFamily = 0; 30 31 friend bool operator==(const OpusDecoderConfig& lhs, 32 const OpusDecoderConfig& rhs) = default; 33 34 /*!\brief Returns the required audio roll distance. 35 * 36 * \param num_samples_per_frame `num_samples_per_frame` in the associated 37 * Codec Config OBU. 38 * \return Required audio roll distance, or an error if there would be a 39 * divide by zero. 40 */ 41 static absl::StatusOr<int16_t> GetRequiredAudioRollDistance( 42 uint32_t num_samples_per_frame); 43 44 /*!\brief Validates and writes the `OpusDecoderConfig` to a buffer. 45 * 46 * \param num_samples_per_frame `num_samples_per_frame` in the associated 47 * Codec Config OBU. 48 * \param audio_roll_distance `audio_roll_distance` in the associated Codec 49 * Config OBU. 50 * \param wb Buffer to write to. 51 * \return `absl::OkStatus()` if the decoder config is valid. A specific error 52 * code on failure. 53 */ 54 absl::Status ValidateAndWrite(uint32_t num_samples_per_frame, 55 int16_t audio_roll_distance, 56 WriteBitBuffer& wb) const; 57 58 /*!\brief Validates and reads the `OpusDecoderConfig` from a buffer. 59 * 60 * \param num_samples_per_frame `num_samples_per_frame` in the associated 61 * Codec Config OBU. 62 * \param audio_roll_distance `audio_roll_distance` in the associated Codec 63 * Config OBU. 64 * \param rb Buffer to read from. 65 * \return `absl::OkStatus()` if the decoder config is valid. A specific error 66 * code on failure. 67 */ 68 absl::Status ReadAndValidate(uint32_t num_samples_per_frame, 69 int16_t audio_roll_distance, ReadBitBuffer& rb); 70 71 /*!\brief Gets the output sample rate represented within the decoder config. 72 * 73 * This sample rate is used for timing and offset calculations. 74 * 75 * IAMF v1.1.0 section 3.11.1 specifies: 76 * > "The sample rate used for computing offsets SHALL be 48 kHz." 77 * 78 * \return Output sample rate. 79 */ GetOutputSampleRate()80 uint32_t GetOutputSampleRate() const { return 48000; } 81 82 /*!\brief Gets the input sample rate represented within the decoder config. 83 * 84 * Opus explicitly has this value in the Codec Config OBU. 85 * 86 * \return Input sample rate. 87 */ GetInputSampleRate()88 uint32_t GetInputSampleRate() const { return input_sample_rate_; } 89 90 /*!\brief Gets the bit-depth of the PCM to be used to measure loudness. 91 * 92 * This typically is the highest bit-depth associated substreams should be 93 * decoded to. The encoder provides data to `libopus::opus_encoder_float()` as 94 * a `float` in the range [-1, +1]. 95 * 96 * \return Bit-depth of the PCM which will be used to measure loudness if the 97 * OBU was initialized successfully. 98 */ GetBitDepthToMeasureLoudness()99 static constexpr uint8_t GetBitDepthToMeasureLoudness() { return 32; } 100 101 /*!\brief Prints logging information about the decoder config. 102 */ 103 void Print() const; 104 105 uint8_t version_; 106 // Must be set to 2. This field is ignored. 107 uint8_t output_channel_count_ = kOutputChannelCount; 108 uint16_t pre_skip_; 109 uint32_t input_sample_rate_; 110 int16_t output_gain_ = kOutputGain; 111 uint8_t mapping_family_ = kMappingFamily; 112 }; 113 114 } // namespace iamf_tools 115 116 #endif // OBU_DECODER_CONFIG_OPUS_DECODER_CONFIG_H_ 117