Version:
~ [ 10.0 ] ~
** Warning: Cannot open xref database.
1
2 /* $Id: filemp3.c,v 1.3 2005/01/11 22:48:05 andreradke Exp $ */
3
4 /******************************************************************************
5
6 UserLand Frontier(tm) -- High performance Web content management,
7 object database, system-level and Internet scripting environment,
8 including source code editing and debugging.
9
10 Copyright (C) 1992-2004 UserLand Software, Inc.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26 ******************************************************************************/
27
28 /*
29 MPEG Audio Layer I/II/III frame header
30
31 There is no main file header in an MPEG audio file. An MPEG
32 audio file is built up from a succession of smaller parts called
33 frames. A frame is a datablock with its own header and audio
34 information.
35
36 In the case of Layer I or Layer II, frames are some totally
37 independent items, so you can cut any part of MPEG file and play
38 it correctly. The player will then play the music starting to
39 the first plain valid frame founded. However, in the case of
40 Layer III, frames are not always independant. Due to the possible
41 use of the "byte reservoir", wich is a kind of buffer, frames
42 are often dependent of each other. In the worst case, 9 frames
43 may be needed before beeing able to decode one frame.
44
45 When you want to read info about an MPEG audio file, it is usually
46 enough to find the first frame, read its header and assume that
47 the other frames are the same. But this is not always the case,
48 as variable bitrate (VBR) files may be encountered. In a VBR
49 file, the bitrate can be changed in each frame. It can be used,
50 as an exemple to keep a constant sound quality during the whole
51 file, by using more bits where the music need more to be
52 encoded.
53
54 The frame header is 32 bits (4 bytes) length. The first eleven
55 bits (or first twelve bits in the case of the MPEG 2.5 extension) of
56 a frame header are always set to 1 and are called "frame sync".
57
58 Frames may have an optional CRC checksum. It is 16 bits long and, if
59 it exists, follows the frame header. After the CRC comes the
60 audio data. By re-calculating the CRC and comparing its value to
61 the sored one, you can check if the frame has been altered during
62 transmission of the bitstream.
63
64 Here is a presentation of the frame header content. Characters A
65 to M are used to indicate different fields. In the table below,
66 you can see details about the content of each field.
67
68 AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
69
70
71 Sign Length (bits) Position (bits) Description
72
73 A 11 (31-21) Frame sync (all bits must be set)
74
75 B 2 (20,19) MPEG Audio version ID
76
77 00 - MPEG Version 2.5 (later extension of MPEG 2)
78 01 - reserved
79 10 - MPEG Version 2 (ISO/IEC 13818-3)
80 11 - MPEG Version 1 (ISO/IEC 11172-3)
81
82 Note: MPEG Version 2.5 was added lately to the MPEG 2 standard.
83 It is an extension used for very low bitrate files, allowing the
84 use of lower sampling frequencies. If your decoder does not
85 support this extension, it is recommended for you to use 12 bits
86 for synchronization instead of 11 bits.
87
88 C 2 (18,17) Layer description
89
90 00 - reserved
91 01 - Layer III
92 10 - Layer II
93 11 - Layer I
94
95 D 1 (16) Protection bit
96
97 0 - Protected by CRC (16bit CRC follows header)
98 1 - Not protected
99
100 E 4 (15,12) Bitrate index
101
102 bits V1,L1 V1,L2 V1,L3 V2,L1 V2, L2 & L3
103
104 0000 free free free free free
105 0001 32 32 32 32 8
106 0010 64 48 40 48 16
107 0011 96 56 48 56 24
108 0100 128 64 56 64 32
109 0101 160 80 64 80 40
110 0110 192 96 80 96 48
111 0111 224 112 96 112 56
112 1000 256 128 112 128 64
113 1001 288 160 128 144 80
114 1010 320 192 160 160 96
115 1011 352 224 192 176 112
116 1100 384 256 224 192 128
117 1101 416 320 256 224 144
118 1110 448 384 320 256 160
119 1111 bad bad bad bad bad
120
121 NOTES: All values are in kbps
122 V1 - MPEG Version 1
123 V2 - MPEG Version 2 and Version 2.5
124 L1 - Layer I
125 L2 - Layer II
126 L3 - Layer III
127
128 "free" means free format. The free bitrate must remain constant,
129 an must be lower than the maximum allowed bitrate. Decoders are
130 not required to support decoding of free bitrate streams.
131
132 "bad" means that the value is unallowed.
133
134 MPEG files may feature variable bitrate (VBR). Each frame may then
135 be created with a different bitrate. It may be used in all layers.
136 Layer III decoders must support this method.
137 Layer I & II decoders may support it.
138
139 For Layer II there are some combinations of bitrate and mode
140 which are not allowed. Here is a list of allowed combinations.
141
142 bitrate single channel stereo intensity stereo dual channel
143
144 free yes yes yes yes
145 32 yes no no no
146 48 yes no no no
147 56 yes no no no
148 64 yes yes yes yes
149 80 yes no no no
150 96 yes yes yes yes
151 112 yes yes yes yes
152 128 yes yes yes yes
153 160 yes yes yes yes
154 192 yes yes yes yes
155 224 no yes yes yes
156 256 no yes yes yes
157 320 no yes yes yes
158 384 no yes yes yes
159
160 F 2 (11,10) Sampling rate frequency index
161
162 bits MPEG1 MPEG2 MPEG2.5
163
164 00 44100 Hz 22050 Hz 11025 Hz
165 01 48000 Hz 24000 Hz 12000 Hz
166 10 32000 Hz 16000 Hz 8000 Hz
167 11 reserv. reserv. reserv.
168
169 G 1 (9) Padding bit
170
171 0 - frame is not padded
172 1 - frame is padded with one extra slot
173
174 Padding is used to exactly fit the bitrate.
175 As an example: 128kbps 44.1kHz layer II uses a lot of 418 bytes
176 and some of 417 bytes long frames to get the exact 128k bitrate.
177 For Layer I slot is 32 bits long, for Layer II and Layer III slot is 8 bits long.
178
179 H 1 (8) Private bit. This one is only informative.
180
181 I 2 (7,6) Channel Mode
182
183 00 - Stereo
184 01 - Joint stereo (Stereo)
185 10 - Dual channel (2 mono channels)
186 11 - Single channel (Mono)
187
188 Note: Dual channel files are made of two independant mono channel.
189 Each one uses exactly half the bitrate of the file. Most decoders
190 output them as stereo, but it might not always be the case.
191 One example of use would be some speech in two different languages
192 carried in the same bitstream, and then an appropriate decoder would
193 decode only the choosen language.
194
195 J 2 (5,4) Mode extension (Only used in Joint stereo)
196
197 Mode extension is used to join informations that are of no use
198 for stereo effect, thus reducing needed bits. These bits are dynamically
199 determined by an encoder in Joint stereo mode, and Joint Stereo can be
200 changed from one frame to another, or even switched on or off.
201
202 Complete frequency range of MPEG file is divided in subbands.
203 There are 32 subbands. For Layer I & II these two bits determine
204 frequency range (bands) where intensity stereo is applied.
205 For Layer III these two bits determine which type of joint stereo
206 is used (intensity stereo or m/s stereo).
207 Frequency range is determined within decompression algorithm.
208
209 Layer III
210 value Layer I & II Intensity stereo MS stereo
211
212 00 bands 4 to 31 off off
213 01 bands 8 to 31 on off
214 10 bands 12 to 31 off on
215 11 bands 16 to 31 on on
216
217 K 1 (3) Copyright
218
219 0 - Audio is not copyrighted
220 1 - Audio is copyrighted
221
222 The copyright has the same meaning as the copyright bit on CDs and DAT tapes,
223 i.e. telling that it is illegal to copy the contents if the bit is set.
224
225 L 1 (2) Original
226
227 0 - Copy of original media
228 1 - Original media
229
230 The original bit indicates, if it is set, that the frame is located
231 on its original media.
232
233 M 2 (1,0) Emphasis
234
235 00 - none
236 01 - 50/15 ms
237 10 - reserved
238 11 - CCIT J.17
239
240 The emphasis indication is here to tell the decoder that the file
241 must be de-emphasized, ie the decoder must 're-equalize' the sound
242 after a Dolby-like noise supression. It is rarely used.
243
244 (c) 1999-2000 Gabriel Bouvigne for MP3'Tech - www.mp3-tech.org
245
246 Source: http://www.mp3-tech.org/programmer/frame_header.html
247 */
248
249
250 #include "frontier.h"
251 #include "standard.h"
252
253 #include "file.h"
254 #include "shell.h"
255
256
257 typedef struct {
258 long version;
259 long layer;
260 long bitrate;
261 long frequency;
262 long channelmode;
263 long modeextension;
264 long emphasis;
265 boolean flchecksum;
266 boolean flpadding;
267 boolean flprivate;
268 boolean flcopyright;
269 boolean floriginal;
270 } tympeginfostruct;
271
272
273 static const long mpegsamplingfrequency[3][4] = {
274 {44100, 48000, 32000, 0}, // MPEG 1
275 {22050, 24000, 16000, 0}, // MPEG 2
276 {11025, 12000, 8000, 0} // MPEG 2.5
277 };
278
279
280 static const long mpegbitrate[2][3][16] = {
281 { // MPEG 1
282 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0}, // Layer 1
283 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0}, // Layer 2
284 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0} // Layer 3
285 },{ // MPEG 2
286 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0}, // Layer 1
287 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}, // Layer 2
288 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0} // Layer 3
289 }
290 };
291
292
293 static boolean decodempegheader (unsigned long header, tympeginfostruct *info) {
294
295 if ((header >> 20) != 0x00000fff) // bits 20-31 must be set, we don't recognize MPEG 2.5
296 return (false);
297
298 info->version = 2 - ((header >> 19) & 0x00000001); // 1 = MPEG1, 2 = MPEG2
299
300 info->layer = 4 - ((header >> 17) & 0x00000003); // 1 = Layer I, 2 = Layer II, 3 = Layer III
301
302 info->bitrate = mpegbitrate [info->version - 1] [info->layer - 1] [(header >> 12) & 0x0000000f];
303
304 info->frequency = mpegsamplingfrequency [info->version - 1] [(header >> 10) & 0x00000003];
305
306 info->channelmode = (header >> 6) & 0x00000003;
307
308 info->modeextension = (header >> 4) & 0x00000003;
309
310 info->emphasis = header & 0x00000003;
311
312 info->flchecksum = ((header >> 16) & 0x00000001) ? false : true;
313
314 info->flpadding = ((header >> 9) & 0x00000001) ? true : false;
315
316 info->flprivate = ((header >> 8) & 0x00000001) ? true : false;
317
318 info->flcopyright = ((header >> 3) & 0x00000001) ? true : false;
319
320 info->floriginal = ((header >> 2) & 0x00000001) ? true : false;
321
322 return (true);
323 }/*decodempegheader*/
324
325
326 static long extractfourbytes (Handle h, long ix) {
327
328 long n;
329
330 n = (unsigned char) (*h)[ix++];
331
332 n <<= 8;
333
334 n |= (unsigned char) (*h)[ix++];
335
336 n <<= 8;
337
338 n |= (unsigned char) (*h)[ix++];
339
340 n <<= 8;
341
342 n |= (unsigned char) (*h)[ix++];
343
344 return (n);
345 }/*extractfourbytes*/
346
347
348 static boolean getxingheader (tympeginfostruct *info, Handle h, long ix, long *ctframes, long *ctbytes) {
349
350 long flags;
351
352 ix += 32; //MPEG 1, Layer 3
353
354 if ((*h)[ix++] != 'X')
355 return (false);
356
357 if ((*h)[ix++] != 'i')
358 return (false);
359
360 if ((*h)[ix++] != 'n')
361 return (false);
362
363 if ((*h)[ix++] != 'g')
364 return (false);
365
366 flags = extractfourbytes (h, ix);
367
368 ix += sizeof(long);
369
370 if (flags & 0x00000001 == 0) //check for frame count
371 return (false);
372
373 *ctframes = extractfourbytes (h, ix);
374
375 ix += sizeof(long);
376
377 if (flags & 0x00000002) { //check for byte count
378 *ctbytes = extractfourbytes (h, ix);
379 ix += sizeof(long);
380 }
381
382 return (true);
383 }/*getxingheader*/
384
385
386 boolean getmp3info (const tyfilespec *fs, long *seconds, long *bitrate, long *frequency, long *offset, boolean *flvariablebitrate) {
387
388 tympeginfostruct mp3;
389 long ix = 0;
390 long oldpos, header, ctbytes, ctframes;
391 Handle h = nil;
392 unsigned char c1, c2;
393 const long buffersize = 32767;
394
395 if (!fifgetposition (fs, &oldpos))
396 return (false);
397
398 if (!fifsetposition (fs, 0))
399 return (false);
400
401 if (!fifreadhandle (fs, buffersize, &h)) //read the first 32k of data
402 return (false);
403
404 for (ix = 0; ix < buffersize - (long) sizeof (header) - 1; ix++) {
405
406 c1 = (unsigned char) (*h)[ix];
407
408 if (c1 == 0xff) {
409
410 c2 = (unsigned char) (*h)[ix+1];
411
412 c2 >>= 4;
413
414 if (c2 == 0x0f) {
415 header = extractfourbytes (h, ix);
416 break;
417 }
418 }
419 }
420
421 if (decodempegheader (header, &mp3) && mp3.version == 1 && mp3.layer == 3) {
422
423 if (!fifgetendoffile (fs, &ctbytes))
424 goto exit;
425
426 ctbytes -= ix;
427
428 *offset = ix;
429
430 *frequency = mp3.frequency;
431
432 *seconds = (mp3.bitrate != 0) ? (ctbytes / (125 * mp3.bitrate)) : 0;
433
434 *bitrate = mp3.bitrate;
435
436 *flvariablebitrate = getxingheader (&mp3, h, ix + sizeof (header), &ctframes, &ctbytes);
437
438 if (*flvariablebitrate) {
439 *seconds = (ctframes * 26) / 1000;
440 *bitrate = (*seconds) ? ((ctbytes) / (*seconds * 125)) : 0;
441 }
442 }
443 else {
444
445 shellerrormessage ("\x4b" "Can't get the MP3 file info because an MP3 frame header could not be found.");
446
447 goto exit;
448 }
449
450 if (!fifsetposition (fs, oldpos))
451 goto exit;
452
453 return (true);
454
455 exit:
456
457 fifsetposition (fs, oldpos);
458
459 return (false);
460 }/*getmp3info*/
461
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.