annotate libmpdemux/mp3_hdr.c @ 4449:501473faf365

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