Mercurial > mplayer.hg
annotate libmpdemux/mp3_hdr.c @ 30589:df6c41f16b40
Add header for AVI print functions; avoids many forward declarations.
author | diego |
---|---|
date | Thu, 18 Feb 2010 00:18:50 +0000 |
parents | 1cb4281551e2 |
children | 32725ca88fed |
rev | line source |
---|---|
29238
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
1 /* |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
2 * This file is part of MPlayer. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
3 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
4 * MPlayer is free software; you can redistribute it and/or modify |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
7 * (at your option) any later version. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
8 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
9 * MPlayer is distributed in the hope that it will be useful, |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
12 * GNU General Public License for more details. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
13 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
14 * You should have received a copy of the GNU General Public License along |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
17 */ |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
17012
diff
changeset
|
18 |
2588 | 19 #include <stdio.h> |
20 | |
21 #include "config.h" | |
17012 | 22 #include "mp_msg.h" |
30554 | 23 #include "mp3_hdr.h" |
2588 | 24 |
25 //----------------------- mp3 audio frame header parser ----------------------- | |
26 | |
27 static int tabsel_123[2][3][16] = { | |
7910
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
28 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0}, |
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
29 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0}, |
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
30 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0} }, |
2588 | 31 |
7910
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
32 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0}, |
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
33 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0}, |
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
34 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0} } |
2588 | 35 }; |
10369 | 36 |
37 static long freqs[9] = { 44100, 48000, 32000, // MPEG 1.0 | |
38 22050, 24000, 16000, // MPEG 2.0 | |
10370 | 39 11025, 12000, 8000}; // MPEG 2.5 |
2588 | 40 |
30572
292ab27ee1fa
Mark mp_mp3_get_lsf() as static; it is not used outside of the file.
diego
parents:
30554
diff
changeset
|
41 static int mp_mp3_get_lsf(unsigned char* hbuf){ |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
42 unsigned long newhead = |
5806 | 43 hbuf[0] << 24 | |
44 hbuf[1] << 16 | | |
45 hbuf[2] << 8 | | |
46 hbuf[3]; | |
47 if( newhead & ((long)1<<20) ) { | |
48 return (newhead & ((long)1<<19)) ? 0x0 : 0x1; | |
49 } | |
50 return 1; | |
51 } | |
52 | |
2588 | 53 /* |
54 * return frame size or -1 (bad frame) | |
55 */ | |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15251
diff
changeset
|
56 int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate, int* spf, int* mpa_layer, int* br){ |
15251
c39173a67cbb
wrong framesize calculation for layers 1 and 2 with lsf set
nicodvb
parents:
15199
diff
changeset
|
57 int stereo,ssize,lsf,framesize,padding,bitrate_index,sampling_frequency, divisor; |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15251
diff
changeset
|
58 int bitrate; |
15039 | 59 int layer, mult[3] = { 12000, 144000, 144000 }; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
60 unsigned long newhead = |
2588 | 61 hbuf[0] << 24 | |
62 hbuf[1] << 16 | | |
63 hbuf[2] << 8 | | |
64 hbuf[3]; | |
65 | |
66 // printf("head=0x%08X\n",newhead); | |
67 | |
68 // head_check: | |
10369 | 69 if( (newhead & 0xffe00000) != 0xffe00000 ){ |
4694
a21735031d6a
Audio file demuxer. Extended version for demuxer info.
albeu
parents:
2589
diff
changeset
|
70 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"head_check failed\n"); |
2588 | 71 return -1; |
72 } | |
73 | |
15039 | 74 layer = 4-((newhead>>17)&3); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
75 if(layer==4){ |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
76 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"not layer-1/2/3\n"); |
4694
a21735031d6a
Audio file demuxer. Extended version for demuxer info.
albeu
parents:
2589
diff
changeset
|
77 return -1; |
a21735031d6a
Audio file demuxer. Extended version for demuxer info.
albeu
parents:
2589
diff
changeset
|
78 } |
2588 | 79 |
10370 | 80 sampling_frequency = ((newhead>>10)&0x3); // valid: 0..2 |
10369 | 81 if(sampling_frequency==3){ |
4694
a21735031d6a
Audio file demuxer. Extended version for demuxer info.
albeu
parents:
2589
diff
changeset
|
82 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid sampling_frequency\n"); |
10370 | 83 return -1; |
2588 | 84 } |
85 | |
10369 | 86 if( newhead & ((long)1<<20) ) { |
87 // MPEG 1.0 (lsf==0) or MPEG 2.0 (lsf==1) | |
88 lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1; | |
89 sampling_frequency += (lsf*3); | |
90 } else { | |
91 // MPEG 2.5 | |
92 lsf = 1; | |
93 sampling_frequency += 6; | |
94 } | |
95 | |
96 // crc = ((newhead>>16)&0x1)^0x1; | |
10370 | 97 bitrate_index = ((newhead>>12)&0xf); // valid: 1..14 |
2588 | 98 padding = ((newhead>>9)&0x1); |
99 // fr->extension = ((newhead>>8)&0x1); | |
100 // fr->mode = ((newhead>>6)&0x3); | |
101 // fr->mode_ext = ((newhead>>4)&0x3); | |
102 // fr->copyright = ((newhead>>3)&0x1); | |
103 // fr->original = ((newhead>>2)&0x1); | |
104 // fr->emphasis = newhead & 0x3; | |
105 | |
106 stereo = ( (((newhead>>6)&0x3)) == 3) ? 1 : 2; | |
107 | |
10369 | 108 // !checked later through tabsel_123[]! |
109 // if(!bitrate_index || bitrate_index==15){ | |
110 // mp_msg(MSGT_DEMUXER,MSGL_DBG2,"Free format not supported.\n"); | |
111 // return -1; | |
112 // } | |
2588 | 113 |
114 if(lsf) | |
115 ssize = (stereo == 1) ? 9 : 17; | |
116 else | |
117 ssize = (stereo == 1) ? 17 : 32; | |
10369 | 118 if(!((newhead>>16)&0x1)) ssize += 2; // CRC |
2588 | 119 |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15251
diff
changeset
|
120 bitrate = tabsel_123[lsf][layer-1][bitrate_index]; |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15251
diff
changeset
|
121 framesize = bitrate * mult[layer-1]; |
7910
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
122 |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
123 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"FRAMESIZE: %d, layer: %d, bitrate: %d, mult: %d\n", |
15039 | 124 framesize, layer, tabsel_123[lsf][layer-1][bitrate_index], mult[layer-1]); |
7910
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
125 if(!framesize){ |
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
126 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid framesize/bitrate_index\n"); |
10370 | 127 return -1; |
7910
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
128 } |
5a276890cec4
check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents:
6763
diff
changeset
|
129 |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
130 divisor = (layer == 3 ? (freqs[sampling_frequency] << lsf) : freqs[sampling_frequency]); |
15251
c39173a67cbb
wrong framesize calculation for layers 1 and 2 with lsf set
nicodvb
parents:
15199
diff
changeset
|
131 framesize /= divisor; |
15039 | 132 if(layer==1) |
133 framesize = (framesize+padding)*4; | |
134 else | |
135 framesize += padding; | |
2588 | 136 |
137 // if(framesize<=0 || framesize>MAXFRAMESIZE) return FALSE; | |
15199
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
138 if(srate) { |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
139 *srate = freqs[sampling_frequency]; |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
140 if(spf) { |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
141 if(layer == 1) |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
142 *spf = 384; |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
143 else if(layer == 2) |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
144 *spf = 1152; |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
145 else if(*srate < 32000) |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
146 *spf = 576; |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
147 else |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
148 *spf = 1152; |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
149 } |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
150 } |
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
15039
diff
changeset
|
151 if(mpa_layer) *mpa_layer = layer; |
6763 | 152 if(chans) *chans = stereo; |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15251
diff
changeset
|
153 if(br) *br = bitrate; |
2588 | 154 |
155 return framesize; | |
156 } | |
157 |