HydrogenAudio

Lossy Audio Compression => MP3 => MP3 - Tech => Topic started by: samokan on 2011-12-16 05:57:10

Title: Parsing the MP3 Data
Post by: samokan on 2011-12-16 05:57:10
Hello Everyone.

I am still a newbie when it comes to MP3 and its detail/internal structure.

This past few days, I have been parsing different MP3 files most of which have ID3vxx format. So basically to get the Mp3 data, I just read the data after the tag in case of ID3v2 and before the tag in case of ID3v1.

So now I am given a Lame encoded? MP3 file. I can get the Header Information but not the actual audio data. 

So basically what I'm trying to do is to get the MP3 header and MP3 data only and ignore any tag/header or info within it.

Thank you for any information.

** I hope I posted this in the correct area 
Title: Parsing the MP3 Data
Post by: mjb2006 on 2011-12-16 07:19:01
Is http://www.codeproject.com/KB/audio-video/mpegaudioinfo.aspx (http://www.codeproject.com/KB/audio-video/mpegaudioinfo.aspx) any help?
Title: Parsing the MP3 Data
Post by: samokan on 2011-12-16 08:12:35
Is http://www.codeproject.com/KB/audio-video/mpegaudioinfo.aspx (http://www.codeproject.com/KB/audio-video/mpegaudioinfo.aspx) any help?


Thank you for that sample code.  Besides the VBR/Xing Headers area, my code basically has the same concepts.
I dump the HEX value of the mp3 file and there is no "Xing", "Info".

One file have the LAME3.94 Version somewhere at the bottom of the file and the on the other file it is scattered all over the file.
So basically I need to know the starting point of the audio stream and how big it is so that I can read it from the file and copy it to the memory and give it to the decoder .. 

Title: Parsing the MP3 Data
Post by: alexeysp on 2011-12-16 12:20:59
So basically I need to know the starting point of the audio stream and how big it is so that I can read it from the file and copy it to the memory and give it to the decoder ..


To find the start of the actual audio stream you have to skip the ID3v2 tag if it is present, using the tag size field from the tag itself, and then start scanning the stream for a valid mp3 frame header, which always starts from the 11 bits all set to one. Then parse the header to verify that this is actually a valid frame and find out its size.

If the first frame of the stream does not contain VBR ("Xing" or "Info") header, then there is no way to find out the length of the entire stream except to parse it frame-by-frame until there are no more data to process.

Title: Parsing the MP3 Data
Post by: pdq on 2011-12-16 14:12:46
One file have the LAME3.94 Version somewhere at the bottom of the file and the on the other file it is scattered all over the file.

I believe that LAME fills any bits not used for data with its version string, thus you will see that throughout the file.
Title: Parsing the MP3 Data
Post by: Sebastian Mares on 2011-12-16 14:31:49
Right, LAME uses its version string as frame padding.
Title: Parsing the MP3 Data
Post by: samokan on 2011-12-19 02:00:10
So basically I need to know the starting point of the audio stream and how big it is so that I can read it from the file and copy it to the memory and give it to the decoder ..


To find the start of the actual audio stream you have to skip the ID3v2 tag if it is present, using the tag size field from the tag itself, and then start scanning the stream for a valid mp3 frame header, which always starts from the 11 bits all set to one. Then parse the header to verify that this is actually a valid frame and find out its size.

If the first frame of the stream does not contain VBR ("Xing" or "Info") header, then there is no way to find out the length of the entire stream except to parse it frame-by-frame until there are no more data to process.


That is what I did for files with ID3v2 tags and ID3v1 tags. 

Unfortunately, my test file right now does not contain "Xing" or "Info" header that is why I am having a hard time knowing where the actual audio stream are located in the file. Our decoder is very hardware specific and the api is very limited and not very friendly to use 

Thank you for the  information though.
Title: Parsing the MP3 Data
Post by: samokan on 2011-12-19 02:04:09
One file have the LAME3.94 Version somewhere at the bottom of the file and the on the other file it is scattered all over the file.

I believe that LAME fills any bits not used for data with its version string, thus you will see that throughout the file.


So I can just  basically ignore this data ?

Title: Parsing the MP3 Data
Post by: pdq on 2011-12-19 04:33:22
Correct. You could replace it with almost anything else and the file would play exactly the same.
Title: Parsing the MP3 Data
Post by: samokan on 2011-12-19 08:54:16
Thank you. I was able to find some source code for reference:

mp3val : http://mp3val.sourceforge.net/ (http://mp3val.sourceforge.net/)
mp3diags: http://mp3diags.sourceforge.net/index.html (http://mp3diags.sourceforge.net/index.html)

No Xing or Info Tag Header within the file so I guess I have to manipulate per frame and check if the decoder will be able to play it.

Will update as soon as I get it working
Title: Parsing the MP3 Data
Post by: Sebastian Mares on 2011-12-19 11:12:14
What I don't fully understand is what you are trying to achieve. The LAME version string at the end of the file is part of a valid MP3 frame, so is the Xing / Info data at the beginning of the file.
Title: Parsing the MP3 Data
Post by: samokan on 2012-01-05 03:37:45
What I don't fully understand is what you are trying to achieve. The LAME version string at the end of the file is part of a valid MP3 frame, so is the Xing / Info data at the beginning of the file.


I need only the actual audio stream. The decoder is very hardware specific and very limited API.
So I am stripping all the additional information and get the actual stream give to the decoder and check if it can play it or not.

Title: Parsing the MP3 Data
Post by: benski on 2012-01-05 16:06:04
once you skip the ID3v2 tag, you can basically send the data to the decoder.  If the decoder requires complete frames, you'll need to parse the 4 byte MPEG frame header to determine the complete size of the frame (trivially computed from the bitrate index, samplerate index, layer and version.  unfortunately the byte size isn't encoded directly like it is with ADTS AAC, for example).  As Sebastian stated, things like "Xing Headers" and "LAME padding area" are actually parts of valid MP3 data and your decoder will be able to deal with it.  In this case, LAME is putting useful, but not necessary for decoding, data into the "ancillary data" portion of the fist MP3 frame. Not all encoders produce this silent first frame with data embedded in the ancillary data.
Title: Parsing the MP3 Data
Post by: samokan on 2012-01-06 08:18:55
thanks for pointing that out. I check all my working test mp3 and some of them were actually lame encoded but with an ID3 tag .

I can see the light.  I will update once I tested them all
Title: Parsing the MP3 Data
Post by: samokan on 2012-01-10 07:11:47
thank you for all the response. it is finally working.

appreciate all the help guys