Skip to main content

Topic: Is this a MP3 Frame header (Read 7942 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
  • paultaylor
  • [*][*][*]
Is this a MP3 Frame header
I have some code that identifis the first MPEG frame header in an audio file, it works ok 99% of the time but is sometimes incorrectly indentifying ID3 tagging data incorrectly as audio data when the ID3 tag has not been unsynchronised.

The 4 bytes incorrectly identified as a frame header were FF FE C9 FF, is there anything that could be done to identify this as not being mpeg frame header, I check all the stuff I know off (Layer/version/bitrate/padding bits ...) .

  • Peter
  • [*][*][*][*][*]
  • Administrator
Is this a MP3 Frame header
Reply #1
My solution is to accept the stream only when you see two valid frames one immediately after another, with no syncwords inbetween and same MPEG/layer/numberofchannels/samplerate info in both frame headers. Chance of random garbage spoofing that is essentially zero.

  • paultaylor
  • [*][*][*]
Is this a MP3 Frame header
Reply #2
Thanks, I have tried to do just that but my method that calculates the frame size so I know where the next frame should start does not return integral numbers, I dont think it returns the correct number with the result that I was then rejecting valid headers because I was looking for the next header in the wrong place.
Psuedo Code shown below if anyone can see anything wrong with it.

switch (layer)
{
    case LAYER_I:
        return 12 * getBitRate()  / getSamplingRate() + getPaddingLength()) * 4;

    case LAYER_II:
        return 144 * getBitRate()  / getSamplingRate() + getPaddingLength() ;

    case LAYER_III:
        return 144 * getBitRate()  / getSamplingRate() + getPaddingLength();
}

  • benski
  • [*][*][*][*][*]
  • Developer
Is this a MP3 Frame header
Reply #3
Make sure you use 72* instead of 144* if it's MPEG-2 or MPEG-2.5 (See: http://minnie.tuhs.org/pipermail/mp3encode...ry/005598.html)

Also, you need to round down, and your integer math might be rounding up.
  • Last Edit: 31 March, 2006, 05:58:39 PM by benski

  • paultaylor
  • [*][*][*]
Is this a MP3 Frame header
Reply #4
thanks vmuch

  • paultaylor
  • [*][*][*]
Is this a MP3 Frame header
Reply #5
Ok, Ive done all this and compared my results to that given by Winamp & Itunes, the caculation of frame length, and number of frames is now always correct and working properly.

But it has made another problem I was having worse, calculating the audio length worse. i have an audio file that lasts for 4mins 30 secs, however my program,Itunes and Winamp all report it as 8mins 6 secs, having changed my framelength calculation from 144 -> 72, I calculate the audio length as 16 mins 12 secs (double)


Im calculating track length as

numberOfFrames * noOfSamplesPerFrame  / SamplingRate

18612 * 1152 / 22050 = 972  = 16 mins 12 secs

The MPEG is MPEG Version2 Layer III, Im sure the framecount is correct. The sampling rate is also as reported by other tools but I wonder if i have to multiply it by two to
compensate for the 144 - 72 change,  I dont know if the number of frames per sample is correct.

  • benski
  • [*][*][*][*][*]
  • Developer
Is this a MP3 Frame header
Reply #6
MPEG2 Layer 3 is 576 samples per frame

  • paultaylor
  • [*][*][*]
Is this a MP3 Frame header
Reply #7
Ok, is that also true for:

MPEG version2, Layer II
MPEG version2.5, Layer II
MPEG version2.5, Layer III

Is this a MP3 Frame header
Reply #8
Samples per frame:

MPEG 1 Layer 1 = 384
MPEG 1 Layer 2 = 1152
MPEG 1 Layer 3 = 1152

MPEG 2 Layer 1 = 384
MPEG 2 Layer 2 = 1152
MPEG 2 Layer 3 = 576

MPEG 2 and 2.5 are the same in this regard.
  • Last Edit: 03 April, 2006, 11:09:36 AM by Sebastian Mares

Is this a MP3 Frame header
Reply #9
Quote
Make sure you use 72* instead of 144* if it's MPEG-2 or MPEG-2.5 (See: http://minnie.tuhs.org/pipermail/mp3encode...ry/005598.html)

Also, you need to round down, and your integer math might be rounding up.
[a href="index.php?act=findpost&pid=377617"][{POST_SNAPBACK}][/a]


Layer I is always 48.

So, to sum up, this is what I use (and according to multiple tests, it also works):

Frame Number = Audio Size in Bytes / (Coefficient * Bit Rate in kbps * 1000 / Sampling Rate in Hz + Padding)

Where Coefficient is:

MPEG 1 Layer 1 = 48
MPEG 1 Layer 2 = 144
MPEG 1 Layer 3 = 144

MPEG 2 Layer 1 = 48
MPEG 2 Layer 2 = 144
MPEG 2 Layer 3 = 72

For Layer II and III, Padding is 1 when the padding bit is set and 0 when the padding bit is not set. For Layer I, Padding is 4 or 0.

Then we have:

Duration in s = Frame Number * Sample Size / Sampling Rate in Hz

Where Sample Size is:

MPEG 1 Layer 1 = 384
MPEG 1 Layer 2 = 1152
MPEG 1 Layer 3 = 1152

MPEG 2 Layer 1 = 384
MPEG 2 Layer 2 = 1152
MPEG 2 Layer 3 = 576
  • Last Edit: 03 April, 2006, 11:18:18 AM by Sebastian Mares