Hello,
 
My name is Matt Schaller, and I have been attempting to read each frame off of an MPEG2 file. I have been succesfull with all MP3s with type MPEG1, but with MPEG2 I am having some difficulties.
 
I have been attemping to read each frame off of an mp3 called s1.mp3. Its statistics are (and its binary value, in parenthesis):
 
MPEG 2 (10)
Layer III (01)
56,000 bits (0111)
22050 hz (00)
Joint Stereo (01)
mode extension: 10
 
The first four bytes of its header is 0xFFF37060 or 11111111111100110111000001100000 in binary.
 
The reference page I have been using is: http://www.opennet.ru/docs/formats/mpeghdr.html
 
According to the way to calculate the frame size, it is:
 
FrameSize = ( ( 144 * BitRate ) / SampleRate ) + Padding
 
For s1.mp3, it is:
 
FrameSize = ( ( 144 * 56000 ) / 22050 ) + 0
 
The frame size roughly equals 365.71, or 365 because it is being stored in an integer. All  is good until I advance to the next frame, which the program assumes is going to be at point 0x16D (365 in decimal) in the file. When I seek to that position, I am starting to read the second byte in the header, not the first. The header actually starts at 0x16C (364 in decimal), one byte short of my frame size. (I use a hex editor while I am debugging so that I can see exactly what my program is reading.) The header indeed starts at position 364 in the file.
 
I did some researching. If the frame size is 365 bytes, and the file is 3982079 bytes long, then there should be 10909 frames in the file (3982079 / 365). I opened up Winamp, and viewed the MPEG details for s1.mp3. It displayed that s1.mp3 contained 21879 frames -- roughly twice the amount of my figure! I went into the hex editor and looked at position 182 (365 divided by two, rounded). Sure enough, at point 182 in the file, there was a file header.
 
I jumped forward another 182 bytes, and I reached position 364 which is what I wanted in the first place! It was then that I realized that the frames were split in half.
 
What certain qualities must a MPEG2 file have in order to have a frame header split into two? Surely programs like Winamp must be able to differentiate between using the actual frame size that is calculated or using half of it. At the moment, my program cannot, which is why it is crashing because it is not reading bytes in the right position (which throws off the bitrates, sampling rates, etc).
 
Any help would be greatly, greatly appreciated. If anyone could explain the qualities a MPEG2 file must have in order to have two frames where there should be one, I would really appreciate it.
 
I am using C++, I can send code and/or the MP3 if needed...
 
Thanks again,
 
Matt Schaller
 
 
 
 
 

Reply via email to