|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Contents1. IntroductionThis article is about the structure of the MPEG audio frame header including the XING and VBRI headers. The aim is to estimate the duration of the MPEG audio file as exact and fast as possible. The article does not include any hints on how to decode/encode the actual audio data. MPEG audio files exist in different layers. The most common is the MPEG 1 Layer III (also known as MP3), as it has the most sophisticated compression technology. I know that there are other articles about the MPEG audio frame header (even on CodeProject), but I will go into it a little bit deeper. 2. MPEG Audio FrameAn MPEG audio file consists out of frames. Each frame contains a header at its beginning followed by the audio data. This audio data always contains a fixed number of samples. There currently exists three layers of MPEG audio, which differ in how the audio data is encoded in the frame, although they all have the same header format. The frame itself consists of slots. In Layer I, a slot is always 4 byte long, in all other the layers a slot is 1 byte long. Additional to the layers there are also three versions of MPEG audio, which differ in the sampling rate they can handle (see table 2.1.2). MPEG 1 (ISO/IEC 13818-3) and MPEG 2 (ISO/IEC 11172-3) are ISO standards. MPEG 2.5 is an unofficial extension of MPEG 2 to support even lower sampling rates. MPEG 2/2.5 is also known under the abbreviation LSF, which stands for Lower Sampling Frequencies. Each version can handle the three layers. If you want to know more about the technical details of an MPEG audio file please have a look at the specifications. You can find them and many other useful information about MPEG at www.MP3-Tech.org. A file can be encoded either with a constant bitrate (CBR) or with a variable bitrate (VBR), which means that each frame can have a different bitrate. Therefore, the quality of those files is often higher than files encoded in constant a bitrate mode, because they can use higher bitrates where the music needs it. 2.1. MPEG Audio Frame HeaderThe header at the beginning of each frame is 32 bits long and has the following format. The bit 0 in the header is the most significant bit (MSB) of the complete header. Note that the position is zero-based; position, length and example are all in bit-format.
The sampling rate specifies how many samples per second are recorded. Each MPEG version can handle different sampling rates.
The bitrates are always displayed in kilobits per second. Note that the prefix kilo (abbreviated with the small 'k') doesn't mean 1024 but 1000 bits per second! The bitrate index 1111 is reserved and should never be used. In the MPEG audio standard there is a free format described. This free format means that the file is encoded with a constant bitrate, which is not one of the predefined bitrates. Only very few decoders can handle those files.
In MPEG 1 Layer II, there are only some combinations of bitrates and modes allowed. In MPEG 2/2.5, there is no such restriction.
For the calculation of the frame size, you need the number of samples per MPEG audio frame. Therefore, you can use the following table:
Then you can calculate the frame size like this: Frame Size = ( (Samples Per Frame / 8 * Bitrate) / Sampling Rate) + Padding Size
Because of rounding errors, the official formula to calculate the frame size is a little bit different. According to the ISO standards, you have to calculate the frame size in slots (see 2. MPEG Audio Format), then truncate this number to an integer, and after that multiply it with the slot size. You can find the correct way of calculating the frame size in the class You get the duration of the file in seconds by applying the following formula: Duration = File Size / Bitrate * 8
The method of getting the first frame header in the file and then calculating the duration by the above formula works correctly only for CBR files. The mode extension is used to join information that is of no use for the stereo effect, thus reducing the needed bits. These bits are dynamically determined by an encoder in the Joint Stereo mode, and Joint Stereo can be changed from one frame to another, or even switched on or off. For all other channel modes, the mode extension field is invalid. The complete frequency range of MPEG audio files is divided into subbands. There are 32 subbands. For Layers I & II, the two bits in the header determine the frequency range (bands) where the intensity stereo is applied. Within this frequency range, only one channel is stored. All other bands contain information in two separate channels. For Layer III, these two bits determine which type of joint stereo is used (intensity stereo and/or M/S stereo).
2.2. Verifying CRCIf the protection bit in the header is not set, the frame contains a 16 bit CRC (Cyclic Redundancy Checksum). This checksum directly follows the frame header and is a big-endian The CRC is calculated by applying the CRC-16 algorithm (with the generator polynom 0x8005) to a part of the frame. The following data is considered for the CRC: the last two bytes of the header and a number of bits from the audio data which follows the checksum after the header. The checksum itself must be skipped for CRC calculation. Unfortunately there is no easy way to compute the number of frames which are necessary for the checksum calculation in Layer II. Therefore I left it out in the code. You would need other information apart from the header to calculate the necessary bits. However it is possible to compute the number of protected bits in Layer I and Layer III only with the information from the header. For Layer III, you consider the complete side information for the CRC calculation. The side information follows the header or the CRC in Layer III files. It contains information about the general decoding of the frame, but doesn't contain the actual encoded audio samples. The following table shows the size of the side information for all Layer III files.
For Layer I files, you must consider the mode extension (see table 2.1.6) from the header. Then you can calculate the number of bits which are necessary for CRC calculation by applying the following formula: 4 * (number of channels * bound of intensity stereo +
(32 - bound of intensity stereo));
This can be read as two times the number of stereo subbands plus the number of mono subbands and the result multiplied with 4. For simple mono frames, this equals 128, because the number of channels is one and the bound of intensity stereo is 32, meaning that there is no intensity stereo. For stereo frames this is 256. For more information have a look at the CRC code in the class 2.3. VBR HeadersSome files are encoded with variable bitrate mode (VBR). To estimate the duration of those files, you have to know the average bitrate of the whole file. It often differs a lot from the bitrate of the first frame, because the lowest bitrate available is used for silence in music titles (especially at the beginning). To get this average bitrate, you must go through all the frames in the file and calculate it, by summarizing the bitrates of each frame and dividing it through the number of frames. Because this isn't a good practice (very slow), there exists additional VBR headers within the data section of the first frame (after the frame header). They contain the total number of frames in the file from which you can calculate the duration in seconds with the following formula: Duration = Number of Frames * Samples Per Frame / Sampling Rate
Additional to that, the VBR header often contains a table which is necessary to seek positions within the file. 2.3.1 XING HeaderThis header is often (but unfortunately not always) added to files which are encoded with variable bitrate mode. This header stands after the first MPEG audio header at a specific position. The whole first frame which contains the XING header is a valid but empty audio frame, so even decoders which don't consider this header can decode the file. The XING header stands after the side information in Layer III files. So you can calculate the beginning of a XING header relative to the beginning of the frame by adding 4 bytes (for the MPEG audio header) to the values from the table 2.2.1. The offset calculation doesn't consider the 16 bit CRC following the header and is equal for all Layers, although only Layer III has a side information. For reading out this header, you have to find the first MPEG audio header and then go to this specific position within the frame. The XING header itself has the following format. (Note that the position is zero-based; position, length and example are each in byte-format.)
According to this format, a XING header must only contain the ID and the flags. All other fields are optional and depend on the flags which are set. Sometimes this header is also added to CBR files. It then often has the ID 'Info' instead of 'Xing'. There exists the LAME extension to this header, which is used by the common LAME Encoder, but I didn't take it into account because it isn't necessary for duration estimation. Nonetheless, here is the link for the documentation of the MP3 Info Tag. 2.3.2 VBRI HeaderThis header is only used by MPEG audio files encoded with the Fraunhofer Encoder as far as I know. It is different from the XING header. You find it exactly 32 bytes after the end of the first MPEG audio header in the file. (Note that the position is zero-based; position, length and example are each in byte-format.)
3. Additional TagsPlease consider that at the end or at the beginning of the file, there might be additional data which is not part of the MPEG audio frames. This data is called a tag, because it contains metadata about the file, like title, artist, track, years etc. You must consider these tags, because only the MPEG audio data count for the duration estimation.. At the end of a file there might be an ID3V1 tag, a Lyrics3 tag or a Musicmatch tag. At the beginning or at the end of the file there might be an ID3V2 tag and/or an APE tag. You find information about ID3 and Lyrics3 tags at www.id3.org. APE was originally developed for lossless compressed audio files in APE (Monkey's Audio) format. But now this tag is also used for MPEG audio files. The Musicmatch tag was used by older version of the Musicmatch Encoder. Unfortunately there is very little information on the web about this tag, since it is a proprietary format of Musicmatch. 4. Using The CodeI wrote some C++ classes to handle the MPEG audio frame header and the VBR headers. The class Here is a code snippet which demonstrates how you could use these classes. Note that you must at least include the header mpafile.h: try { // open file and look for first header CMPAFile MPAFile(_T("C:\\test.mp3")) ; cout << MPAFile.GetLengthSec() << _T(" seconds"); } catch(CMPAException& Exc) { // show error message Exc.ShowError(); } For more information, have a look at the source of the demo project MPEG Audio Info, which is a simple MFC Dialog Application, which uses the class 5. Links And MiscellaneousHere, you can find the sources which I used for this article:
If you find any mistakes in the code or in the article, or have suggestions for improvements, just post them to the forum below or write me an e-mail to webmaster@wincd.de. 6. History
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||