mp3reader.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright (C) 2014 The Android Open Source Project
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  18. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  22. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  25. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #define LOG_TAG "mp3reader"
  29. #include <stdlib.h>
  30. #include <assert.h>
  31. #include <stdint.h>
  32. #include <string.h> // Resolves that memset, memcpy aren't found while APP_PLATFORM >= 22 on Android
  33. #include <vector>
  34. #include "audio/android/cutils/log.h"
  35. #include "pvmp3decoder_api.h"
  36. #include "audio/android/mp3reader.h"
  37. static uint32_t U32_AT(const uint8_t *ptr) {
  38. return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
  39. }
  40. static bool parseHeader(
  41. uint32_t header, size_t *frame_size,
  42. uint32_t *out_sampling_rate = NULL, uint32_t *out_channels = NULL ,
  43. uint32_t *out_bitrate = NULL, uint32_t *out_num_samples = NULL) {
  44. *frame_size = 0;
  45. if (out_sampling_rate) {
  46. *out_sampling_rate = 0;
  47. }
  48. if (out_channels) {
  49. *out_channels = 0;
  50. }
  51. if (out_bitrate) {
  52. *out_bitrate = 0;
  53. }
  54. if (out_num_samples) {
  55. *out_num_samples = 1152;
  56. }
  57. if ((header & 0xffe00000) != 0xffe00000) {
  58. return false;
  59. }
  60. unsigned version = (header >> 19) & 3;
  61. if (version == 0x01) {
  62. return false;
  63. }
  64. unsigned layer = (header >> 17) & 3;
  65. if (layer == 0x00) {
  66. return false;
  67. }
  68. unsigned bitrate_index = (header >> 12) & 0x0f;
  69. if (bitrate_index == 0 || bitrate_index == 0x0f) {
  70. // Disallow "free" bitrate.
  71. return false;
  72. }
  73. unsigned sampling_rate_index = (header >> 10) & 3;
  74. if (sampling_rate_index == 3) {
  75. return false;
  76. }
  77. static const int kSamplingRateV1[] = { 44100, 48000, 32000 };
  78. int sampling_rate = kSamplingRateV1[sampling_rate_index];
  79. if (version == 2 /* V2 */) {
  80. sampling_rate /= 2;
  81. } else if (version == 0 /* V2.5 */) {
  82. sampling_rate /= 4;
  83. }
  84. unsigned padding = (header >> 9) & 1;
  85. if (layer == 3) {
  86. // layer I
  87. static const int kBitrateV1[] = {
  88. 32, 64, 96, 128, 160, 192, 224, 256,
  89. 288, 320, 352, 384, 416, 448
  90. };
  91. static const int kBitrateV2[] = {
  92. 32, 48, 56, 64, 80, 96, 112, 128,
  93. 144, 160, 176, 192, 224, 256
  94. };
  95. int bitrate =
  96. (version == 3 /* V1 */)
  97. ? kBitrateV1[bitrate_index - 1]
  98. : kBitrateV2[bitrate_index - 1];
  99. if (out_bitrate) {
  100. *out_bitrate = bitrate;
  101. }
  102. *frame_size = (12000 * bitrate / sampling_rate + padding) * 4;
  103. if (out_num_samples) {
  104. *out_num_samples = 384;
  105. }
  106. } else {
  107. // layer II or III
  108. static const int kBitrateV1L2[] = {
  109. 32, 48, 56, 64, 80, 96, 112, 128,
  110. 160, 192, 224, 256, 320, 384
  111. };
  112. static const int kBitrateV1L3[] = {
  113. 32, 40, 48, 56, 64, 80, 96, 112,
  114. 128, 160, 192, 224, 256, 320
  115. };
  116. static const int kBitrateV2[] = {
  117. 8, 16, 24, 32, 40, 48, 56, 64,
  118. 80, 96, 112, 128, 144, 160
  119. };
  120. int bitrate;
  121. if (version == 3 /* V1 */) {
  122. bitrate = (layer == 2 /* L2 */)
  123. ? kBitrateV1L2[bitrate_index - 1]
  124. : kBitrateV1L3[bitrate_index - 1];
  125. if (out_num_samples) {
  126. *out_num_samples = 1152;
  127. }
  128. } else {
  129. // V2 (or 2.5)
  130. bitrate = kBitrateV2[bitrate_index - 1];
  131. if (out_num_samples) {
  132. *out_num_samples = (layer == 1 /* L3 */) ? 576 : 1152;
  133. }
  134. }
  135. if (out_bitrate) {
  136. *out_bitrate = bitrate;
  137. }
  138. if (version == 3 /* V1 */) {
  139. *frame_size = 144000 * bitrate / sampling_rate + padding;
  140. } else {
  141. // V2 or V2.5
  142. size_t tmp = (layer == 1 /* L3 */) ? 72000 : 144000;
  143. *frame_size = tmp * bitrate / sampling_rate + padding;
  144. }
  145. }
  146. if (out_sampling_rate) {
  147. *out_sampling_rate = sampling_rate;
  148. }
  149. if (out_channels) {
  150. int channel_mode = (header >> 6) & 3;
  151. *out_channels = (channel_mode == 3) ? 1 : 2;
  152. }
  153. return true;
  154. }
  155. // Mask to extract the version, layer, sampling rate parts of the MP3 header,
  156. // which should be same for all MP3 frames.
  157. static const uint32_t kMask = 0xfffe0c00;
  158. static ssize_t sourceReadAt(mp3_callbacks *callback, void* source, off64_t offset, void *data, size_t size) {
  159. int retVal = callback->seek(source, offset, SEEK_SET);
  160. if (retVal != EXIT_SUCCESS) {
  161. return 0;
  162. } else {
  163. return callback->read(data, 1, size, source);
  164. }
  165. }
  166. // Resync to next valid MP3 frame in the file.
  167. static bool resync(
  168. mp3_callbacks *callback, void* source, uint32_t match_header,
  169. off64_t *inout_pos, uint32_t *out_header) {
  170. if (*inout_pos == 0) {
  171. // Skip an optional ID3 header if syncing at the very beginning
  172. // of the datasource.
  173. for (;;) {
  174. uint8_t id3header[10];
  175. int retVal = sourceReadAt(callback, source, *inout_pos, id3header,
  176. sizeof(id3header));
  177. if (retVal < (ssize_t)sizeof(id3header)) {
  178. // If we can't even read these 10 bytes, we might as well bail
  179. // out, even if there _were_ 10 bytes of valid mp3 audio data...
  180. return false;
  181. }
  182. if (memcmp("ID3", id3header, 3)) {
  183. break;
  184. }
  185. // Skip the ID3v2 header.
  186. size_t len =
  187. ((id3header[6] & 0x7f) << 21)
  188. | ((id3header[7] & 0x7f) << 14)
  189. | ((id3header[8] & 0x7f) << 7)
  190. | (id3header[9] & 0x7f);
  191. len += 10;
  192. *inout_pos += len;
  193. ALOGV("skipped ID3 tag, new starting offset is %lld (0x%016llx)",
  194. (long long)*inout_pos, (long long)*inout_pos);
  195. }
  196. }
  197. off64_t pos = *inout_pos;
  198. bool valid = false;
  199. const int32_t kMaxReadBytes = 1024;
  200. const int32_t kMaxBytesChecked = 128 * 1024;
  201. uint8_t buf[kMaxReadBytes];
  202. ssize_t bytesToRead = kMaxReadBytes;
  203. ssize_t totalBytesRead = 0;
  204. ssize_t remainingBytes = 0;
  205. bool reachEOS = false;
  206. uint8_t *tmp = buf;
  207. do {
  208. if (pos >= (off64_t)(*inout_pos + kMaxBytesChecked)) {
  209. // Don't scan forever.
  210. ALOGV("giving up at offset %lld", (long long)pos);
  211. break;
  212. }
  213. if (remainingBytes < 4) {
  214. if (reachEOS) {
  215. break;
  216. } else {
  217. memcpy(buf, tmp, remainingBytes);
  218. bytesToRead = kMaxReadBytes - remainingBytes;
  219. /*
  220. * The next read position should start from the end of
  221. * the last buffer, and thus should include the remaining
  222. * bytes in the buffer.
  223. */
  224. totalBytesRead = sourceReadAt(callback, source, pos + remainingBytes,
  225. buf + remainingBytes, bytesToRead);
  226. if (totalBytesRead <= 0) {
  227. break;
  228. }
  229. reachEOS = (totalBytesRead != bytesToRead);
  230. remainingBytes += totalBytesRead;
  231. tmp = buf;
  232. continue;
  233. }
  234. }
  235. uint32_t header = U32_AT(tmp);
  236. if (match_header != 0 && (header & kMask) != (match_header & kMask)) {
  237. ++pos;
  238. ++tmp;
  239. --remainingBytes;
  240. continue;
  241. }
  242. size_t frame_size;
  243. uint32_t sample_rate, num_channels, bitrate;
  244. if (!parseHeader(
  245. header, &frame_size,
  246. &sample_rate, &num_channels, &bitrate)) {
  247. ++pos;
  248. ++tmp;
  249. --remainingBytes;
  250. continue;
  251. }
  252. // ALOGV("found possible 1st frame at %lld (header = 0x%08x)", (long long)pos, header);
  253. // We found what looks like a valid frame,
  254. // now find its successors.
  255. off64_t test_pos = pos + frame_size;
  256. valid = true;
  257. const int FRAME_MATCH_REQUIRED = 3;
  258. for (int j = 0; j < FRAME_MATCH_REQUIRED; ++j) {
  259. uint8_t tmp[4];
  260. ssize_t retval = sourceReadAt(callback, source, test_pos, tmp, sizeof(tmp));
  261. if (retval < (ssize_t)sizeof(tmp)) {
  262. valid = false;
  263. break;
  264. }
  265. uint32_t test_header = U32_AT(tmp);
  266. ALOGV("subsequent header is %08x", test_header);
  267. if ((test_header & kMask) != (header & kMask)) {
  268. valid = false;
  269. break;
  270. }
  271. size_t test_frame_size;
  272. if (!parseHeader(test_header, &test_frame_size)) {
  273. valid = false;
  274. break;
  275. }
  276. ALOGV("found subsequent frame #%d at %lld", j + 2, (long long)test_pos);
  277. test_pos += test_frame_size;
  278. }
  279. if (valid) {
  280. *inout_pos = pos;
  281. if (out_header != NULL) {
  282. *out_header = header;
  283. }
  284. } else {
  285. ALOGV("no dice, no valid sequence of frames found.");
  286. }
  287. ++pos;
  288. ++tmp;
  289. --remainingBytes;
  290. } while (!valid);
  291. return valid;
  292. }
  293. Mp3Reader::Mp3Reader() : mSource(NULL), mCallback(NULL) {
  294. }
  295. // Initialize the MP3 reader.
  296. bool Mp3Reader::init(mp3_callbacks *callback, void* source) {
  297. mSource = source;
  298. mCallback = callback;
  299. // Open the file.
  300. // mFp = fopen(file, "rb");
  301. // if (mFp == NULL) return false;
  302. // Sync to the first valid frame.
  303. off64_t pos = 0;
  304. uint32_t header;
  305. bool success = resync(callback, source, 0 /*match_header*/, &pos, &header);
  306. if (!success)
  307. {
  308. ALOGE("%s, resync failed", __FUNCTION__);
  309. return false;
  310. }
  311. mCurrentPos = pos;
  312. mFixedHeader = header;
  313. size_t frame_size;
  314. return parseHeader(header, &frame_size, &mSampleRate,
  315. &mNumChannels, &mBitrate);
  316. }
  317. // Get the next valid MP3 frame.
  318. bool Mp3Reader::getFrame(void *buffer, uint32_t *size) {
  319. size_t frame_size;
  320. uint32_t bitrate;
  321. uint32_t num_samples;
  322. uint32_t sample_rate;
  323. for (;;) {
  324. ssize_t n = sourceReadAt(mCallback, mSource, mCurrentPos, buffer, 4);
  325. if (n < 4) {
  326. return false;
  327. }
  328. uint32_t header = U32_AT((const uint8_t *)buffer);
  329. if ((header & kMask) == (mFixedHeader & kMask)
  330. && parseHeader(
  331. header, &frame_size, &sample_rate, NULL /*out_channels*/,
  332. &bitrate, &num_samples)) {
  333. break;
  334. }
  335. // Lost sync.
  336. off64_t pos = mCurrentPos;
  337. if (!resync(mCallback, mSource, mFixedHeader, &pos, NULL /*out_header*/)) {
  338. // Unable to resync. Signalling end of stream.
  339. return false;
  340. }
  341. mCurrentPos = pos;
  342. // Try again with the new position.
  343. }
  344. ssize_t n = sourceReadAt(mCallback, mSource, mCurrentPos, buffer, frame_size);
  345. if (n < (ssize_t)frame_size) {
  346. return false;
  347. }
  348. *size = frame_size;
  349. mCurrentPos += frame_size;
  350. return true;
  351. }
  352. // Close the MP3 reader.
  353. void Mp3Reader::close() {
  354. assert(mCallback != NULL);
  355. mCallback->close(mSource);
  356. }
  357. Mp3Reader::~Mp3Reader() {
  358. }
  359. enum {
  360. kInputBufferSize = 10 * 1024,
  361. kOutputBufferSize = 4608 * 2,
  362. };
  363. int decodeMP3(mp3_callbacks* cb, void* source, std::vector<char>& pcmBuffer, int* numChannels, int* sampleRate, int* numFrames)
  364. {
  365. // Initialize the config.
  366. tPVMP3DecoderExternal config;
  367. config.equalizerType = flat;
  368. config.crcEnabled = false;
  369. // Allocate the decoder memory.
  370. uint32_t memRequirements = pvmp3_decoderMemRequirements();
  371. void *decoderBuf = malloc(memRequirements);
  372. assert(decoderBuf != NULL);
  373. // Initialize the decoder.
  374. pvmp3_InitDecoder(&config, decoderBuf);
  375. // Open the input file.
  376. Mp3Reader mp3Reader;
  377. bool success = mp3Reader.init(cb, source);
  378. if (!success) {
  379. ALOGE("mp3Reader.init: Encountered error reading\n");
  380. free(decoderBuf);
  381. return EXIT_FAILURE;
  382. }
  383. // Open the output file.
  384. // SF_INFO sfInfo;
  385. // memset(&sfInfo, 0, sizeof(SF_INFO));
  386. // sfInfo.channels = mp3Reader.getNumChannels();
  387. // sfInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
  388. // sfInfo.samplerate = mp3Reader.getSampleRate();
  389. // SNDFILE *handle = sf_open(argv[2], SFM_WRITE, &sfInfo);
  390. // if (handle == NULL) {
  391. // ALOGE("Encountered error writing %s\n", argv[2]);
  392. // mp3Reader.close();
  393. // free(decoderBuf);
  394. // return EXIT_FAILURE;
  395. // }
  396. // Allocate input buffer.
  397. uint8_t *inputBuf = static_cast<uint8_t*>(malloc(kInputBufferSize));
  398. assert(inputBuf != NULL);
  399. // Allocate output buffer.
  400. int16_t *outputBuf = static_cast<int16_t*>(malloc(kOutputBufferSize));
  401. assert(outputBuf != NULL);
  402. // Decode loop.
  403. int retVal = EXIT_SUCCESS;
  404. while (1) {
  405. // Read input from the file.
  406. uint32_t bytesRead;
  407. bool success = mp3Reader.getFrame(inputBuf, &bytesRead);
  408. if (!success) break;
  409. *numChannels = mp3Reader.getNumChannels();
  410. *sampleRate = mp3Reader.getSampleRate();
  411. // Set the input config.
  412. config.inputBufferCurrentLength = bytesRead;
  413. config.inputBufferMaxLength = 0;
  414. config.inputBufferUsedLength = 0;
  415. config.pInputBuffer = inputBuf;
  416. config.pOutputBuffer = outputBuf;
  417. config.outputFrameSize = kOutputBufferSize / sizeof(int16_t);
  418. ERROR_CODE decoderErr;
  419. decoderErr = pvmp3_framedecoder(&config, decoderBuf);
  420. if (decoderErr != NO_DECODING_ERROR) {
  421. ALOGE("Decoder encountered error=%d", decoderErr);
  422. retVal = EXIT_FAILURE;
  423. break;
  424. }
  425. pcmBuffer.insert(pcmBuffer.end(), (char*)outputBuf, ((char*)outputBuf) + config.outputFrameSize * 2);
  426. *numFrames += config.outputFrameSize / mp3Reader.getNumChannels();
  427. }
  428. // Close input reader and output writer.
  429. mp3Reader.close();
  430. // sf_close(handle);
  431. // Free allocated memory.
  432. free(inputBuf);
  433. free(outputBuf);
  434. free(decoderBuf);
  435. return retVal;
  436. }