Where can I get a document that explains MP3 header formats?

Where can I get a document that explains MP3 header formats?

Post by Tom Diet » Wed, 02 Dec 1998 04:00:00



I am trying to write a simple program that parses MP3 files.  I want to be
able to read the header information to extract thing such as BitRate, Sample
Frequency etc.  I found some very old c-source code that I was able to get
to work partially.

It seems with some MP3 files the header information doesn't contain the
correct information, although other MP3 programs display it fine.  This
leads me to believe that the source I have is a bit dated.

I went to the ISO home page, but it looks as though they charge for the spec
docs.

I just need to know how the header information is laid out.  I don't care
about decoding anything.

Thanks,

Tom

 
 
 

Where can I get a document that explains MP3 header formats?

Post by R.Bertran » Wed, 02 Dec 1998 04:00:00


Following is what i found in "MPEG Encoder" documentation:

Hope this can help you.

Rmy.

        FILE STRUCTURE AUDIO MPEG FILE
--------------------------------------------------------------------------
file

        a file has one or more 'frames'.

        a frame has a 'frame header' and 'frame data'
--------------------------------------------------------------------------
frame length

        the frame length depends on the format and can vary per frame.
        the frame ends before the next sync header (see frame header).
--------------------------------------------------------------------------
frame header

        the frame header of an audio mpeg file consist of 4 bytes, which
        are at the beginning of a frame. They don't have to be the same
        for every frame. (p.e. the bit 'padding' can change per frame)

        byte       0        1        2        3
        binary 11111111 1111abbc ddddeefg hhjjkmpp

                1 - sync header
                a - version
                b - layer
                c - error protection
                d - bit rate index
                e - sample rate index
                f - padding
                g - extension
                h - channel mode
                j - mode extension
                k - copyright
                m - original
                p - emphasis
--------------------------------------------------------------------------
1 - sync header

        must be 11111111 1111xxxx ($fffx)
--------------------------------------------------------------------------
a - version

        0 = mpg-ii
        1 = mpg-i
--------------------------------------------------------------------------
b - layer

        0 = layer-1
        3 = layer-2
        2 = layer-3
--------------------------------------------------------------------------
c - error protection

        0 = on
        1 = off
--------------------------------------------------------------------------
d - bit rate

                             mpg-i
        (hex)   bps@38kHz  b...@44.1kHz  bps@48kHz
        $0          0            0           0
        $1         32           32          32
        $2         64           48          40
        $3         96           56          48
        $4        128           64          56
        $5        160           80          64
        $6        192           96          80
        $7        224          112          96
        $8        256          128         112
        $9        288          160         128
        $a        320          192         160
        $b        352          224         192
        $c        384          256         224
        $d        416          320         256
        $e        448          384         320

                            mpg-ii
        (hex)   bps@16kHz  bps@22kHz  bps@24kHz
        $0          0            0           0
        $1         32            8           8
        $2         48           16          16
        $3         56           24          24
        $4         64           32          32
        $5         80           40          40
        $6         96           48          48
        $7        112           56          56
        $8        128           64          64
        $9        144           80          80
        $a        160           96          96
        $b        176          112         112
        $c        192          128         128
        $d        224          144         144
        $e        256          160         160
--------------------------------------------------------------------------
e - sample rate

              mpg-i     mpg-ii  (version)
        0 = 44.1 kHz    22 kHz
        1 =   48 kHz    24 kHz
        2 =   38 kHz    16 kHz
--------------------------------------------------------------------------
f - padding

        1 = unused bits are filled (padding required)
        0 = all bits in frame are used
--------------------------------------------------------------------------
g - extension

        0 = none
        1 = private
--------------------------------------------------------------------------
h - channel mode

        0 = stereo
        1 = joint stereo
        2 = dual channel
        3 = mono
--------------------------------------------------------------------------
j = mode extension

        when encoding with joint stereo, this specifies to which freq.band
        it is bound.

            layer-1&2  layer-3
        0 =     4         0
        1 =     8         4
        2 =    12         8
        3 =    16        16
--------------------------------------------------------------------------
k - copyright

        0 = no
        1 = yes
--------------------------------------------------------------------------
m - original

        0 = no
        1 = yes
--------------------------------------------------------------------------
p - emphasis

        0 = none
        1 = 50/15 microseconds
        3 = CITT j.17
--------------------------------------------------------------------------

 
 
 

1. mp3 frame headers - calculating frame lengths?

I'm working on a small app to run through mp3 frame headers,
mainly to determine bitrate & sample frequency. (and, in the
case of variable bit rate files, i'll want to find the average bit
rate).

The method I'm using is rather basic:
-read bytes until we find frame sync (11 bits set to 1)
-examine the 32 bits beginning with that frame sync,
 find any reserved/invalid values. if we don't find any,
 assume this is a valid frame header.
-calculate bitrate / sample freq.
-move on, find next frame header

Unfortunately, this isn't reliable. While the majority
of frame headers found are legit, some random bit
patterns inside frames appear to be valid frame
headers. Output from a 192 kbps / 44 khz file,
edited to remove rejection cases:

Prelim: Found 255 at byte 1..
Header: Second half of frame sync found at byte 2..
MPEG Version 1 (ISO/IEC 11172-3)
Layer 1 192 kb/s - 44100 hz
...
Prelim: Found 255 at byte 212..
Header: Second half of frame sync found at byte 213..
MPEG Version 1 (ISO/IEC 11172-3)
Layer 3 288 kb/s - 32000 hz
....
Prelim: Found 255 at byte 628..
Header: Second half of frame sync found at byte 629..
MPEG Version 1 (ISO/IEC 11172-3)
Layer 1 192 kb/s - 44100 hz
...
MPEG Version 2 (ISO/IEC 13818-3)
Layer 2 144 kb/s - 24000 hz
...
Prelim: Found 255 at byte 1255..
Header: Second half of frame sync found at byte 1256..
MPEG Version 1 (ISO/IEC 11172-3)
Layer 1 192 kb/s - 44100 hz
...

Clearly, determining a given 32-bit chunk's validity as a
frame header by testing for the frame sync and certain
acceptible values is not sufficient - I need to skip over
the frame contents and read only headers, because
audio data will occasionally resemble a valid frame
header enough to trip my code up.

immediately I noticed that, in the case of my
example file, valid frame headers are occuring
at intervals of 627 bytes. Is there any reliable way
to use bitrate/samplefreq/padding info from each
frame header to calculate the distance to the next
valid frame header?

any help here would be greatly appreciated!

2. directory of sites

3. Any book on Acrobat tagged format (Portable Document Format) ?

4. Solaris pthreads and MUTEXTYPE_RECURSIVE

5. explain EXPLAIN?

6. Source for Multitech MT1432?? Fax Modem

7. Explaining explain ...

8. standard document of MP3

9. XL 5 - Getting Headers/Footers to Line Up With Margin...

10. PDFMaker macro creating new Headers in Word documents

11. Problems using field codes in Header of Word Documents