Audio Codec -MPEG Audio Compression Basics
This is one of many methods to compress audio in digital form trying to consume as little space as possible but keep audio quality as good as possible. MPEG compression showed up as one of the best achievements in this area.
This is a lossy compression, which means, you will certainly lose some audio information when you use this compression methods. But, this lost can hardly be noticed because the compression method tries to control it. By using several quite complicate and demanding mathematical algorithms it will only loose those parts of sound that are hard to be heard even in the original form. This leaves more space for information that is important. This way you can compress audio up to 12 times (you may choose compression ratio) which is really significant. Due to its quality MPEG audio became very popular.
MPEG standards MPEG-1, MPEG-2 and MPEG-4 are known but this document covers first two of them. There is an unofficial MPEG-2.5 which is rarely used. It is also covered.
MPEG-1 audio (described in ISO/IEC 11172-3) describes three Layers of audio coding with the following properties:
- one or two audio channels
- sample rate 32kHz, 44.1kHz or 48kHz
- bit rates from 32kbps up to 448kbps
Each layer has its merits.
MPEG-2 audio (described in ISO/IEC 13818-3) has two extensions to MPEG-1, usually referred as MPEG-2/LSF and MPEG-2/Multichannel.
MPEG-2/LSF has the following properties:
- one or two audio channels
- sample rates half those of MPEG-1
- bit rates from 8 kbps up to 256kbps.
MPEG-2/Multichannel has the following properties:
- up to 5 full range audio channels and an LFE-channel (Low Frequency Enhancement <> subwoofer!)
- sample rates the same as those of MPEG-1
- highest possible bitrate goes up to about 1Mbps for 5.1
- MPEG Audio Frame Header
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 information. There is no file header. Therefore, 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). For Layer III, this is not 100% correct. Due to internal data organization in MPEG version 1 Layer III files (mp3), 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, 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 (in the case of the MPEG 2.5 extension) (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 occurrence 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, and which values may be checked for validity. Each value that is specified as reserved, invalid, bad, or not allowed should indicate an invalid header. Remember, this is not enough, frame sync can be easily (and very frequently) found in any binary file. Also it is likely that MPEG file contains garbage on it's beginning which also may contain false sync. Thus, you have to check two or more frames in a row to assure you are really dealing with MPEG audio file.
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 length of the frame and use it if you need to read other headers too or just want to calculate the CRC of the frame, to compare it with the one you read from the file. This is actually a very good method to check the MPEG header validity.
Here is "graphical" presentation of the 32 bit header content. Characters from A to M are used to indicate different fields. In the table, you can see details about the content of each field.
76543210 76543210 76543210 76543210
AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
Frame sync (all bits set)
MPEG Audio version ID
00 - MPEG Version 2.5
01 - reserved
10 - MPEG Version 2 (ISO/IEC 13818-3)
11 - MPEG Version 1 (ISO/IEC 11172-3)
Note: MPEG Version 2.5 is not official standard. Bit No 20 in frame header is used to indicate version 2.5. Applications that do not support this MPEG version expect this bit always to be set, meaning that frame sync (A) is twelve bits long, not eleve as stated here. Accordingly, B is one bit long (represents only bit No 19). I recommend using methodology presented here, since this allows you to distinguish all three versions and keep full compatibility.
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
V1 - MPEG Version 1
V2 - MPEG Version 2 and Version 2.5
L1 - Layer I
L2 - Layer II
L3 - Layer III
"free" means free format. If the correct fixed bitrate (such files cannot use variable bitrate) is different than those presented in upper table it must be determined by the application. This may be implemented only for internal purposes since third party applications have no means to find out correct bitrate. Howewer, this is not impossible to do but demands lot's of efforts.
"bad" means that this is not an allowed value
MPEG files may have variable bitrate (VBR). This means that bitrate in the file may change. I have learned about two used methods:
bitrate switching. 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.
bit reservoir. Bitrate may be borrowed (within limits) from previous frames in order to provide more bits to demanding parts of the input signal. This causes, however, that the frames are no longer independent, which means you should not cut this files. This is supported only in Layer III.
For Layer II there are some combinations of bitrate and mode which are not allowed. Here is a list of allowed combinations.
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.
Private bit. It may be freely used for specific needs of an application, i.e. if it has to trigger some application specific events.
00 - Stereo
01 - Joint stereo (Stereo)
10 - Dual channel (Stereo)
11 - Single channel (Mono)
Mode extension (Only if Joint stereo)
Mode extension is used to join informations that are of no use for stereo effect, thus reducing needed resources. These bits are dynamically determined by an encoder in Joint stereo mode.
Complete frequency range of MPEG file is divided in subbands There are 32 subbands. For Layer I & II these two bits determine frequency range (bands) where intensity stereo is applied. For Layer III these two bits determine which type of joint stereo is used (intensity stereo or m/s stereo). Frequency range is determined within decompression algorythm.
0 - Audio is not copyrighted
1 - Audio is copyrighted
0 - Copy of original media
1 - Original media
00 - none
01 - 50/15 ms
10 - reserved
11 - CCIT J.17
- How to calculate frame length
First, let's distinguish two terms frame size and frame length. Frame size is the number of samples contained in a frame. It is constant and always 384 samples for Layer I and 1152 samples for Layer II and Layer III. Frame length is length of a frame when compressed. It is calculated in slots. One slot is 4 bytes long for Layer I, and one byte long for Layer II and Layer III. When you are reading MPEG file you must calculate this to be able to find each consecutive frame. Remember, frame length may change from frame to frame due to padding or bitrate switching.
Read the BitRate, SampleRate and Padding of the frame header.
For Layer I files us this formula:
FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4
For Layer II & III files use this formula:
FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
Layer III, BitRate = 128000, SampleRate = 441000, Padding = 0
==> FrameSize=417 bytes
- Frames per Second
Just as the movie industry has a standard that specifies the number of frames per second in a film in order to guarantee a constant rate of playback on any projector, the MP3 spec employs a similar standard. Regardless of the bitrate of the file, a frame in an MPEG-1 file lasts for 26ms (26/1000 of a second). This works out to around 38fps. If the bitrate is higher, the frame size is simply larger, and vice versa. In addition, the number of samples stored in an MP3 frame is constant, at 1,152 samples per frame.
The total size in bytes for any given frame can be calculated with the following formula: FrameSize = 144 * BitRate / (SampleRate + Padding).
Where the bitrate is measured in bits per second (remember to add the relevant number of zeros to convert from kbps to bps), SampleRate refers to the samplerate of the original input data, and padding refers to extra data added to the frame to fill it up completely in the event that the encoding process leaves unfilled space in the frame. For example, if you're encoding a file at 128 kbps, the original samplerate was 44.1kHz, and no padding bit has been set, the total size of each frame will be 417.96 bytes: 144 * 128000 / (44100 + 0) = 417.96 bytes .
Keeping in mind that each frame contains the header information described above, it would be easy to think that header data accounts for a lot of redundant information being stored and read back. However, keep in mind that each frame header is only 32 bits long. At 38fps, that means you get around 1,223 bits per second of header data, total. Since a file encoded at 128 kbps contains 128,000 bits every second; the total amount of header data is miniscule in comparison to the amount of audio data in the frame itself
- ID3 Tag
Digital audio files can contain, in addition to the audio track, related text and/or graphical information, like Song title, Artist name, Album name, Year and Genre. This is the information displayed when you playback a digital audio file on your computer or portable device.
The process of including information other than sound into these digital audio files is commonly referred to as "tagging" in which you "tag" the audio file with additional information that describes the audio file. The original standard for tagging digital files was developed in 1996 by Eric Kemp and he coined the term ID3. At that time ID3 simply meant "IDentify an MP3".
ID3 tags were designed with the MP3 file format in mind. ID3v2 tags will break formats which are container-based such as Ogg Vorbis and WMA. Here is some information on specific formats:
- ID3 tags work in MP3 and MP3pro files
- WAV has no tags
- WMA has its own tagging format, which is specified in the wma spec, available in the MSDN (which unfortunately, basically does not allow Open Source implementations)
- Ogg Vorbis uses "Xiph Comments" (same as later versions of FLAC and Speex), which are embedded into the Ogg container. You can find information on these in the comment and container specs on www.xiph.org
- AAC uses yet another tagging format, which does not at present have a published spec as of 3/1/2006.
The TagLib Audio Meta-Data Library supports MP3s (with ID3v1, ID3v2 or APE tags), Ogg Vorbis, FLAC (with Xiph Comments or ID3 tags), and MPC files (with APE tags).
- MPEG Audio Tag ID3v1
The TAG is used to describe the MPEG Audio file. It contains information about artist, title, album, publishing year and genre. There is some extra space for comments. It is exactly 128 bytes long and is located at very end of the audio data. You can get it by reading the last 128 bytes of the MPEG audio file.
AAABBBBB BBBBBBBB BBBBBBBB BBBBBBBB
BCCCCCCC CCCCCCCC CCCCCCCC CCCCCCCD
DDDDDDDD DDDDDDDD DDDDDDDD DDDDDEEE
EFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFG
BCCCCCCC CCCCCCCC CCCCCCCC CCCCCCCD
DDDDDDDD DDDDDDDD DDDDDDDD DDDDDEEE
EFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFG
Tag identification. Must contain 'TAG' if tag exists and is correct.
The specification asks for all fields to be padded with null character (ASCII 0). However, not all applications respect this (an example is WinAmp which pads fields with <space>, ASCII 32).
There is a small change proposed in ID3v1.1 structure. The last byte of the Comment field may be used to specify the track number of a song in an album. It should contain a null character (ASCII 0) if the information is unknown.
Genre is a numeric field which may have one of the following values:
'Rock & Roll'
WinAmp expanded this table with next codes:
Any other value should be considered as 'Unknown'
This is new proposed TAG format which is different than ID3v1 and ID3v1.1. In the next blog, let us see in detail about ID3V2.