Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: ID3v2 tag size (Read 7853 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

ID3v2 tag size

I seem to have partial understanding about ID3v2 tag size.
This is how I calculate the tag size (exludling the header but including the footer if present):

   uint8_t* pId3Header = pStream->psi.buf;
   int32_t payloadLen = ((pId3Header[9] & 0x7F) +
                   ((pId3Header[8] & 0x7F << 7) +
                   ((pId3Header[7] & 0x7F << 14) +
                   ((pId3Header[6] & 0x7F << 21));
   uint8_t flags = pId3Header[5];
   if (pId3Header[5] & 0x10)
   {
      payloadLen += 10;
   }

I looked around and confirmed that this is correct.
But it does not work with mp3 files I have.
Basically I am trying to calculate the size of this tag and skip over to get to the beginning of frame header(1111 1111 111x).
I've tried 2 mp3 files so far but both files require addtional offset to be added to the length to get to the first frame header.

With Kalimba.mp3, its calcuated tag size is 60593(excluding the 10-byte header) but I have to add an offset of 386 bytes to locate its first frame header.
With Sleep Away.mp3, 28283 is calculated but I have to add 528 to get to its first frame header.

I am much puzzled.
Help is much appreciated.




I had to add 386 (+ 60593)







ID3v2 tag size

Reply #2
This is what I use:
Code: [Select]
//-----is a version we know about-----
if ( (Buffer[5] & 0x0F) || ((Buffer[6] | Buffer[7] | Buffer[8] | Buffer[9]) & 0x80) )
{
} else {
  int Footer = Buffer[5] & 0x10;
  ToRet = Buffer[6] << 21;
  ToRet+= Buffer[7] << 14;
  ToRet+= Buffer[8] << 7;
  ToRet+= Buffer[9];
  ToRet+=10;
  if (Footer)
    ToRet+=10;
}

ID3v2 tag size

Reply #3
Thanks spoon.
Your code is identical in essece to my code.
I don't have problem calculating the tag size and I believe it's correct.
The problem is there seems to be extra data that I am not accounting for.
The calculated tag size is not enough to skip over the entire id3v2 tag bytes before the start of first mp3 audio frame.
Does this make sense?


ID3v2 tag size

Reply #5
I had to add 386 (+ 60593)

Axone complains about Kalimba.mp3: "Lost data: 386 bytes".


Does this mean the mp3 is ill-formed at least with respect to ID3 Tag?
What's odd is it's not just one mp3 file. Both Kalimba.mp3 and SleepAway.mp3 are readily freely available.

Most mp3 players have no problem playing both files.
Does this mean thes players are written to look for the first frame header(1111 1111 111b) in case of ID3 corruption?
I am trying to be 100% sure that I am not missing some other header(s) that I am supposed to parse.

ID3v2 tag size

Reply #6
Does this mean thes players are written to look for the first frame header(1111 1111 111b) in case of ID3 corruption?


Aren't you required to do this regardless?  You can't start decoding until you lock onto a valid frame.