Debian Codegraph @

external/mp3/mp3.cpp

raw
1 /*
2 * Praat wrappers for libMAD (MPEG Audio Decoder) Copyright 2007 Erez Volk
3 *
4 * This code 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 (at
7 * your option) any later version.
8 *
9 * This code is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this work. If not, see <http://www.gnu.org/licenses/>.
16 *
17 *
18 *
19 * Exact seeking in an MP3 file turns out to be quite complex.
20 *
21 * For constant bit rate (CBR) files, frame size is fixed and we can calculate
22 * in advance the location of every frame.
23 *
24 * For variable bit rate (VBR) files, no option gives us more than a rough
25 * estimate of where every percent of the file is lcoated, with no way of
26 * checking (after we make a seek) where we ended up. The only reliable way we
27 * have, then, is to scan the entire file in advance and keep a table with the
28 * offsets of all (or some) frames. In the worst case, we will have to scan
29 * the file *twice*, since we can't know the exact number of frames.
30 *
31 * What's worse, there is no guaranteed standard way of checking whether an MP3
32 * file is CBR or VBR. We therefore use the following compromise, which is
33 * guaranteed to work on all files:
34 *
35 * - If there is a Xing header, read it to get the number of frames.
36 * - Otherwise *estimate* the number of frames.
37 * - Keep a reasonably (?) sized table of "key" offsets.
38 * - Scan all the headers and keep required offsets in the table.
39 * - After the scan, we also know the precise number of frames and samples.
40 *
41 * TODO: Find exactly what the encoder delay is.
42 * (see http://mp3decoders.mp3-tech.org/decoders_lame.html)
43 * TODO: Compensate for end padding.
44 * TODO: Better conversion to short.
45 *
46 */
47
48 /*#define MP3_DEBUG*/
49
50 #include "mp3.h"
51
52 extern "C" {
53 #include "mad_config.h"
54 #include "mad_decoder.h"
55 }
56
57 #define MP3F_BUFFER_SIZE (8 * 1024)
58 #define MP3F_MAX_LOCATIONS 1024
59
60 /*
61 * MP3 encoders and decoders add a number of silent samples at the beginning.
62 * I don't know of any reliable way to detect these delays in an MP3 file.
63 * The numbers below are (hopefully reasonable) estimates based on LAME and
64 * some testing; it is more important to preserve all the meaningful samples
65 * than to eliminate all silent ones.
66 */
67 #define MP3F_DECODER_DELAY 529
68 /*#define MP3F_ENCODER_DELAY 576*/
69 #define MP3F_ENCODER_DELAY 96
70
71 #ifdef MP3_DEBUG
72 # define MP3_DPRINTF(x) printf x
73 # define MP3_PERCENT(base, value) (((float)(value) - (float)(base)) * 100.0 / (float)(base))
74 #else /* !MP3_DEBUG */
75 # define MP3_DPRINTF(x)
76 #endif /* MP3_DEBUG */
77
78 struct _MP3_FILE
79 {
80 struct mad_decoder decoder;
81
82 FILE *f;
83 unsigned char buffer [MP3F_BUFFER_SIZE];
84
85 int xing;
86
87 unsigned channels;
88 unsigned frequency;
89 unsigned frames;
90 unsigned samples_per_frame;
91 MP3F_OFFSET samples;
92
93 MP3F_OFFSET locations[MP3F_MAX_LOCATIONS];
94 unsigned num_locations;
95 unsigned frames_per_location;
96
97 unsigned delay;
98
99 MP3F_CALLBACK callback;
100 void *context;
101
102 MP3F_OFFSET next_read_position;
103 MP3F_OFFSET read_amount;
104 MP3F_OFFSET first_offset;
105 unsigned skip_amount;
106 int need_seek;
107 MP3F_OFFSET id3TagSize_bytes; /* David Weenink */
108 };
109
110 static enum mad_flow mp3f_mad_error (void *context, struct mad_stream *stream, struct mad_frame *frame);
111 static enum mad_flow mp3f_mad_input (void *context, struct mad_stream *stream);
112 static enum mad_flow mp3f_mad_first_header (void *context, struct mad_header const *header);
113 static enum mad_flow mp3f_mad_first_filter (void *context,
114 struct mad_stream const *stream,
115 struct mad_frame *frame);
116 static int mp3f_check_xing (MP3_FILE mp3f, struct mad_stream const *stream);
117 static enum mad_flow mp3f_mad_scan_header (void *context, struct mad_header const *header);
118 static enum mad_flow mp3f_mad_report_samples (void *context, struct mad_header const *header, struct mad_pcm *pcm);
119
120 int mp3_recognize (int nread, const char *data)
121 {
122 const unsigned char *bytes = (const unsigned char *)data;
123
124 if (nread < 3)
125 return 0;
126
127 /* MP3 files can start with an ID3 tag */
128 if (bytes [0] == 'I' && bytes [1] == 'D' && bytes [2] == '3')
129 return 1;
130
131 /* Otherwise the beginning of the file must be an MP3 frame */
132 if (bytes [0] != 0xFF)
133 return 0;
134
135 /* This is not a foolproof check, but it is similar to file(1) */
136 return
137 ((bytes [1] & 0xFE) == 0xFA) || /* MPEG ADTS, layer III, v1 */
138 ((bytes [1] & 0xFE) == 0xFC) || /* MPEG ADTS, layer II, v1 */
139 ((bytes [1] & 0xFE) == 0xFE) || /* MPEG ADTS, layer I, v1 */
140 ((bytes [1] & 0xFE) == 0xF2) || /* MPEG ADTS, layer III, v2 */
141 ((bytes [1] & 0xFE) == 0xF4) || /* MPEG ADTS, layer II, v2 */
142 ((bytes [1] & 0xFE) == 0xF6) || /* MPEG ADTS, layer I, v2 */
143 ((bytes [1] & 0xFE) == 0xE2); /* MPEG ADTS, layer III, v2.5 */
144 }
145
146 MP3_FILE mp3f_new ()
147 {
148 try {
149 return Melder_calloc (struct _MP3_FILE, 1);
150 } catch (MelderError) {
151 Melder_throw (U"Cannot create MP3 file object.");
152 }
153 }
154
155 void mp3f_delete (MP3_FILE mp3f)
156 {
157 Melder_free (mp3f);
158 }
159
160 void mp3f_set_file (MP3_FILE mp3f, FILE *f)
161 {
162 mp3f -> f = f;
163 if (! f)
164 return;
165 fseek (f, 0, SEEK_SET);
166 /* David Weenink 20151005
167 Check if an ID3 header version 2 or newer is present at the START of the file (older header types are always at the END of the file). We calculate the size of the header (in bytes), store it in the mp3f -> id3TagSize_bytes field and simply skip this amount of bytes before analyzing/decoding starts.
168
169 According to http://id3.org/id3v2-00:
170 The ID3v2 tag header, which should be the FIRST information in the file, is 10 bytes as follows:
171
172 ID3/file identifier "ID3"
173 ID3 version $02 00
174 ID3 flags %xx000000
175 ID3 size 4 * %0xxxxxxx
176
177 The first three bytes of the tag are always "ID3" to indicate that
178 this is an ID3 tag, directly followed by the two version bytes. The
179 first byte of ID3 version is it's major version, while the second byte
180 is its revision number. All revisions are backwards compatible while
181 major versions are not. If software with ID3v2 and below support
182 should encounter version three or higher it should simply ignore the
183 whole tag. Version and revision will never be $FF.
184
185 The first bit (bit 7) in the 'ID3 flags' is indicating whether or not
186 unsynchronisation is used (see section 5 for details); a set bit
187 indicates usage.
188
189 The second bit (bit 6) is indicating whether or not compression is
190 used; a set bit indicates usage. Since no compression scheme has been
191 decided yet, the ID3 decoder (for now) should just ignore the entire
192 tag if the compression bit is set.
193
194 The ID3 tag size is encoded with four bytes where the first bit (bit
195 7) is set to zero in every byte, making a total of 28 bits. The zeroed
196 bits are ignored, so a 257 bytes long tag is represented as $00 00 02 01.
197
198 The ID3 tag size is the size of the complete tag after
199 unsychronisation, including padding, excluding the header (total tag
200 size - 10). The reason to use 28 bits (representing up to 256MB) for
201 size description is that we don't want to run out of space here.
202
203 A ID3v2 tag can be detected with the following pattern:
204 $49 44 33 yy yy xx zz zz zz zz
205 Where yy is less than $FF, xx is the 'flags' byte and zz is less than$80.
206 */
207 {
208 unsigned char bytes [10];
209 (void) fread (& bytes, 1, 10, mp3f -> f);
210 mp3f -> id3TagSize_bytes = 0;
211 if (bytes[0] == 'I' && bytes[1] == 'D' && bytes[2] == '3') {
212 if (bytes[3] < 0xFF && bytes[4] < 0xFF &&
213 bytes[6] < 0x80 && bytes[7] < 0x80 && bytes[8] < 0x80 && bytes[9] < 0x80 ) {
214 /*
215 Ignore version: bytes[3] & bytes[4]
216 Ignore flags: bytes[5]
217 The only purpose of the 'unsychronisation scheme' is to make the ID3v2
218 tag as compatible as possible with existing software. There is no use
219 in 'unsynchronising' tags if the file is only to be processed by new
220 software. Unsynchronisation may only be made with MPEG 2 layer I, II
221 and III and MPEG 2.5 files.
222 */
223 mp3f -> id3TagSize_bytes = (bytes[6] << 21 | bytes[7] << 14 | bytes[8] << 7 | bytes[9]) + 10;
224 }
225 }
226 }
227
228 fseek (f, mp3f -> id3TagSize_bytes, SEEK_SET); // David Weenink
229
230 mp3f -> next_read_position = 0;
231 mp3f -> need_seek = 0;
232 mp3f -> delay = MP3F_DECODER_DELAY + MP3F_ENCODER_DELAY;
233 mp3f -> skip_amount = mp3f -> delay;
234 mp3f -> first_offset = 0;
235 }
236
237 int mp3f_analyze (MP3_FILE mp3f)
238 {
239 struct mad_decoder *decoder = & mp3f -> decoder;
240 int status;
241 #ifdef MP3_DEBUG
242 unsigned estimate, last;
243 #endif /* MP3_DEBUG */
244
245 if (! mp3f || ! mp3f -> f)
246 return 0;
247
248 fseek (mp3f -> f, mp3f -> id3TagSize_bytes, SEEK_SET); // David Weenink
249
250 mp3f -> xing = 0;
251 mp3f -> channels = 0;
252 mp3f -> frequency = 0;
253 mp3f -> frames = 0;
254 mp3f -> samples = 0;
255 mp3f -> samples_per_frame = 0;
256 mp3f -> num_locations = 0;
257
258 /* Read first frames to get basic parameters and hopefully Xing */
259 mad_decoder_init (decoder,
260 mp3f,
261 mp3f_mad_input,
262 mp3f_mad_first_header,
263 mp3f_mad_first_filter,
264 nullptr /* Output: Don't actually decode for now */,
265 mp3f_mad_error,
266 nullptr /* Message */);
267
268 status = mad_decoder_run (decoder, MAD_DECODER_MODE_SYNC);
269 if (status != 0)
270 goto end;
271
272 /*
273 * If we don't have a Xing header we need to estimate the frame count.
274 * This doesn't have to be accurate since we're going to count them
275 * later when we scan for header offsets.
276 */
277 if (! mp3f -> xing) {
278 MP3F_OFFSET file_size, frame_size;
279
280 /* Take size of first frame */
281 frame_size = mp3f -> locations [1] - mp3f -> locations[0];
282
283 /* For file size, seek to end */
284 fseek (mp3f -> f, mp3f -> id3TagSize_bytes, SEEK_END); // David Weenink
285 file_size = ftell (mp3f -> f);
286
287 /* This estimate will be pretty accurate for CBR */
288 mp3f -> frames = file_size / frame_size;
289
290 MP3_DPRINTF (("File size: %lu bytes\n", (unsigned long)file_size));
291 MP3_DPRINTF (("First frame size: %lu bytes\n", (unsigned long)frame_size));
292 MP3_DPRINTF (("Estimated frames: %lu\n", (unsigned long)mp3f -> frames));
293 }
294
295 /* Calculate how many frames can fit in a "location" */
296 if (mp3f -> frames <= MP3F_MAX_LOCATIONS)
297 mp3f -> frames_per_location = 1;
298 else
299 mp3f -> frames_per_location = (mp3f -> frames + MP3F_MAX_LOCATIONS - 1) / MP3F_MAX_LOCATIONS;
300
301 MP3_DPRINTF (("MP3: Each location is %u frame(s) (%.3fs), each %u samples\n",
302 mp3f -> frames_per_location,
303 mp3f -> frames_per_location * mp3f -> samples_per_frame / (float)mp3f -> frequency,
304 mp3f -> samples_per_frame));
305
306 /* Read all frames to get offsets*/
307 #ifdef MP3_DEBUG
308 estimate = mp3f -> frames;
309 #endif /* MP3_DEBUG */
310 mp3f -> num_locations = 0;
311 mp3f -> frames = 0;
312 mp3f -> samples = 0;
313
314 fseek (mp3f -> f, mp3f -> id3TagSize_bytes, SEEK_SET); // David Weenink
315 mad_decoder_init (decoder,
316 mp3f,
317 mp3f_mad_input,
318 mp3f_mad_scan_header,
319 nullptr /* Filter */,
320 nullptr /* Output */,
321 mp3f_mad_error,
322 nullptr /* Message */);
323
324 status = mad_decoder_run (decoder, MAD_DECODER_MODE_SYNC);
325
326 MP3_DPRINTF (("MP3 Frames: %u, estimated %u (%+.2f%%)\n",
327 mp3f -> frames,
328 estimate,
329 MP3_PERCENT (mp3f -> frames, estimate)));
330 #ifdef MP3_DEBUG
331 last = mp3f -> frames - (mp3f -> frames_per_location * (mp3f -> num_locations - 1));
332 MP3_DPRINTF (("MP3F: Last location frames = %u (%+.2f%%) = %.3fs\n",
333 last,
334 MP3_PERCENT (mp3f -> frames_per_location, last),
335 last * mp3f -> samples_per_frame / (float)mp3f -> frequency));
336 #endif /* MP3_DEBUG */
337
338 if(status!=-1) // ppgb 2015-01-17
339 mp3f_seek (mp3f, 0);
340
341 end:
342 mad_decoder_finish (decoder);
343
344 return (status == 0);
345 }
346
347 unsigned mp3f_channels (MP3_FILE mp3f)
348 {
349 return mp3f -> channels;
350 }
351
352 unsigned mp3f_frequency (MP3_FILE mp3f)
353 {
354 return mp3f -> frequency;
355 }
356
357 MP3F_OFFSET mp3f_samples (MP3_FILE mp3f)
358 {
359 return mp3f -> samples - mp3f -> delay;
360 }
361
362 void mp3f_set_callback (MP3_FILE mp3f,
363 MP3F_CALLBACK callback, void *context)
364 {
365 mp3f -> callback = callback;
366 mp3f -> context = context;
367 }
368
369 int mp3f_seek (MP3_FILE mp3f, MP3F_OFFSET sample)
370 {
371 MP3F_OFFSET frame, location, base, offset;
372
373 if (! mp3f || ! mp3f -> f)
374 return 0;
375
376 if (! mp3f -> frames_per_location)
377 if (! mp3f_analyze (mp3f))
378 return 0;
379
380 /* Compensate for initial empty frames */
381 sample += mp3f -> delay;
382
383 /* Calculate where we need to seek */
384 frame = sample / mp3f -> samples_per_frame;
385 if ( frame ) /* libMAD can skip the first frame... */
386 -- frame;
387 if ( frame ) /* ...and the first frame it decodes is useless */
388 -- frame;
389 Melder_assert (mp3f -> frames_per_location > 0);
390 Melder_assert (mp3f -> num_locations > 0);
391 location = frame / mp3f -> frames_per_location;
392 if (location >= mp3f -> num_locations)
393 location = mp3f -> num_locations - 1;
394 frame = location * mp3f -> frames_per_location;
395 base = frame * mp3f -> samples_per_frame;
396
397 Melder_assert (location >= 0);
398 offset = mp3f -> locations [location];
399 if (fseek (mp3f -> f, offset, SEEK_SET) < 0)
400 return 0;
401
402 mp3f -> first_offset = offset;
403 mp3f -> skip_amount = sample - base;
404 mp3f -> need_seek = 0;
405
406 MP3_DPRINTF (("SEEK to %lu (%lu + %u): Frame %lu, location %lu, offset %lu, base %lu, skip %u\n",
407 (unsigned long)sample,
408 (unsigned long)sample - mp3f -> delay,
409 mp3f -> delay,
410 (unsigned long)frame,
411 (unsigned long)location,
412 (unsigned long)offset,
413 (unsigned long)base,
414 mp3f -> skip_amount));
415
416 return 1;
417 }
418
419 int mp3f_read (MP3_FILE mp3f, MP3F_OFFSET num_samples)
420 {
421 int status;
422 struct mad_decoder *decoder = &(mp3f -> decoder);
423
424 if (! mp3f || ! mp3f -> f || ! mp3f -> callback)
425 return 0;
426
427 /* Seek if the last read left us in mid-frame */
428 if (mp3f -> need_seek)
429 if (! mp3f_seek (mp3f, mp3f -> next_read_position))
430 return 0;
431
432 mad_decoder_init (decoder,
433 mp3f,
434 mp3f_mad_input,
435 nullptr /* Header */,
436 nullptr /* Filter */,
437 mp3f_mad_report_samples,
438 mp3f_mad_error,
439 nullptr /* Message */);
440
441 mp3f -> read_amount = num_samples;
442 status = mad_decoder_run (decoder, MAD_DECODER_MODE_SYNC);
443 mad_decoder_finish (decoder);
444
445 mp3f -> next_read_position += num_samples;
446 return (status == 0);
447 }
448
449 static enum mad_flow mp3f_mad_report_samples (void *context, struct mad_header const *header, struct mad_pcm *pcm)
450 {
451 MP3_FILE mp3f = (MP3_FILE) context;
452 const int *channels [] = { pcm -> samples [0], pcm -> samples [1] };
453 unsigned length = pcm -> length;
454
455 if (! mp3f || ! mp3f -> callback)
456 return MAD_FLOW_BREAK;
457
458 if (mp3f -> first_offset) {
459 /* libMAD can decide to skip the first frame */
460 if (header -> offset > mp3f -> first_offset) {
461 MP3_DPRINTF (("Skip %u of %lu\n", length, mp3f -> skip_amount));
462 mp3f -> skip_amount -= length;
463 }
464 mp3f -> first_offset = 0;
465 }
466
467 if (mp3f -> skip_amount >= length) {
468 mp3f -> skip_amount -= length;
469 return MAD_FLOW_IGNORE;
470 }
471
472 if (mp3f -> skip_amount > 0) {
473 channels [0] += mp3f -> skip_amount;
474 channels [1] += mp3f -> skip_amount;
475 length -= mp3f -> skip_amount;
476 mp3f -> skip_amount = 0;
477 }
478
479 if (length > mp3f -> read_amount) {
480 length = mp3f -> read_amount;
481 mp3f -> need_seek = 1;
482 }
483
484 if (length > 0)
485 mp3f -> callback (channels, length, mp3f -> context);
486
487 mp3f -> read_amount -= length;
488 return (mp3f -> read_amount > 0) ? MAD_FLOW_CONTINUE : MAD_FLOW_STOP;
489 }
490
491 /* This conversion function was taken from minimad, and it could be better */
492 short mp3f_sample_to_short (MP3F_SAMPLE sample)
493 {
494 /* round */
495 sample += (1L << (MAD_F_FRACBITS - 16));
496
497 /* clip */
498 if (sample >= MAD_F_ONE)
499 sample = MAD_F_ONE - 1;
500 else if (sample < -MAD_F_ONE)
501 sample = -MAD_F_ONE;
502
503 /* quantize */
504 return sample >> (MAD_F_FRACBITS + 1 - 16);
505 }
506
507 /* This function was adapted from libmad */
508 static enum mad_flow mp3f_mad_error(void *context, struct mad_stream *stream, struct mad_frame *frame)
509 {
510 (void) context;
511 (void) stream;
512 (void) frame;
513 return MAD_FLOW_CONTINUE;
514 }
515
516 /* This function was adapted from Audacity */
517 static enum mad_flow mp3f_mad_input(void *context, struct mad_stream *stream)
518 {
519 MP3_FILE mp3f = (MP3_FILE) context;
520 FILE *f = mp3f -> f;
521 unsigned char *buffer = nullptr;
522 unsigned nthrown = 0, ncopied = 0, size = 0;
523 size_t nread = 0;
524 MP3F_OFFSET offset;
525
526 if (feof (f))
527 return MAD_FLOW_STOP;
528
529 if (stream -> next_frame) {
530 nthrown = stream -> next_frame - mp3f -> buffer;
531 ncopied = MP3F_BUFFER_SIZE - nthrown;
532 memmove (mp3f -> buffer, stream -> next_frame, ncopied);
533 }
534
535 buffer = mp3f -> buffer + ncopied;
536 size = MP3F_BUFFER_SIZE - ncopied;
537 offset = ftell (f) - ncopied;
538
539 if (size > 0)
540 nread = fread (buffer, 1, size, f);
541
542 mad_stream_buffer_offset (stream, mp3f -> buffer, nread + ncopied, offset);
543 return MAD_FLOW_CONTINUE;
544 }
545
546 static enum mad_flow mp3f_mad_first_header(void *context, struct mad_header const *header)
547 {
548 MP3_FILE mp3f = (MP3_FILE) context;
549
550 mp3f -> channels = MAD_NCHANNELS (header);
551 mp3f -> frequency = header -> samplerate;
552 mp3f -> samples_per_frame = 32 * MAD_NSBSAMPLES (header);
553 /* Just in case there is no Xing header: */
554 mp3f -> locations [mp3f -> num_locations ++] = header -> offset;
555
556 return MAD_FLOW_CONTINUE;
557 }
558
559 static enum mad_flow mp3f_mad_first_filter (void *context,
560 struct mad_stream const *stream,
561 struct mad_frame *frame)
562 {
563 MP3_FILE mp3f = (MP3_FILE) context;
564
565 (void) frame;
566
567 mp3f -> xing = mp3f_check_xing (mp3f, stream);
568
569 /* Xing? No need to look further */
570 if (mp3f -> xing)
571 return MAD_FLOW_STOP;
572
573 /* Otherwise, read two frames for size estimate */
574 return (mp3f -> num_locations < 2) ? MAD_FLOW_IGNORE : MAD_FLOW_STOP;
575 }
576
577 static enum mad_flow mp3f_mad_scan_header(void *context, struct mad_header const *header)
578 {
579 MP3_FILE mp3f = (MP3_FILE) context;
580
581 /* Some sanity checks */
582 if (mp3f -> channels != MAD_NCHANNELS (header))
583 return MAD_FLOW_BREAK;
584
585 if (mp3f -> frequency != header -> samplerate)
586 return MAD_FLOW_BREAK;
587
588 if (mp3f -> samples_per_frame != 32 * MAD_NSBSAMPLES (header))
589 return MAD_FLOW_BREAK;
590
591 /* Check whether to log this offset in the table */
592 if ((mp3f -> frames % mp3f -> frames_per_location) == 0 &&
593 mp3f -> num_locations < MP3F_MAX_LOCATIONS)
594 mp3f -> locations [mp3f -> num_locations ++] = header -> offset;
595
596 /* Count this frame */
597 ++ mp3f -> frames;
598 mp3f -> samples += mp3f -> samples_per_frame;
599
600 return MAD_FLOW_IGNORE;
601 }
602
603 /*
604 * Identify a Xing VBR header.
605 * This was adapted from madplay.
606 */
607 # define XING_VBR_MAGIC (('X' << 24) | ('i' << 16) | ('n' << 8) | 'g')
608 # define XING_CBR_MAGIC (('I' << 24) | ('n' << 16) | ('f' << 8) | 'o')
609
610 enum {
611 XING_FLAGS_FRAMES = 0x0000'0001L,
612 XING_FLAGS_BYTES = 0x0000'0002L,
613 XING_FLAGS_TOC = 0x0000'0004L,
614 XING_FLAGS_SCALE = 0x0000'0008L
615 };
616
617 static int mp3f_check_xing (MP3_FILE mp3f, struct mad_stream const *stream)
618 {
619 struct mad_bitptr ptr = stream -> anc_ptr;
620 unsigned long magic, flags;
621
622 /* When we get here we have the following data from the header:
623 * channels
624 * frequency
625 * samples_per_frame
626 * We need to calculate the total number of frames and samples.
627 */
628
629 magic = mad_bit_read (&ptr, 32);
630 if (magic != XING_CBR_MAGIC && magic != XING_VBR_MAGIC)
631 return 0;
632
633 flags = mad_bit_read (&ptr, 32);
634 if ((flags & XING_FLAGS_FRAMES) != XING_FLAGS_FRAMES)
635 return 0;
636
637 mp3f -> frames = mad_bit_read (&ptr, 32);
638 mp3f -> samples = mp3f -> samples_per_frame * mp3f -> frames;
639
640 return 1;
641 }
642