MPEG Audio Layer I/II/III frame header::http://www.mpgedit.org/mpgedit/mpeg_format/MP3Format.html
An MPEG audio file is built up from smaller parts called frames. Generally, frames are independent items. Each frame has its own header and audio informations. As there is no file header, you can cut any part of MPEG file and play it correctly (this should be done on frame boundaries but most applications will handle incorrect headers). However, for Layer III, this is not 100% correct. Due to internal data organization in MPEG Layer III files, frames are often dependent of each other and they cannot be cut off just like that.
When you want to read info about an MPEG file, it is usually enough to find the first frame, read its header and assume that the other frames are the same. But this may not be always the case. Variable bitrate MPEG files may use so called bitrate switching, which means that bitrate changes according to the content of each frame. This way lower bitrates may be used in frames where it will not reduce sound quality. This allows making better compression while keeping high quality of sound.
The frame header is constituted by the very first four bytes (32bits) in a frame. The first eleven bits (or first twelve bits, see below about frame sync) of a frame header are always set and they are called "frame sync". Therefore, you can search through the file for the first occurence of frame sync (meaning that you have to find a byte with a value of 255, and followed by a byte with its three (or four) most significant bits set). Then you read the whole header and check if the values are correct. You will see in the following table the exact meaning of each bit in the header. Each value that is specified as reserved, invalid, bad, or not allowed should indicate an invalid header.
Frames may have a CRC check. The CRC is 16 bits long and, if it exists, it follows the frame header. After the CRC comes the audio data. You may calculate the CRC of the frame, and compare it with the one you read from the file. This is actually a very good method to check the MPEG frame validity.
Here is a presentation of the header content. Characters from A to M are used to indicate different fields. In the table below, you can see details about the content of each field.
|A||11||(31-21)||Frame sync (all bits set)|
MPEG Audio version ID
Note: MPEG Version 2.5 is not official standard. It is an extension of the standard used for very low bitrate files. If your decoder does not support this extension, it is recommended for you to use 12 bits for synchronization instead of 11 bits.
00 - reserved
01 - Layer III
10 - Layer II
11 - Layer I
0 - Protected by CRC (16bit crc follows header)
1 - Not protected
NOTES: All values are in kbps
MPEG files may have variable bitrate (VBR). Each frame may be created with different bitrate. It may be used in all layers. Layer III decoders must support this method. Layer I & II decoders may support it.
For Layer II there are some combinations of bitrate and mode which are not allowed. Here is a list of allowed combinations.
|F||2||(11,10)||Sampling rate frequency index (values are in Hz)
0 - frame is not padded
1 - frame is padded with one extra slot
Padding is used to fit the bit rates exactly. For an example: 128k 44.1kHz layer II uses a lot of 418 bytes and some of 417 bytes long frames to get the exact 128k bitrate. For Layer I slot is 32 bits long, for Layer II and Layer III slot is 8 bits long.
|H||1||(8)||Private bit. It may be freely used for specific needs of an application.|
00 - Stereo
01 - Joint stereo (Stereo)
10 - Dual channel (2 mono channels)
11 - Single channel (Mono)
Note: Dual channel files are made of two independant mono channel. Each one uses exactly half the bitrate of the file. Most decoders output them as stereo, but it might not always be the case.
One example of use would be some speech in two different languages carried in the same bitstream, and and appropriate decoder would decode only the choosen language </>