10l fix by Jindrich Makovicka
[mplayer/greg.git] / libmpdemux / mp3_hdr.c
blob53528d9fbe77b2fd1773cf664c4cc899c793d0a5
1 #include <stdio.h>
3 #include "config.h"
4 #include "../mp_msg.h"
6 //----------------------- mp3 audio frame header parser -----------------------
8 static int tabsel_123[2][3][16] = {
9 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
10 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0},
11 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0} },
13 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
14 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
15 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0} }
18 static long freqs[9] = { 44100, 48000, 32000, // MPEG 1.0
19 22050, 24000, 16000, // MPEG 2.0
20 11025, 12000, 8000}; // MPEG 2.5
22 int mp_mp3_get_lsf(unsigned char* hbuf){
23 unsigned long newhead =
24 hbuf[0] << 24 |
25 hbuf[1] << 16 |
26 hbuf[2] << 8 |
27 hbuf[3];
28 if( newhead & ((long)1<<20) ) {
29 return (newhead & ((long)1<<19)) ? 0x0 : 0x1;
31 return 1;
35 * return frame size or -1 (bad frame)
37 int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate){
38 int stereo,ssize,lsf,framesize,padding,bitrate_index,sampling_frequency;
39 unsigned long newhead =
40 hbuf[0] << 24 |
41 hbuf[1] << 16 |
42 hbuf[2] << 8 |
43 hbuf[3];
45 // printf("head=0x%08X\n",newhead);
47 #if 1
48 // head_check:
49 if( (newhead & 0xffe00000) != 0xffe00000 ){
50 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"head_check failed\n");
51 return -1;
53 #endif
55 if((4-((newhead>>17)&3))!=3){
56 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"not layer-3\n");
57 return -1;
60 sampling_frequency = ((newhead>>10)&0x3); // valid: 0..2
61 if(sampling_frequency==3){
62 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid sampling_frequency\n");
63 return -1;
66 if( newhead & ((long)1<<20) ) {
67 // MPEG 1.0 (lsf==0) or MPEG 2.0 (lsf==1)
68 lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
69 sampling_frequency += (lsf*3);
70 } else {
71 // MPEG 2.5
72 lsf = 1;
73 sampling_frequency += 6;
76 // crc = ((newhead>>16)&0x1)^0x1;
77 bitrate_index = ((newhead>>12)&0xf); // valid: 1..14
78 padding = ((newhead>>9)&0x1);
79 // fr->extension = ((newhead>>8)&0x1);
80 // fr->mode = ((newhead>>6)&0x3);
81 // fr->mode_ext = ((newhead>>4)&0x3);
82 // fr->copyright = ((newhead>>3)&0x1);
83 // fr->original = ((newhead>>2)&0x1);
84 // fr->emphasis = newhead & 0x3;
86 stereo = ( (((newhead>>6)&0x3)) == 3) ? 1 : 2;
88 // !checked later through tabsel_123[]!
89 // if(!bitrate_index || bitrate_index==15){
90 // mp_msg(MSGT_DEMUXER,MSGL_DBG2,"Free format not supported.\n");
91 // return -1;
92 // }
94 if(lsf)
95 ssize = (stereo == 1) ? 9 : 17;
96 else
97 ssize = (stereo == 1) ? 17 : 32;
98 if(!((newhead>>16)&0x1)) ssize += 2; // CRC
100 framesize = tabsel_123[lsf][2][bitrate_index] * 144000;
102 if(!framesize){
103 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid framesize/bitrate_index\n");
104 return -1;
107 framesize /= freqs[sampling_frequency]<<lsf;
108 framesize += padding;
110 // if(framesize<=0 || framesize>MAXFRAMESIZE) return FALSE;
111 if(srate) *srate = freqs[sampling_frequency];
112 if(chans) *chans = stereo;
114 return framesize;