Merge svn changes up to r28712
[mplayer/glamo.git] / libmpdemux / mp3_hdr.c
blobda1826215cd5177c438bb2549e7ac3fdf4af9fbb
1 #include <stdio.h>
3 #include "config.h"
4 #include "mp3_hdr.h"
5 #include "mp_msg.h"
7 //----------------------- mp3 audio frame header parser -----------------------
9 static int tabsel_123[2][3][16] = {
10 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
11 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0},
12 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0} },
14 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
15 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
16 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0} }
19 static long freqs[9] = { 44100, 48000, 32000, // MPEG 1.0
20 22050, 24000, 16000, // MPEG 2.0
21 11025, 12000, 8000}; // MPEG 2.5
23 static int mp_mp3_get_lsf(unsigned char* hbuf){
24 unsigned long newhead =
25 hbuf[0] << 24 |
26 hbuf[1] << 16 |
27 hbuf[2] << 8 |
28 hbuf[3];
29 if( newhead & ((long)1<<20) ) {
30 return (newhead & ((long)1<<19)) ? 0x0 : 0x1;
32 return 1;
36 * return frame size or -1 (bad frame)
38 int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate, int* spf, int* mpa_layer, int* br){
39 int stereo,ssize,lsf,framesize,padding,bitrate_index,sampling_frequency, divisor;
40 int bitrate;
41 int layer, mult[3] = { 12000, 144000, 144000 };
42 unsigned long newhead =
43 hbuf[0] << 24 |
44 hbuf[1] << 16 |
45 hbuf[2] << 8 |
46 hbuf[3];
48 // printf("head=0x%08X\n",newhead);
50 #if 1
51 // head_check:
52 if( (newhead & 0xffe00000) != 0xffe00000 ){
53 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"head_check failed\n");
54 return -1;
56 #endif
58 layer = 4-((newhead>>17)&3);
59 if(layer==4){
60 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"not layer-1/2/3\n");
61 return -1;
64 sampling_frequency = ((newhead>>10)&0x3); // valid: 0..2
65 if(sampling_frequency==3){
66 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid sampling_frequency\n");
67 return -1;
70 if( newhead & ((long)1<<20) ) {
71 // MPEG 1.0 (lsf==0) or MPEG 2.0 (lsf==1)
72 lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
73 sampling_frequency += (lsf*3);
74 } else {
75 // MPEG 2.5
76 lsf = 1;
77 sampling_frequency += 6;
80 // crc = ((newhead>>16)&0x1)^0x1;
81 bitrate_index = ((newhead>>12)&0xf); // valid: 1..14
82 padding = ((newhead>>9)&0x1);
83 // fr->extension = ((newhead>>8)&0x1);
84 // fr->mode = ((newhead>>6)&0x3);
85 // fr->mode_ext = ((newhead>>4)&0x3);
86 // fr->copyright = ((newhead>>3)&0x1);
87 // fr->original = ((newhead>>2)&0x1);
88 // fr->emphasis = newhead & 0x3;
90 stereo = ( (((newhead>>6)&0x3)) == 3) ? 1 : 2;
92 // !checked later through tabsel_123[]!
93 // if(!bitrate_index || bitrate_index==15){
94 // mp_msg(MSGT_DEMUXER,MSGL_DBG2,"Free format not supported.\n");
95 // return -1;
96 // }
98 if(lsf)
99 ssize = (stereo == 1) ? 9 : 17;
100 else
101 ssize = (stereo == 1) ? 17 : 32;
102 if(!((newhead>>16)&0x1)) ssize += 2; // CRC
104 bitrate = tabsel_123[lsf][layer-1][bitrate_index];
105 framesize = bitrate * mult[layer-1];
107 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"FRAMESIZE: %d, layer: %d, bitrate: %d, mult: %d\n",
108 framesize, layer, tabsel_123[lsf][layer-1][bitrate_index], mult[layer-1]);
109 if(!framesize){
110 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid framesize/bitrate_index\n");
111 return -1;
114 divisor = (layer == 3 ? (freqs[sampling_frequency] << lsf) : freqs[sampling_frequency]);
115 framesize /= divisor;
116 if(layer==1)
117 framesize = (framesize+padding)*4;
118 else
119 framesize += padding;
121 // if(framesize<=0 || framesize>MAXFRAMESIZE) return FALSE;
122 if(srate) {
123 *srate = freqs[sampling_frequency];
124 if(spf) {
125 if(layer == 1)
126 *spf = 384;
127 else if(layer == 2)
128 *spf = 1152;
129 else if(*srate < 32000)
130 *spf = 576;
131 else
132 *spf = 1152;
135 if(mpa_layer) *mpa_layer = layer;
136 if(chans) *chans = stereo;
137 if(br) *br = bitrate;
139 return framesize;