1 #define _PAGE_ MPEG Audio Frame Header
4 <P>This is a brief and informal document targeted to those who want to deal
5 with the MPEG format. If you are one of them, you probably already know what
6 is MPEG audio. If not, jump to <A
7 HREF="http://www.mp3.com/">http://www.mp3.com/</A> or <A
8 HREF="http://www.layer3.org/">http://www.layer3.org/</A> where you will find
9 more details and also more links. This document does not cover compression and
10 decompression algorithm.
12 <P>NOTE: You cannot just search the Internet and find the MPEG audio specs. It
13 is copyrighted and you will have to pay quite a bit to get the Paper. That's why
14 I made this. Information I got is gathered from the Internet, and mostly originate
15 from program sources I found available for free. Despite my intention to always
16 specify the information sources, I am not able to do it this time. Sorry, I did
17 not maintain the list. :-(
18 <P><B>These are not a decoding specs, it just informs you how to read the </B><A
19 HREF="#MPEG HEADER"><B>MPEG headers</B></A><B> and the </B> <A HREF="#MPEGTAG"
20 TARGET=""><B>MPEG TAG</B></A><B>. MPEG Version 1, 2 and 2.5 and Layer I, II
21 and III are supported, the MP3 TAG (ID3v1 and ID3v1.1) also.</B>. Those of you
22 who use Delphi may find <A
23 HREF="http://www.dv.co.yu/mpgscript/mpgtools.htm">MPGTools Delphi unit (freeware source)</A>
24 useful, it is where I implemented this stuff.
26 <P>I do not claim information presented in this document is accurate. At first
27 I just gathered it from different sources. It was not an easy task but I needed
28 it. Later, I received lots of comments as feedback when I published this document.
29 I think this last release is highly accurate due to comments and corrections I
31 <P>This document is last updated on December 22, 1999.
32 <P ALIGN="center"><FONT SIZE="5"><B>MPEG Audio Compression Basics</B></FONT>
34 <P>This is one of many methods to compress audio in digital form trying to consume
35 as little space as possible but keep audio quality as good as possible. MPEG compression
36 showed up as one of the best achievements in this area.
37 <P>This is a lossy compression, which means, you will certainly loose some audio
38 information when you use this compression methods. But, this lost can hardly be
39 noticed because the compression method tries to control it. By using several quite
40 complicate and demanding mathematical algorithms it will only loose those parts
41 of sound that are hard to be heard even in the original form. This leaves more
42 space for information that is important. This way you can compress audio up to
43 12 times (you may choose compression ratio) which is really significant. Due to
44 its quality MPEG audio became very popular.
45 <P>MPEG standards MPEG-1, MPEG-2 and MPEG-4 are known but this document covers
46 first two of them. There is an unofficial MPEG-2.5 which is rarely used. It is
48 <P><B>MPEG-1 audio</B> (described in ISO/IEC 11172-3) describes three Layers of audio coding with the following properties:
49 <LI>one or two audio channels
50 <LI>sample rate 32kHz, 44.1kHz or 48kHz.
51 <LI>bit rates from 32kbps up to 448kbps<BR>
52 Each layer has its merits.
54 <P><B>MPEG-2 audio</B> (described in ISO/IEC 13818-3) has two extensions to MPEG-1, usually referred as MPEG-2/LSF and MPEG-2/Multichannel.
55 <P>MPEG-2/LSF has the following properties:
56 <LI>one or two audio channels
57 <LI>sample rates half those of MPEG-1
58 <LI>bit rates from 8 kbps up to 256kbps.
60 <P>MPEG-2/Multichannel has the following properties:
61 <LI>up to 5 full range audio channels and an LFE-channel (Low Frequency
62 Enhancement <> subwoofer!)
63 <LI>sample rates the same as those of MPEG-1
64 <LI>highest possible bitrate goes up to about 1Mbps for 5.1
67 <P ALIGN="center"><A NAME="MPEG HEADER"></A><FONT SIZE="5"><B>MPEG Audio Frame
70 <P>An MPEG audio file is built up from smaller parts called frames. Generally,
71 frames are independent items. Each frame has its own header and audio informations.
72 There is no file header. Therefore, you can cut any part of MPEG file and play
73 it correctly (this should be done on frame boundaries but most applications will
74 handle incorrect headers). For Layer III, this is not 100% correct. Due to internal
75 data organization in MPEG version 1 Layer III files, frames are often dependent
76 of each other and they cannot be cut off just like that.
77 <P>When you want to read info about an MPEG file, it is usually enough to find
78 the first frame, read its header and assume that the other frames are the same
79 This may not be always the case. Variable bitrate MPEG files may use so called
80 bitrate switching, which means that bitrate changes according to the content of
81 each frame. This way lower bitrates may be used in frames where it will not reduce
82 sound quality. This allows making better compression while keeping high quality
84 <P>The frame header is constituted by the very first four bytes (32bits) in a
85 frame. The first eleven bits (or first twelve bits, see below about frame sync)
86 of a frame header are always set and they are called "frame sync". Therefore,
87 you can search through the file for the first occurence of frame sync (meaning
88 that you have to find a byte with a value of 255, and followed by a byte with
89 its three (or four) most significant bits set). Then you read the whole header
90 and check if the values are correct. You will see in the following table the exact
91 meaning of each bit in the header, and which values may be checked for validity.
92 Each value that is specified as reserved, invalid, bad, or not allowed should
93 indicate an invalid header. Remember, this is not enough, frame sync can be easily
94 (and very frequently) found in any binary file. Also it is likely that MPEG file
95 contains garbage on it's beginning which also may contain false sync. Thus, you
96 have to check two or more frames in a row to assure you are really dealing with
98 <P>Frames may have a CRC check. The CRC is 16 bits long
99 and, if it exists, it follows the frame header. After the CRC comes the audio
100 data. You may calculate the length of the frame and use it if you need to read
101 other headers too or just want to calculate the CRC of the frame, to compare
102 it with the one you read from the file. This is actually a very good method to
103 check the MPEG header validity.
105 <P>Here is "graphical" presentation of the header content. Characters
106 from A to M are used to indicate different fields. In the table, you can see
107 details about the content of each field.
109 <B><TT><FONT SIZE="5">
110 AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
116 <TABLE BORDER="0" CELLSPACING=7>
117 <TR VALIGN=TOP BGCOLOR="#E2E2E2">
118 <TD>Sign</TD><TD>Length<BR>(bits)</TD><TD>Position<BR>(bits)</TD><TD>Description</TD>
122 <TD>A</TD><TD>11</TD><TD>(31-21)</TD><TD>Frame sync (all bits set)</TD>
126 <TD>B</TD><TD>2</TD><TD>(20,19)</TD><TD>MPEG Audio version ID<BR>
127 00 - MPEG Version 2.5<BR>01 - reserved<BR>10 - MPEG Version 2 (ISO/IEC 13818-3)<BR>11 - MPEG Version 1 (ISO/IEC 11172-3)
128 <P>Note: MPEG Version 2.5 is not official standard. Bit No 20 in frame header
129 is used to indicate version 2.5. Applications that do not support this MPEG version
130 expect this bit always to be set, meaning that frame sync (A) is twelve bits long,
131 not eleve as stated here. Accordingly, B is one bit long (represents only bit
132 No 19). I recommend using methodology presented here, since this allows you to
133 distinguish all three versions and keep full compatibility.
137 <TR VALIGN=TOP><TD>C</TD><TD>2</TD><TD>(18,17)</TD>
138 <TD>Layer description<BR>
146 <TD>D</TD><TD>1</TD><TD>(16)</TD>
147 <TD>Protection bit<BR>
148 0 - Protected by CRC (16bit crc follows header)<BR>
149 1 - Not protected</TD>
151 <TR VALIGN=TOP><TD>E</TD><TD>4</TD><TD>(15,12)</TD><TD>Bitrate index<BR>
152 <TABLE BORDER="1" CELLSPACING="0" CELLPADING="0">
153 <TR><TD>bits</TD><TD>V1,L1</TD><TD>V1,L2</TD><TD>V1,L3</TD><TD>V2,L1</TD><TD>V2, L2 & L3</TD></TR>
154 <TR><TD>0000</TD><TD>free</TD><TD>free</TD><TD>free</TD><TD>free</TD><TD>free</TD></TR>
155 <TR><TD>0001</TD><TD>32</TD><TD>32</TD><TD>32</TD><TD>32</TD><TD>8</TD></TR>
156 <TR><TD>0010</TD><TD>64</TD><TD>48</TD><TD>40</TD><TD>48</TD><TD>16</TD></TR>
157 <TR><TD>0011</TD><TD>96</TD><TD>56</TD><TD>48</TD><TD>56</TD><TD>24</TD></TR>
158 <TR><TD>0100</TD><TD>128</TD><TD>64</TD><TD>56</TD><TD>64</TD><TD>32</TD></TR>
159 <TR><TD>0101</TD><TD>160</TD><TD>80</TD><TD>64</TD><TD>80</TD><TD>40</TD></TR>
160 <TR><TD>0110</TD><TD>192</TD><TD>96</TD><TD>80</TD><TD>96</TD><TD>48</TD></TR>
161 <TR><TD>0111</TD><TD>224</TD><TD>112</TD><TD>96</TD><TD>112</TD><TD>56</TD></TR>
162 <TR><TD>1000</TD><TD>256</TD><TD>128</TD><TD>112</TD><TD>128</TD><TD>64</TD></TR>
163 <TR><TD>1001</TD><TD>288</TD><TD>160</TD><TD>128</TD><TD>144</TD><TD>80</TD></TR>
164 <TR><TD>1010</TD><TD>320</TD><TD>192</TD><TD>160</TD><TD>160</TD><TD>96</TD></TR>
165 <TR><TD>1011</TD><TD>352</TD><TD>224</TD><TD>192</TD><TD>176</TD><TD>112</TD></TR>
166 <TR><TD>1100</TD><TD>384</TD><TD>256</TD><TD>224</TD><TD>192</TD><TD>128</TD></TR>
167 <TR><TD>1101</TD><TD>416</TD><TD>320</TD><TD>256</TD><TD>224</TD><TD>144</TD></TR>
168 <TR><TD>1110</TD><TD>448</TD><TD>384</TD><TD>320</TD><TD>256</TD><TD>160</TD></TR>
169 <TR><TD>1111</TD><TD>bad</TD><TD>bad</TD><TD>bad</TD><TD>bad</TD><TD>bad</TD></TR>
172 NOTES: All values are in kbps<BR>
173 V1 - MPEG Version 1<BR>
174 V2 - MPEG Version 2 and Version 2.5<BR>
178 "free" means free format. If the correct fixed bitrate (such files cannot
179 use variable bitrate) is different than those presented in upper table it must
180 be determined by the application. This may be implemented only for internal purposes
181 since third party applications have no means to find out correct bitrate. Howewer,
182 this is not impossible to do but demands lot's of efforts.<BR>
183 "bad" means that this is not an allowed value
185 <P>MPEG files may have variable bitrate (VBR). This means that bitrate in the file may change. I have learned about two used methods:
186 <LI>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.
187 <LI>bit reservoir. Bitrate may be borrowed (within limits) from previous frames
188 in order to provide more bits to demanding parts of the input signal. This causes,
189 however, that the frames are no longer independent, which means you should not
190 cut this files. This is supported only in Layer III.
191 <P>More about VBR you may find on <A HREF="http://www.xingtech.com/">Xing Tech
193 <P>For Layer II there are some combinations of bitrate and mode which are not
194 allowed. Here is a list of allowed combinations.
195 <TABLE BORDER=1 CELLSPACING=0>
198 <TD>allowed modes</TD>
206 <TD>single channel</TD>
210 <TD>single channel</TD>
214 <TD>single channel</TD>
222 <TD>single channel</TD>
246 <TD>stereo, intensity stereo, dual channel</TD>
250 <TD>stereo, intensity stereo, dual channel</TD>
254 <TD>stereo, intensity stereo, dual channel</TD>
258 <TD>stereo, intensity stereo, dual channel</TD>
263 <TD>F</TD><TD>2</TD><TD>(11,10)</TD>
264 <TD>Sampling rate frequency index (values are in Hz)
265 <TABLE BORDER="1" CELLSPACING="0" CELLPADING="0">
266 <TR><TD>bits</TD><TD>MPEG1</TD><TD>MPEG2</TD><TD>MPEG2.5</TD></TR>
267 <TR><TD>00</TD><TD>44100</TD><TD>22050</TD><TD>11025</TD></TR>
268 <TR><TD>01</TD><TD>48000</TD><TD>24000</TD><TD>12000</TD></TR>
269 <TR><TD>10</TD><TD>32000</TD><TD>16000</TD><TD>8000</TD></TR>
270 <TR><TD>11</TD><TD>reserv.</TD><TD>reserv.</TD><TD>reserv.</TD></TR>
275 <TD>G</TD><TD>1</TD><TD>(9)</TD>
277 0 - frame is not padded<BR>
278 1 - frame is padded with one extra slot<BR>
280 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.
284 <P><B>How to calculate frame length</B>
286 <P>First, let's distinguish two terms frame size and frame length. Frame size
287 is the number of samples contained in a frame. It is constant and always 384 samples
288 for Layer I and 1152 samples for Layer II and Layer III. Frame length is length
289 of a frame when compressed. It is calculated in slots. One slot is 4 bytes long
290 for Layer I, and one byte long for Layer II and Layer III. When you are reading
291 MPEG file you must calculate this to be able to find each consecutive frame. Remember,
292 frame length may change from frame to frame due to padding or bitrate switching.
293 <P>Read the BitRate, SampleRate and Padding of the frame header.
294 <P>For Layer I files us this formula:
295 <P ALIGN=CENTER>FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4
296 <P ALIGN=LEFT>For Layer II & III files use this formula:
297 <P ALIGN=CENTER>FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
299 Layer III, BitRate=128000, SampleRate=44100, Padding=0<BR>
300 ==> FrameSize=417 bytes
303 <TR VALIGN=TOP><TD>H</TD><TD>1</TD><TD>(8)</TD>
304 <TD>Private bit. It may be freely used for specific needs of an application, i.e. if it has to trigger some application specific events.</TD>
307 <TR VALIGN=TOP><TD>I</TD><TD>2</TD><TD>(7,6)</TD>
310 01 - Joint stereo (Stereo)<BR>
311 10 - Dual channel (Stereo)<BR>
312 11 - Single channel (Mono)</TD>
315 <TR VALIGN=TOP><TD>J</TD><TD>2</TD><TD>(5,4)</TD>
316 <TD>Mode extension (Only if Joint stereo)
317 <P>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.
319 <P>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
321 is used (intensity stereo or m/s stereo). Frequency range is determined within decompression algorythm.
323 <TABLE BORDER="0" CELLSPACING="2" CELLPADDING="0">
324 <TR><TD ALIGN=CENTER>Layer I and II</TD><TD ALIGN=CENTER>Layer III</TD></TR>
326 <TABLE BORDER="1" CELLSPACING="0" CELLPADING="0">
327 <TR><TD>value</TD><TD>Layer I & II</TD></TR>
328 <TR><TD>00</TD><TD>bands 4 to 31</TD></TR>
329 <TR><TD>01</TD><TD>bands 8 to 31</TD></TR>
330 <TR><TD>10</TD><TD>bands 12 to 31</TD></TR>
331 <TR><TD>11</TD><TD>bands 16 to 31</TD></TR></TABLE>
335 <TABLE BORDER="1" CELLSPACING="0" CELLPADING="0" WIDTH="212">
336 <TR ALIGN=CENTER><TD>Intensity stereo</TD><TD>MS stereo</TD></TR>
337 <TR ALIGN=CENTER><TD>off</TD><TD>off</TD></TR>
338 <TR ALIGN=CENTER><TD>on</TD><TD>off</TD></TR>
339 <TR ALIGN=CENTER><TD>off</TD><TD>on</TD></TR>
340 <TR ALIGN=CENTER><TD>on</TD><TD>on</TD></TR></TABLE>
344 <TR VALIGN=TOP><TD>K</TD><TD>1</TD><TD>(3)</TD>
346 0 - Audio is not copyrighted<BR>
347 1 - Audio is copyrighted</TD>
350 <TR VALIGN=TOP><TD>L</TD><TD>1</TD><TD>(2)</TD>
352 0 - Copy of original media<BR>
353 1 - Original media</TD>
357 <TD>M</TD><TD>2</TD><TD>(1,0)</TD>
364 <P> </P></CENTER>
366 <P ALIGN="center"><A NAME="MPEGTAG"></A><FONT SIZE="5"><B>MPEG Audio Tag ID3v1</B></FONT>
367 <P>The TAG is used to describe the MPEG Audio file. It contains information
368 about artist, title, album, publishing year and genre. There is some extra
369 space for comments. It is exactly 128 bytes long and is located at very end of
370 the audio data. You can get it by reading the last 128 bytes of the MPEG audio
373 <P ALIGN="center"><FONT SIZE="5"><B><TT>
374 AAABBBBB BBBBBBBB BBBBBBBB BBBBBBBB<BR>
375 BCCCCCCC CCCCCCCC CCCCCCCC CCCCCCCD<BR>
376 DDDDDDDD DDDDDDDD DDDDDDDD DDDDDEEE<BR>
377 EFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFG<BR>
382 <TABLE BORDER="0" WIDTH="500">
383 <TR VALIGN=TOP BGCOLOR=#E2E2E2>
384 <TD>Sign</TD><TD>Length<BR>(bytes)</TD><TD>Position<BR>
385 (bytes)</TD><TD>Description</TD></TR>
386 <TR VALIGN=TOP><TD>A</TD><TD>3</TD><TD>(0-2)</TD>
387 <TD>Tag identification. Must contain 'TAG' if tag exists and is
390 <TR><TD>B</TD><TD>30</TD><TD>(3-32)</TD><TD>Title</TD></TR>
391 <TR><TD>C</TD><TD>30</TD><TD>(33-62)</TD><TD>Artist</TD></TR>
392 <TR><TD>D</TD><TD>30</TD><TD>(63-92)</TD><TD>Album</TD></TR>
393 <TR><TD>E</TD><TD>4</TD><TD>(93-96)</TD><TD>Year</TD></TR>
394 <TR><TD>F</TD><TD>30</TD><TD>(97-126)</TD><TD>Comment</TD></TR>
395 <TR><TD>G</TD><TD>1</TD><TD>(127)</TD><TD>Genre</TD></TR></TABLE>
398 <P>The specification asks for all fields to be padded with null character
399 (ASCII 0). However, not all applications respect this (an example is WinAmp
400 which pads fields with <space>, ASCII 32).
402 <P>There is a small change proposed in <B>ID3v1.1</B> structure. The last byte
403 of the Comment field may be used to specify the track number of a song in an
404 album. It should contain a null character (ASCII 0) if the information is
407 <P>Genre is a numeric field which may have one of the following values:
410 <TABLE BORDER="0" WIDTH="90%">
412 <TD WIDTH="1%">0</TD>
413 <TD WIDTH="20%">'Blues'</TD>
414 <TD WIDTH="1%">20</TD>
415 <TD WIDTH="20%">'Alternative'</TD>
416 <TD WIDTH="1%">40</TD>
417 <TD WIDTH="20%">'AlternRock'</TD>
418 <TD WIDTH="1%">60</TD>
419 <TD WIDTH="20%">'Top 40'</TD>
423 <TD>'Classic Rock'</TD>
429 <TD>'Christian Rap'</TD>
435 <TD>'Death Metal'</TD>
455 <TD>'Soundtrack'</TD>
459 <TD>'Native American'</TD>
465 <TD>'Euro-Techno'</TD>
467 <TD>'Meditative'</TD>
477 <TD>'Instrumental Pop'</TD>
487 <TD>'Instrumental Rock'</TD>
489 <TD>'Psychadelic'</TD>
527 <TD>'Techno-Industrial'</TD>
537 <TD>'Electronic'</TD>
545 <TD>'Instrumental'</TD>
577 <TD>'Southern Rock'</TD>
585 <TD>'Sound Clip'</TD>
599 <TD>'Rock & Roll'</TD>
603 <TD>'Industrial'</TD>
612 <DIV ALIGN="LEFT"><BR>
613 WinAmp expanded this table with next codes: </DIV>
614 <TABLE BORDER="0" WIDTH="90%">
616 <TD WIDTH="1%">80</TD>
617 <TD WIDTH="20%">'Folk'</TD>
619 <TD>'Progressive Rock'</TD>
621 <TD>'Chamber Music'</TD>
628 <TD WIDTH="1%">93</TD>
629 <TD WIDTH="20%">'Psychedelic Rock'</TD>
633 <TD>'Poweer Ballad'</TD>
637 <TD>'National Folk'</TD>
639 <TD>'Symphonic Rock'</TD>
640 <TD WIDTH="1%">106</TD>
641 <TD WIDTH="20%">'Symphony'</TD>
643 <TD>'Rhytmic Soul'</TD>
651 <TD>'Booty Brass'</TD>
652 <TD WIDTH="1%">119</TD>
653 <TD WIDTH="20%">'Freestyle'</TD>
657 <TD>'Fast Fusion'</TD>
671 <TD>'Porn Groove'</TD>
679 <TD>'Easy Listening'</TD>
703 <TD>'Euro-House'</TD>
713 <TD>'Dance Hall'</TD>
717 <TD>'Avantgarde'</TD>
727 <TD>'Gothic Rock'</TD>
736 <TD COLSPAN=8>Any other value should be considered as 'Unknown'
738 <P> </P></CENTER>
740 <P ALIGN="center"><A NAME="MPEG TAG"></A><FONT SIZE="5"><B>MPEG Audio Tag ID3v2</B></FONT>
741 <P>This is new proposed TAG format which is different than ID3v1 and ID3v1.1.
742 Complete tech specs for it may be found at <A
743 HREF="http://www.id3.org/">http://www.id3.org/</A>.
746 <P><FONT SIZE="-1"> Created on September 1998. by <A HREF="http://www.dv.co.yu/broker/">Predrag
748 Thanks to <A HREF="http://www.jps.net/kyunghi/">Jean</A> for debugging and polishing
749 of this document, <A HREF="http://members.tripod.com/~videoripper/home.htm">Peter
750 Luijer</A>, Guwani, Rob Leslie and Franc Zijderveld<BR>
751 for valuable comments and corrections.</FONT></P>
752 <P>© 1998, 1999 Copyright by DataVoyage</P>
753 <P><FONT SIZE="-1">This document may be changed. Check <A
754 HREF="http://www.dv.co.yu/mpgscript/mpeghdr.htm">http://www.dv.co.yu/mpgscript/mpeghdr.htm</A>
756 You may use it freely. Distribution is allowed only in unaltered form. If you
757 can help me make it more accurate, please do. </FONT></P>