mp3.cpp 19 KB

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