Why do MP3 files use Synchsafe Integers?
Asked Answered
R

3

12

I started reading mp3-files in c++.

All went well until I read the specs of the ID3-Tag. There is some information in the ID3v2-Header about its size stored in so-called Synchsafe Integers. That is a four-byte integer where the most significant bit of each byte is set to zero.

I found out how to convert it to an ordinary integer, but I cannot stop asking myself why an integer value is stored in such an unnecessarily complicated way.

I hope there is someone who can tell me why it is stored this way.

Rematch answered 7/3, 2011 at 17:33 Comment(0)
M
31

To understand why sync-safe integers are used, it's helpful to understand a little about the format of MP3 data as well as how an MP3 file is played by a media player. MP3 data is stored in a file as a series of frames. Each frame contains a small bit of digital music encoded in MP3 format as well as some meta data about the frame itself. At the beginning of each MP3 frame are 11 bits (sometimes 12) all set to 1. This is called the sync, and it's the pattern a media player looks for when attempting to play an MP3 file or stream. If the player finds this 11 bit sequence, then it knows its found an MP3 frame which can be decoded and played back.

See: www.id3.org/mp3Frame

As you know an ID3 tag contains data about the track as a whole. An ID3 tag -- in version 2.x and later -- is located at the beginning of a file, or can even be embedded in an MP3 stream(though this is not often done). The header of an ID3 tag contains a 32 bit size field, which indicates how many bytes are in the tag. The max value an unsigned, 32 bit integer can hold is 0xFFFFFFFF. So if we write 0xFFFFFFFF into the size field, we're claiming a really big tag (pragmatically too big). When the player attempts to play the file or stream, it looks for the 11 bit sequence of an MP3 data frame, but instead finds the size field in the ID3 tag header and tries to play the tag, since the size field has the first 11 bits set. This usually doesn't sound as good, depending on your musical tastes. The solution is to create an integer format that contains no 11 bit sequences of all 1's. Hence the sync-safe integer format.

A sync-safe integer can be converted to an integer in C/C++ using something like the following:

int ID3_sync_safe_to_int( uint8_t* sync_safe )
{
    uint32_t byte0 = sync_safe[0];
    uint32_t byte1 = sync_safe[1];
    uint32_t byte2 = sync_safe[2];
    uint32_t byte3 = sync_safe[3];

    return byte0 << 21 | byte1 << 14 | byte2 << 7 | byte3;
}

Hope this helps.

Mireillemireles answered 13/4, 2011 at 16:56 Comment(2)
Thank, you very much. I thought that i'll never get it.Rematch
Also, the reason it was done this way is because MP3 was not created with metadata tags in mind. ID3 came along later and had to find a way to work within the existing MP3 limitations without breaking anything.Reiter
L
3

In addition to the above answers i would like to add a page from my blog: http://phoxis.org/2010/05/08/synch-safe/

Langer answered 9/5, 2011 at 17:41 Comment(0)
M
0

6.2. Synchsafe integers

In some parts of the tag it is inconvenient to use the unsychronisation scheme because the size of unsynchronised data is not known in advance, which is particularly problematic with size descriptors. The solution in ID3v2 is to use synchsafe integers, in which there can never be any false synchs. Synchsafe integers are integers that keep its highest bit (bit 7) zeroed, making seven bits out of eight available. Thus a 32 bit synchsafe integer can store 28 bits of information.

From http://www.id3.org/id3v2.4.0-structure

It is tightly related to what they call "Unsynchronisation" in the given document, you should read the entire chapter 6. All this is related to maximize compatibility with a wide range of software and hardware.

Medievalism answered 7/3, 2011 at 17:51 Comment(1)
I don't undersand why the size should be unknown. There are data fields of different size, but it's described in the specs where a particular information is stored in the file and what of size this information is.Rematch

© 2022 - 2024 — McMap. All rights reserved.