2588
|
1 #include <stdio.h>
|
|
2
|
|
3 #include "config.h"
|
|
4
|
|
5 //----------------------- mp3 audio frame header parser -----------------------
|
|
6
|
|
7 static int tabsel_123[2][3][16] = {
|
|
8 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
|
|
9 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
|
|
10 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
|
|
11
|
|
12 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
|
|
13 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
|
|
14 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
|
|
15 };
|
|
16 static long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
|
|
17
|
|
18 /*
|
|
19 * return frame size or -1 (bad frame)
|
|
20 */
|
|
21 int mp_decode_mp3_header(unsigned char* hbuf){
|
|
22 int stereo,ssize,crc,lsf,mpeg25,framesize,padding,bitrate_index,sampling_frequency;
|
|
23 unsigned long newhead =
|
|
24 hbuf[0] << 24 |
|
|
25 hbuf[1] << 16 |
|
|
26 hbuf[2] << 8 |
|
|
27 hbuf[3];
|
|
28
|
|
29 // printf("head=0x%08X\n",newhead);
|
|
30
|
|
31 #if 1
|
|
32 // head_check:
|
|
33 if( (newhead & 0xffe00000) != 0xffe00000 ||
|
|
34 (newhead & 0x0000fc00) == 0x0000fc00){
|
|
35 printf("head_check failed\n");
|
|
36 return -1;
|
|
37 }
|
|
38 #endif
|
|
39
|
|
40 if((4-((newhead>>17)&3))!=3){ printf("not layer-3\n"); return -1;}
|
|
41
|
|
42 if( newhead & ((long)1<<20) ) {
|
|
43 lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
|
|
44 mpeg25 = 0;
|
|
45 } else {
|
|
46 lsf = 1;
|
|
47 mpeg25 = 1;
|
|
48 }
|
|
49
|
|
50 if(mpeg25)
|
|
51 sampling_frequency = 6 + ((newhead>>10)&0x3);
|
|
52 else
|
|
53 sampling_frequency = ((newhead>>10)&0x3) + (lsf*3);
|
|
54
|
|
55 if(sampling_frequency>8){
|
|
56 printf("invalid sampling_frequency\n");
|
|
57 return -1; // valid: 0..8
|
|
58 }
|
|
59
|
|
60 crc = ((newhead>>16)&0x1)^0x1;
|
|
61 bitrate_index = ((newhead>>12)&0xf);
|
|
62 padding = ((newhead>>9)&0x1);
|
|
63 // fr->extension = ((newhead>>8)&0x1);
|
|
64 // fr->mode = ((newhead>>6)&0x3);
|
|
65 // fr->mode_ext = ((newhead>>4)&0x3);
|
|
66 // fr->copyright = ((newhead>>3)&0x1);
|
|
67 // fr->original = ((newhead>>2)&0x1);
|
|
68 // fr->emphasis = newhead & 0x3;
|
|
69
|
|
70 stereo = ( (((newhead>>6)&0x3)) == 3) ? 1 : 2;
|
|
71
|
|
72 if(!bitrate_index){
|
|
73 fprintf(stderr,"Free format not supported.\n");
|
|
74 return -1;
|
|
75 }
|
|
76
|
|
77 if(lsf)
|
|
78 ssize = (stereo == 1) ? 9 : 17;
|
|
79 else
|
|
80 ssize = (stereo == 1) ? 17 : 32;
|
|
81 if(crc) ssize += 2;
|
|
82
|
|
83 framesize = (long) tabsel_123[lsf][2][bitrate_index] * 144000;
|
|
84 framesize /= freqs[sampling_frequency]<<lsf;
|
|
85 framesize += padding;
|
|
86
|
|
87 // if(framesize<=0 || framesize>MAXFRAMESIZE) return FALSE;
|
|
88
|
|
89 return framesize;
|
|
90 }
|
|
91
|