forked from mvaneerde/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpegaudioheader.pl
299 lines (268 loc) · 6.64 KB
/
mpegaudioheader.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use strict;
# based on http://www.mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm
# assumes that the file you point it at starts with an MPEG audio header
unless (1 == @ARGV and $ARGV[0] ne "/?" and $ARGV[0] ne "-?") {
print "USAGE: perl $0 mpeg-audio-file.mpeg";
exit(0);
};
my %version = (
"00" => "MPEG Version 2.5 (unofficial)",
"01" => "reserved",
"10" => "MPEG version 2 (ISO/IEC 13818-3)",
"11" => "MPEG version 1 (ISO/IEC 11172-3)",
);
my %layer = (
"00" => "reserved",
"01" => "layer III",
"10" => "layer II",
"11" => "layer I",
);
my %protection = (
"0" => "protected by CRC (16-bit CRC follows header)",
"1" => "not protected",
);
my %bitrate = (
# version 1
"11" => {
# layer 1
"11" => {
"0000" => "free",
"0001" => "32 kbps",
"0010" => "64 kbps",
"0011" => "96 kbps",
"0100" => "128 kbps",
"0101" => "160 kbps",
"0110" => "192 kbps",
"0111" => "224 kbps",
"1000" => "256 kbps",
"1001" => "288 kbps",
"1010" => "320 kbps",
"1011" => "352 kbps",
"1100" => "384 kbps",
"1101" => "416 kbps",
"1110" => "448 kbps",
"1111" => "bad",
},
# layer 2
"10" => {
"0000" => "free",
"0001" => "32 kbps",
"0010" => "48 kbps",
"0011" => "56 kbps",
"0100" => "64 kbps",
"0101" => "80 kbps",
"0110" => "96 kbps",
"0111" => "112 kbps",
"1000" => "128 kbps",
"1001" => "160 kbps",
"1010" => "192 kbps",
"1011" => "224 kbps",
"1100" => "256 kbps",
"1101" => "320 kbps",
"1110" => "384 kbps",
"1111" => "bad",
},
# layer 3
"01" => {
"0000" => "free",
"0001" => "32 kbps",
"0010" => "40 kbps",
"0011" => "48 kbps",
"0100" => "56 kbps",
"0101" => "64 kbps",
"0110" => "80 kbps",
"0111" => "96 kbps",
"1000" => "112 kbps",
"1001" => "128 kbps",
"1010" => "160 kbps",
"1011" => "192 kbps",
"1100" => "224 kbps",
"1101" => "256 kbps",
"1110" => "320 kbps",
"1111" => "bad",
},
},
# version 2
"10" => {
# layer 1
"11" => {
"0000" => "free",
"0001" => "32 kbps",
"0010" => "48 kbps",
"0011" => "56 kbps",
"0100" => "64 kbps",
"0101" => "80 kbps",
"0110" => "96 kbps",
"0111" => "112 kbps",
"1000" => "128 kbps",
"1001" => "144 kbps",
"1010" => "160 kbps",
"1011" => "176 kbps",
"1100" => "192 kbps",
"1101" => "224 kbps",
"1110" => "256 kbps",
"1111" => "bad",
},
# layer 2
"10" => {
"0000" => "free",
"0001" => "8 kbps",
"0010" => "16 kbps",
"0011" => "24 kbps",
"0100" => "32 kbps",
"0101" => "40 kbps",
"0110" => "48 kbps",
"0111" => "56 kbps",
"1000" => "64 kbps",
"1001" => "80 kbps",
"1010" => "96 kbps",
"1011" => "112 kbps",
"1100" => "128 kbps",
"1101" => "144 kbps",
"1110" => "160 kbps",
"1111" => "bad",
},
# layer 3
"01" => {
"0000" => "free",
"0001" => "8 kbps",
"0010" => "16 kbps",
"0011" => "24 kbps",
"0100" => "32 kbps",
"0101" => "40 kbps",
"0110" => "48 kbps",
"0111" => "56 kbps",
"1000" => "64 kbps",
"1001" => "80 kbps",
"1010" => "96 kbps",
"1011" => "112 kbps",
"1100" => "128 kbps",
"1101" => "144 kbps",
"1110" => "160 kbps",
"1111" => "bad",
},
},
);
my %samplerate = (
# version 1
"11" => {
"00" => "44100 Hz",
"01" => "48000 Hz",
"10" => "32000 Hz",
"11" => "reserved",
},
# version 2
"10" => {
"00" => "22050 Hz",
"01" => "24000 Hz",
"10" => "16000 Hz",
"11" => "reserved",
},
# version 2.5 (unofficial)
"00" => {
"00" => "11025 Hz",
"01" => "12000 Hz",
"10" => "8000 Hz",
"11" => "reserved",
},
);
my %padding = (
"0" => "frame is not padded",
"1" => "frame is padded with one extra slot",
);
my %channelmode = (
"00" => "stereo",
"01" => "joint stereo (stereo)",
"10" => "dual channel (stereo)",
"11" => "single channel (mono)",
);
my %modeextension = (
# layer I
"11" => {
"00" => "bands 4 to 31",
"01" => "bands 8 to 31",
"10" => "bands 12 to 31",
"11" => "bands 16 to 31",
},
# layer II
"10" => {
"00" => "bands 4 to 31",
"01" => "bands 8 to 31",
"10" => "bands 12 to 31",
"11" => "bands 16 to 31",
},
# layer III
"01" => {
"00" => "intensity stereo off; m/s stereo off",
"01" => "intensity stereo on; m/s stereo off",
"10" => "intensity stereo off; m/s stereo on",
"11" => "intensity stereo on; m/s stereo on",
},
);
my %copyright = (
"0" => "audio is not copyrighted",
"1" => "audio is copyrighted",
);
my %original = (
"0" => "copy of original media",
"1" => "original media",
);
my %emphasis = (
"00" => "none",
"01" => "50/15 microseconds", # the source incorrectly says "ms" which is milliseconds
"10" => "reserved",
"11" => "CCIT J.17",
);
open(MPEG, "<", $ARGV[0]) or die("Could not open $ARGV[0]: $!");
binmode(MPEG) or die("Could not set file handle to binary mode: $!"); # binary file
my $header = "";
my $header_size = 16;
my $read = read(MPEG, $header, $header_size, 0);
close(MPEG);
$header_size == $read or die("Expected $header_size bytes to be read, not $read");
my @bits = ();
for my $byte (map { ord( $_ ) } split (//, $header)) {
for my $bit (1 .. 8) {
push @bits, (($byte & (1 << (8 - $bit))) ? 1 : 0);
}
}
unless ("1" x 11 eq join("", @bits[0 .. 10])) {
printf("WARNING: the frame header is not all ones. This is not a valid MPEG audio header.\n");
# carry on regardless
}
printf(
"Frame header: %s %s (%s)\n" .
"MPEG Audio version ID: %s (%s)\n" .
"Layer description: %s (%s)\n" .
"Protection bit: %s (%s)\n" .
"Bitrate index: %s (%s)\n" .
"Sample rate index: %s (%s)\n" .
"Padding bit: %s (%s)\n" .
"Private bit: %s (%s)\n" .
"Channel mode: %s (%s)\n" .
"Mode extension (if channel mode is joint stereo:) %s (%s)\n" .
"Copyright: %s (%s)\n" .
"Original: %s (%s)\n" .
"Emphasis: %s (%s)\n" .
""
,
join("", @bits[0 .. 7]), join("", @bits[8 .. 10]), "should be all ones",
join("", @bits[11 .. 12]), $version{ join("", @bits[11 .. 12]) },
join("", @bits[13 .. 14]), $layer{ join("", @bits[13 .. 14]) },
$bits[15], $protection{ $bits[15] },
join("", @bits[16 .. 19]),
# bit rate depends on version, layer, and bitrate index
$bitrate{ join("", @bits[11 .. 12]) }{ join("", @bits[13 .. 14]) }{ join("", @bits[16 .. 19]) },
join("", @bits[20 .. 21]),
# sample rate depends on version
$samplerate{ join("", @bits[11 .. 12]) }{ join("", @bits[20 .. 21]) },
$bits[22], $padding{ $bits[22] },
$bits[23], "application specific",
join("", @bits[24 .. 25]), $channelmode{ join("", @bits[24 .. 25]) },
join("", @bits[26 .. 27]),
# mode extension depends on layer
$modeextension{ join("", @bits[13 .. 14]) }{ join("", @bits[26 .. 27]) },
$bits[28], $copyright{ $bits[28] },
$bits[29], $original{ $bits[29] },
join("", @bits[30 .. 31]), $emphasis{ join("", @bits[30 .. 31]) },
);