annotate libmpdemux/mp3_hdr.c @ 16513:68890ac57391

Reflect recent changes to the the pan audio filter syntax and behavior in the documentation, patch by Reimar Doeffinger with some fixes by me.
author diego
date Sun, 18 Sep 2005 11:40:06 +0000
parents b5c2254d13f8
children 6ff3379a0862
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"
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
4 #include "../mp_msg.h"
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
5
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
6 //----------------------- mp3 audio frame header parser -----------------------
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
7
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
8 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
9 { {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
10 {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
11 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0} },
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
12
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
13 { {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
14 {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
15 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0} }
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
16 };
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
17
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
18 static long freqs[9] = { 44100, 48000, 32000, // MPEG 1.0
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
19 22050, 24000, 16000, // MPEG 2.0
10370
2cfd7ef9cb75 codmetics (noticed by Alex)
arpi
parents: 10369
diff changeset
20 11025, 12000, 8000}; // MPEG 2.5
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
21
5806
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
22 int mp_mp3_get_lsf(unsigned char* hbuf){
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
23 unsigned long newhead =
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
24 hbuf[0] << 24 |
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
25 hbuf[1] << 16 |
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
26 hbuf[2] << 8 |
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
27 hbuf[3];
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
28 if( newhead & ((long)1<<20) ) {
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
29 return (newhead & ((long)1<<19)) ? 0x0 : 0x1;
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
30 }
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
31 return 1;
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
32 }
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
33
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
34 /*
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
35 * return frame size or -1 (bad frame)
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
36 */
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15251
diff changeset
37 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
38 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
39 int bitrate;
15039
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
40 int layer, mult[3] = { 12000, 144000, 144000 };
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
41 unsigned long newhead =
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
42 hbuf[0] << 24 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
43 hbuf[1] << 16 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
44 hbuf[2] << 8 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
45 hbuf[3];
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
46
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
47 // printf("head=0x%08X\n",newhead);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
48
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
49 #if 1
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
50 // head_check:
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
51 if( (newhead & 0xffe00000) != 0xffe00000 ){
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
52 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"head_check failed\n");
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
53 return -1;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
54 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
55 #endif
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
56
15039
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
57 layer = 4-((newhead>>17)&3);
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
58 if(layer==4){
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
59 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
60 return -1;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
61 }
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
62
10370
2cfd7ef9cb75 codmetics (noticed by Alex)
arpi
parents: 10369
diff changeset
63 sampling_frequency = ((newhead>>10)&0x3); // valid: 0..2
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
64 if(sampling_frequency==3){
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
65 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid sampling_frequency\n");
10370
2cfd7ef9cb75 codmetics (noticed by Alex)
arpi
parents: 10369
diff changeset
66 return -1;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
67 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
68
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
69 if( newhead & ((long)1<<20) ) {
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
70 // MPEG 1.0 (lsf==0) or MPEG 2.0 (lsf==1)
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
71 lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
72 sampling_frequency += (lsf*3);
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
73 } else {
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
74 // MPEG 2.5
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
75 lsf = 1;
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
76 sampling_frequency += 6;
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
77 }
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
78
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
79 // crc = ((newhead>>16)&0x1)^0x1;
10370
2cfd7ef9cb75 codmetics (noticed by Alex)
arpi
parents: 10369
diff changeset
80 bitrate_index = ((newhead>>12)&0xf); // valid: 1..14
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
81 padding = ((newhead>>9)&0x1);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
82 // fr->extension = ((newhead>>8)&0x1);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
83 // fr->mode = ((newhead>>6)&0x3);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
84 // fr->mode_ext = ((newhead>>4)&0x3);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
85 // fr->copyright = ((newhead>>3)&0x1);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
86 // fr->original = ((newhead>>2)&0x1);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
87 // fr->emphasis = newhead & 0x3;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
88
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
89 stereo = ( (((newhead>>6)&0x3)) == 3) ? 1 : 2;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
90
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
91 // !checked later through tabsel_123[]!
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
92 // if(!bitrate_index || bitrate_index==15){
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
93 // mp_msg(MSGT_DEMUXER,MSGL_DBG2,"Free format not supported.\n");
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
94 // return -1;
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
95 // }
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
96
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
97 if(lsf)
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
98 ssize = (stereo == 1) ? 9 : 17;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
99 else
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
100 ssize = (stereo == 1) ? 17 : 32;
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
101 if(!((newhead>>16)&0x1)) ssize += 2; // CRC
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
102
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15251
diff changeset
103 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
104 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
105
15039
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
106 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"FRAMESIZE: %d, layer: %d, bitrate: %d, mult: %d\n",
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
107 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
108 if(!framesize){
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
109 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid framesize/bitrate_index\n");
10370
2cfd7ef9cb75 codmetics (noticed by Alex)
arpi
parents: 10369
diff changeset
110 return -1;
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
111 }
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
112
15251
c39173a67cbb wrong framesize calculation for layers 1 and 2 with lsf set
nicodvb
parents: 15199
diff changeset
113 divisor = (layer == 3 ? (freqs[sampling_frequency] << lsf) : freqs[sampling_frequency]);
c39173a67cbb wrong framesize calculation for layers 1 and 2 with lsf set
nicodvb
parents: 15199
diff changeset
114 framesize /= divisor;
15039
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
115 if(layer==1)
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
116 framesize = (framesize+padding)*4;
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
117 else
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
118 framesize += padding;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
119
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
120 // 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
121 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
122 *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
123 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
124 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
125 *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
126 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
127 *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
128 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
129 *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
130 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
131 *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
132 }
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
133 }
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
134 if(mpa_layer) *mpa_layer = layer;
6763
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5806
diff changeset
135 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
136 if(br) *br = bitrate;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
137
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
138 return framesize;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
139 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
140