Move printing of ID_AID_???_LANG to ts_add_stream instead of ts_detect_streams.
[mplayer/glamo.git] / libmpdemux / mp3_hdr.c
blob9c649d7e1b60e52f11afe99eb38aa2bc0d140f1d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
21 #include "config.h"
22 #include "mp_msg.h"
24 //----------------------- mp3 audio frame header parser -----------------------
26 static int tabsel_123[2][3][16] = {
27 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
28 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0},
29 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0} },
31 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
32 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
33 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0} }
36 static long freqs[9] = { 44100, 48000, 32000, // MPEG 1.0
37 22050, 24000, 16000, // MPEG 2.0
38 11025, 12000, 8000}; // MPEG 2.5
40 int mp_mp3_get_lsf(unsigned char* hbuf){
41 unsigned long newhead =
42 hbuf[0] << 24 |
43 hbuf[1] << 16 |
44 hbuf[2] << 8 |
45 hbuf[3];
46 if( newhead & ((long)1<<20) ) {
47 return (newhead & ((long)1<<19)) ? 0x0 : 0x1;
49 return 1;
53 * return frame size or -1 (bad frame)
55 int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate, int* spf, int* mpa_layer, int* br){
56 int stereo,ssize,lsf,framesize,padding,bitrate_index,sampling_frequency, divisor;
57 int bitrate;
58 int layer, mult[3] = { 12000, 144000, 144000 };
59 unsigned long newhead =
60 hbuf[0] << 24 |
61 hbuf[1] << 16 |
62 hbuf[2] << 8 |
63 hbuf[3];
65 // printf("head=0x%08X\n",newhead);
67 #if 1
68 // head_check:
69 if( (newhead & 0xffe00000) != 0xffe00000 ){
70 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"head_check failed\n");
71 return -1;
73 #endif
75 layer = 4-((newhead>>17)&3);
76 if(layer==4){
77 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"not layer-1/2/3\n");
78 return -1;
81 sampling_frequency = ((newhead>>10)&0x3); // valid: 0..2
82 if(sampling_frequency==3){
83 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid sampling_frequency\n");
84 return -1;
87 if( newhead & ((long)1<<20) ) {
88 // MPEG 1.0 (lsf==0) or MPEG 2.0 (lsf==1)
89 lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
90 sampling_frequency += (lsf*3);
91 } else {
92 // MPEG 2.5
93 lsf = 1;
94 sampling_frequency += 6;
97 // crc = ((newhead>>16)&0x1)^0x1;
98 bitrate_index = ((newhead>>12)&0xf); // valid: 1..14
99 padding = ((newhead>>9)&0x1);
100 // fr->extension = ((newhead>>8)&0x1);
101 // fr->mode = ((newhead>>6)&0x3);
102 // fr->mode_ext = ((newhead>>4)&0x3);
103 // fr->copyright = ((newhead>>3)&0x1);
104 // fr->original = ((newhead>>2)&0x1);
105 // fr->emphasis = newhead & 0x3;
107 stereo = ( (((newhead>>6)&0x3)) == 3) ? 1 : 2;
109 // !checked later through tabsel_123[]!
110 // if(!bitrate_index || bitrate_index==15){
111 // mp_msg(MSGT_DEMUXER,MSGL_DBG2,"Free format not supported.\n");
112 // return -1;
113 // }
115 if(lsf)
116 ssize = (stereo == 1) ? 9 : 17;
117 else
118 ssize = (stereo == 1) ? 17 : 32;
119 if(!((newhead>>16)&0x1)) ssize += 2; // CRC
121 bitrate = tabsel_123[lsf][layer-1][bitrate_index];
122 framesize = bitrate * mult[layer-1];
124 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"FRAMESIZE: %d, layer: %d, bitrate: %d, mult: %d\n",
125 framesize, layer, tabsel_123[lsf][layer-1][bitrate_index], mult[layer-1]);
126 if(!framesize){
127 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid framesize/bitrate_index\n");
128 return -1;
131 divisor = (layer == 3 ? (freqs[sampling_frequency] << lsf) : freqs[sampling_frequency]);
132 framesize /= divisor;
133 if(layer==1)
134 framesize = (framesize+padding)*4;
135 else
136 framesize += padding;
138 // if(framesize<=0 || framesize>MAXFRAMESIZE) return FALSE;
139 if(srate) {
140 *srate = freqs[sampling_frequency];
141 if(spf) {
142 if(layer == 1)
143 *spf = 384;
144 else if(layer == 2)
145 *spf = 1152;
146 else if(*srate < 32000)
147 *spf = 576;
148 else
149 *spf = 1152;
152 if(mpa_layer) *mpa_layer = layer;
153 if(chans) *chans = stereo;
154 if(br) *br = bitrate;
156 return framesize;