Help - Search - Members - Calendar
Full Version: Is this a MP3 Frame header
Hydrogenaudio Forums > Lossy Audio Compression > MP3 > MP3 - Tech
paultaylor
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
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
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
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.
paultaylor
thanks vmuch
paultaylor
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
MPEG2 Layer 3 is 576 samples per frame
paultaylor
Ok, is that also true for:

MPEG version2, Layer II
MPEG version2.5, Layer II
MPEG version2.5, Layer III
Sebastian Mares
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.
Sebastian Mares
QUOTE (benski @ Apr 1 2006, 12:52 AM)
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.
*


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
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.