Lam Yick Yan wrote:
I have downloaded a Perl Script to record mp3 stream from ShoutCast
server. Now my task is need to determine the property of the mp3 stream:
whether it is stereo, joint-stereo or mono
whether it is a 11kHz, 22kHz, or 44kHz sampling rate stream
whether the bit rate is 16KHz, 32kHz, 64kHZ or 128kHz
The problem is there are a lot of junk before the stream begin,
and there is so many consecutive 12 or 11 1's in the program ( for those who dont understand what I mean, please see document of mpeg header). I have trouble determining the begin of of MP3 stream!! Some decoder does not have this ability and I must tell it the begin of MP3 stream.
Can any one point out where I can find the C/C++ source code
to do that?
This algorithm works pretty well to find correct sync words:
1. Search forward for the next sync word (0xfff or 0xffe for MPEG 2.5). 2. Try to decode the header. If any field is invalid ("forbidden"), the sync word was false; go back to 1. 3. Use the layer, bitrate, sampling frequency, and padding bit to calculate the frame size (and hence, beginning of the next frame.) 4. Verify that a sync word follows at the calculated location. If not, the original sync word was false; go back to 1.
This is what libmad does, generally. For additional verification of a correct sync, you can also:
5. Decode the next frame header. If any field is invalid ("forbidden"), the original sync word was false; go back to 1. 6. Verify that the layer and sampling frequency of the second header match the first. If not, the original sync word was likely false; go back to 1.
These additional checks are performed by libmad only when trying to determine a free format bitrate.
If the stream has CRC protection, you could also use that for additional verification, but the above is pretty effective in most cases.