问题
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.
回答1:
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.
回答2:
In addition to the above answers i would like to add a page from my blog: http://phoxis.org/2010/05/08/synch-safe/
回答3:
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.
来源:https://stackoverflow.com/questions/5223025/why-do-mp3-files-use-synchsafe-integers