7 #include <packager/media/codecs/iamf_audio_util.h>
11 #include <packager/media/base/bit_reader.h>
12 #include <packager/media/base/fourccs.h>
13 #include <packager/media/base/rcheck.h>
14 #include <packager/media/base/stream_info.h>
21 const uint8_t kMaxIamfProfile = 2;
27 OBU_IA_Codec_Config = 0,
28 OBU_IA_Sequence_Header = 31,
33 bool ReadLeb128(BitReader& reader,
size_t* size,
size_t* leb128_bytes) {
35 size_t bytes_read = 0;
36 for (
int i = 0; i < 8; i++) {
37 size_t leb128_byte = 0;
38 RCHECK(reader.ReadBits(8, &leb128_byte));
39 value |= (leb128_byte & 0x7f) << (i * 7);
41 if (!(leb128_byte & 0x80))
46 RCHECK(value <= ((1ull << 32) - 1));
47 if (size !=
nullptr) {
50 if (leb128_bytes !=
nullptr) {
51 *leb128_bytes = bytes_read;
56 bool ParseObuHeader(BitReader& reader,
int& obu_type,
size_t& obu_size) {
58 bool obu_trimming_status_flag;
59 bool obu_extension_flag;
61 RCHECK(reader.ReadBits(5, &obu_type));
62 RCHECK(reader.SkipBits(1));
63 RCHECK(reader.ReadBits(1, &obu_trimming_status_flag));
64 RCHECK(reader.ReadBits(1, &obu_extension_flag));
66 RCHECK(ReadLeb128(reader, &obu_size, &leb128_bytes));
68 if (obu_trimming_status_flag) {
70 RCHECK(ReadLeb128(reader,
nullptr, &leb128_bytes));
71 obu_size -= leb128_bytes;
73 RCHECK(ReadLeb128(reader,
nullptr, &leb128_bytes));
74 obu_size -= leb128_bytes;
77 if (obu_extension_flag) {
78 size_t extension_header_size;
79 RCHECK(ReadLeb128(reader, &extension_header_size, &leb128_bytes));
80 obu_size -= leb128_bytes;
81 RCHECK(reader.SkipBits(extension_header_size * 8));
82 obu_size -= extension_header_size * 8;
88 bool ParseSequenceHeaderObu(BitReader& reader,
89 uint8_t& primary_profile,
90 uint8_t& additional_profile) {
93 RCHECK(reader.ReadBits(32, &ia_code));
94 if (ia_code != FOURCC_iamf) {
95 LOG(WARNING) <<
"Unknown ia_code= " << std::setfill(
'0') << std::setw(8)
96 << std::hex << ia_code;
100 RCHECK(reader.ReadBits(8, &primary_profile));
101 if (primary_profile > kMaxIamfProfile) {
102 LOG(WARNING) <<
"Unknown primary_profile= " << primary_profile;
106 RCHECK(reader.ReadBits(8, &additional_profile));
107 if (additional_profile > kMaxIamfProfile) {
108 LOG(WARNING) <<
"Unknown additional_profile= " << additional_profile;
115 bool ParseCodecConfigObu(BitReader& reader,
size_t obu_size, Codec& codec) {
120 RCHECK(ReadLeb128(reader,
nullptr, &leb128_bytes));
121 obu_size -= leb128_bytes;
123 RCHECK(reader.ReadBits(32, &codec_id));
127 RCHECK(reader.SkipBits(obu_size * 8));
143 LOG(WARNING) <<
"Unknown codec_id= " << std::setfill(
'0') << std::setw(8)
144 << std::hex << codec_id;
152 bool GetIamfCodecStringInfo(
const std::vector<uint8_t>& iacb,
153 uint8_t& codec_string_info) {
154 uint8_t primary_profile = 0;
155 uint8_t additional_profile = 0;
157 Codec iamf_codec = Codec::kUnknownCodec;
162 BitReader reader(iacb.data(), iacb.size());
165 RCHECK(reader.SkipBits(8));
168 RCHECK(ReadLeb128(reader, &obu_size,
nullptr));
170 while (reader.bits_available() > 0) {
171 RCHECK(ParseObuHeader(reader, obu_type, obu_size));
174 case OBU_IA_Sequence_Header:
175 RCHECK(ParseSequenceHeaderObu(reader, primary_profile,
176 additional_profile));
178 case OBU_IA_Codec_Config:
179 RCHECK(ParseCodecConfigObu(reader, obu_size, iamf_codec));
183 RCHECK(reader.SkipBits(obu_size * 8));
204 ((primary_profile << 6) | ((additional_profile << 4) & 0x3F) |
205 ((iamf_codec - kCodecAudio) & 0xF));
All the methods that are virtual are virtual for mocking.