Re: How to use libid3tag to find offset to first mp3 header

Rob Leslie <[email protected]> Mon, 28 Jun 2004 16:24:41 -0700
Newsgroups gmane.comp.audio.mad.user
Message-ID <[email protected]>
On Jun 28, 2004, at 12:33 PM, tj wrote:
> What call in libid3tag returns the offset to the mp3 header?

Strictly speaking, none does. ID3 tags are autonomous entities, not 
containers for MP3 data. However, MP3 data will typically begin 
immediately following an ID3v2 tag, if any. So you just need to know 
the length of the tag.

As I wrote before, id3_tag_query() will tell you the length of a tag, 
if you pass it a small buffer containing the beginning of the tag data.

Usually it's easiest to assume MP3 data begins at the beginning of the 
file. Try to decode a frame; if it fails due to lost sync, try 
id3_tag_query() to see if an ID3 tag lives there. If so, parse the tag 
and/or skip its length (e.g. with mad_stream_skip()) and then resume 
decoding.

> Plus, how the hell is that four byte sync safe length decoded?

Each byte contains 7 bits of the number; libid3tag decodes it this way:

     unsigned long value = 0;

     value = (value << 7) | (*ptr++ & 0x7f);
     value = (value << 7) | (*ptr++ & 0x7f);
	value = (value << 7) | (*ptr++ & 0x7f);
	value = (value << 7) | (*ptr++ & 0x7f);

     return value;

-- 
Rob Leslie
[email protected]