mp3reader.cpp 15 KB

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