From f8199bbd6d7b8bc64d8f58dc5638af8bc36cc73f Mon Sep 17 00:00:00 2001 From: Tom Kirchner Date: Sat, 11 Jan 2025 22:04:49 -0800 Subject: [PATCH] Handle optional frames field in bytes offset of MP3 header --- src/codecs/mad.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/codecs/mad.cpp b/src/codecs/mad.cpp index a0184487..e6e641f5 100644 --- a/src/codecs/mad.cpp +++ b/src/codecs/mad.cpp @@ -367,20 +367,26 @@ auto MadMp3Decoder::GetMp3Info(const mad_header& header) uint32_t flags = ((uint32_t)flags_raw[0] << 24) + ((uint32_t)flags_raw[1] << 16) + ((uint32_t)flags_raw[2] << 8) + ((uint32_t)flags_raw[3]); + auto toc_offset = 8; + auto bytes_offset = 8; + if (flags & 1) { + // Frames field is present + toc_offset += 4; + bytes_offset += 4; + } + if (flags & 2) { + // Bytes field is present + toc_offset += 4; + } if (flags & 4) { // TOC flag is set - auto toc_offset = 8; - if (flags & 1) { - toc_offset += 4; - } if (flags & 2) { // Bytes field - unsigned char const* bytes_raw = stream_->this_frame + xing_offset + 12; + unsigned char const* bytes_raw = stream_->this_frame + xing_offset + bytes_offset; uint32_t num_bytes = ((uint32_t)bytes_raw[0] << 24) + ((uint32_t)bytes_raw[1] << 16) + ((uint32_t)bytes_raw[2] << 8) + ((uint32_t)bytes_raw[3]); bytes.emplace(num_bytes); - toc_offset += 4; } // Read the table of contents in toc.emplace((stream_->this_frame + xing_offset + toc_offset), 100);