comparison libmpdemux/mp3_hdr.c @ 2588:19731609ea59

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