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_LPCM_DECODER_CONFIG_H_ 13 #define OBU_DECODER_CONFIG_LPCM_DECODER_CONFIG_H_ 14 15 #include <cstdint> 16 17 #include "absl/status/status.h" 18 #include "iamf/common/read_bit_buffer.h" 19 #include "iamf/common/write_bit_buffer.h" 20 21 namespace iamf_tools { 22 23 /*!\brief The `CodecConfig` `decoder_config` field for LPCM. */ 24 class LpcmDecoderConfig { 25 public: 26 /*!\brief An 8-bit enum to describe how the samples are encoded. 27 * 28 * See `format_flags` in MP4-PCM. 29 */ 30 enum LpcmFormatFlagsBitmask : uint8_t { 31 kLpcmBigEndian = 0x00, 32 kLpcmLittleEndian = 0x01, 33 kLpcmBeginReserved = 0x02, 34 kLpcmEndReserved = 0xff, 35 }; 36 37 friend bool operator==(const LpcmDecoderConfig& lhs, 38 const LpcmDecoderConfig& rhs) = default; 39 40 /*!\brief Returns the required audio roll distance. 41 * 42 * \return Audio roll distance required by the IAMF spec. 43 */ GetRequiredAudioRollDistance()44 static int16_t GetRequiredAudioRollDistance() { return 0; } 45 46 /*!\brief Returns true if the samples are encoded in little-endian format.*/ 47 bool IsLittleEndian() const; 48 49 /*!\brief Validates the values in `LpcmDecoderConfig` and the roll distance. 50 * 51 * \param audio_roll_distance `audio_roll_distance` in the associated Codec 52 * Config OBU. 53 * \return `absl::OkStatus()` if the decoder config is valid. A specific error 54 * code on failure. 55 */ 56 absl::Status Validate(int16_t audio_roll_distance) const; 57 58 /*!\brief Validates and writes the `LpcmDecoderConfig` to a buffer. 59 * 60 * \param audio_roll_distance `audio_roll_distance` in the associated Codec 61 * Config OBU. 62 * \param wb Buffer to write to. 63 * \return `absl::OkStatus()` if the decoder config is valid. A specific error 64 * code on failure. 65 */ 66 absl::Status ValidateAndWrite(int16_t audio_roll_distance, 67 WriteBitBuffer& wb) const; 68 69 /*!\brief Reads and validates the `LpcmDecoderConfig` to a buffer. 70 * 71 * \param audio_roll_distance `audio_roll_distance` in the associated Codec 72 * Config OBU. 73 * \param rb Buffer to read from. 74 * \return `absl::OkStatus()` if the decoder config is valid. A specific error 75 * code on failure. 76 */ 77 absl::Status ReadAndValidate(int16_t audio_roll_distance, ReadBitBuffer& rb); 78 79 /*!\brief Gets the output sample rate represented within the decoder config. 80 * 81 * This sample rate is used for timing and offset calculations. 82 * 83 * IAMF v1.1.0 section 3.11.4 specifies: 84 * > "The sample rate used for computing offsets SHALL be sample_rate." 85 * 86 * \param output_sample_rate Output sample rate. 87 * \return `absl::OkStatus()` if successful. `absl::InvalidArgumentError()` 88 * if there is an unexpected sample rate. 89 */ 90 absl::Status GetOutputSampleRate(uint32_t& output_sample_rate) const; 91 92 /*!\brief Gets the bit-depth of the PCM to be used to measure loudness. 93 * 94 * This typically is the highest bit-depth the user should decode the signal 95 * to. 96 * 97 * \param bit_depth_to_measure_loudness Bit-depth of the PCM which will be 98 * used to measure loudness. 99 * \return `absl::OkStatus()` if successful. `absl::InvalidArgumentError()` 100 * if there is an unexpected bit-depth. 101 */ 102 absl::Status GetBitDepthToMeasureLoudness( 103 uint8_t& bit_depth_to_measure_loudness) const; 104 105 /*!\brief Prints logging information about the decoder config. 106 */ 107 void Print() const; 108 109 LpcmFormatFlagsBitmask sample_format_flags_bitmask_; 110 uint8_t sample_size_; 111 uint32_t sample_rate_; 112 }; 113 114 } // namespace iamf_tools 115 116 #endif // OBU_DECODER_CONFIG_LPCM_DECODER_CONFIG_H_ 117