2001-10-31 14:42:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2002-02-14 13:04:35 +00:00
|
|
|
#include "../mp_msg.h"
|
2001-10-31 14:42:32 +00:00
|
|
|
|
|
|
|
//----------------------- mp3 audio frame header parser -----------------------
|
|
|
|
|
|
|
|
static int tabsel_123[2][3][16] = {
|
2002-10-25 14:33:51 +00:00
|
|
|
{ {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
|
|
|
|
{0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0},
|
|
|
|
{0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0} },
|
2001-10-31 14:42:32 +00:00
|
|
|
|
2002-10-25 14:33:51 +00:00
|
|
|
{ {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
|
|
|
|
{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
|
|
|
|
{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0} }
|
2001-10-31 14:42:32 +00:00
|
|
|
};
|
|
|
|
static long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
|
|
|
|
|
2002-04-24 00:49:06 +00:00
|
|
|
int mp_mp3_get_lsf(unsigned char* hbuf){
|
|
|
|
unsigned long newhead =
|
|
|
|
hbuf[0] << 24 |
|
|
|
|
hbuf[1] << 16 |
|
|
|
|
hbuf[2] << 8 |
|
|
|
|
hbuf[3];
|
|
|
|
if( newhead & ((long)1<<20) ) {
|
|
|
|
return (newhead & ((long)1<<19)) ? 0x0 : 0x1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-10-31 14:42:32 +00:00
|
|
|
/*
|
|
|
|
* return frame size or -1 (bad frame)
|
|
|
|
*/
|
2002-07-21 14:36:33 +00:00
|
|
|
int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate){
|
2001-10-31 14:42:32 +00:00
|
|
|
int stereo,ssize,crc,lsf,mpeg25,framesize,padding,bitrate_index,sampling_frequency;
|
|
|
|
unsigned long newhead =
|
|
|
|
hbuf[0] << 24 |
|
|
|
|
hbuf[1] << 16 |
|
|
|
|
hbuf[2] << 8 |
|
|
|
|
hbuf[3];
|
|
|
|
|
|
|
|
// printf("head=0x%08X\n",newhead);
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
// head_check:
|
|
|
|
if( (newhead & 0xffe00000) != 0xffe00000 ||
|
|
|
|
(newhead & 0x0000fc00) == 0x0000fc00){
|
2002-02-14 13:04:35 +00:00
|
|
|
mp_msg(MSGT_DEMUXER,MSGL_DBG2,"head_check failed\n");
|
2001-10-31 14:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-02-14 13:04:35 +00:00
|
|
|
if((4-((newhead>>17)&3))!=3){
|
|
|
|
mp_msg(MSGT_DEMUXER,MSGL_DBG2,"not layer-3\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-10-31 14:42:32 +00:00
|
|
|
|
|
|
|
if( newhead & ((long)1<<20) ) {
|
|
|
|
lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
|
|
|
|
mpeg25 = 0;
|
|
|
|
} else {
|
|
|
|
lsf = 1;
|
|
|
|
mpeg25 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mpeg25)
|
|
|
|
sampling_frequency = 6 + ((newhead>>10)&0x3);
|
|
|
|
else
|
|
|
|
sampling_frequency = ((newhead>>10)&0x3) + (lsf*3);
|
|
|
|
|
|
|
|
if(sampling_frequency>8){
|
2002-02-14 13:04:35 +00:00
|
|
|
mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid sampling_frequency\n");
|
2001-10-31 14:42:32 +00:00
|
|
|
return -1; // valid: 0..8
|
|
|
|
}
|
|
|
|
|
|
|
|
crc = ((newhead>>16)&0x1)^0x1;
|
|
|
|
bitrate_index = ((newhead>>12)&0xf);
|
|
|
|
padding = ((newhead>>9)&0x1);
|
|
|
|
// fr->extension = ((newhead>>8)&0x1);
|
|
|
|
// fr->mode = ((newhead>>6)&0x3);
|
|
|
|
// fr->mode_ext = ((newhead>>4)&0x3);
|
|
|
|
// fr->copyright = ((newhead>>3)&0x1);
|
|
|
|
// fr->original = ((newhead>>2)&0x1);
|
|
|
|
// fr->emphasis = newhead & 0x3;
|
|
|
|
|
|
|
|
stereo = ( (((newhead>>6)&0x3)) == 3) ? 1 : 2;
|
|
|
|
|
|
|
|
if(!bitrate_index){
|
2002-02-14 13:04:35 +00:00
|
|
|
mp_msg(MSGT_DEMUXER,MSGL_DBG2,"Free format not supported.\n");
|
2001-10-31 14:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(lsf)
|
|
|
|
ssize = (stereo == 1) ? 9 : 17;
|
|
|
|
else
|
|
|
|
ssize = (stereo == 1) ? 17 : 32;
|
|
|
|
if(crc) ssize += 2;
|
|
|
|
|
2002-10-25 14:33:51 +00:00
|
|
|
framesize = tabsel_123[lsf][2][bitrate_index] * 144000;
|
|
|
|
|
|
|
|
if(!framesize){
|
|
|
|
mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid framesize/bitrate_index\n");
|
|
|
|
return -1; // valid: 1..14
|
|
|
|
}
|
|
|
|
|
2001-10-31 14:42:32 +00:00
|
|
|
framesize /= freqs[sampling_frequency]<<lsf;
|
|
|
|
framesize += padding;
|
|
|
|
|
|
|
|
// if(framesize<=0 || framesize>MAXFRAMESIZE) return FALSE;
|
2002-07-21 14:36:33 +00:00
|
|
|
if(srate) *srate = freqs[sampling_frequency];
|
|
|
|
if(chans) *chans = stereo;
|
2001-10-31 14:42:32 +00:00
|
|
|
|
|
|
|
return framesize;
|
|
|
|
}
|
|
|
|
|