EncodedAudioStream.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "EncodedAudioStream.h"
  2. #include <string.h> // for memcpy, memmove
  3. #include <stdexcept> // for runtime_error
  4. #include <type_traits> // for remove_extent_t
  5. #include <utility> // for move
  6. #include "BellLogger.h" // for AbstractLogger, BELL_LOG, bell
  7. #include "ByteStream.h" // for ByteStream
  8. #include "DecoderGlobals.h" // for DecodersInstance, decodersInstance, AAC_...
  9. using namespace bell;
  10. EncodedAudioStream::EncodedAudioStream() {
  11. bell::decodersInstance->ensureAAC();
  12. bell::decodersInstance->ensureMP3();
  13. inputBuffer = std::vector<uint8_t>(1024 * 4);
  14. outputBuffer = std::vector<short>(2 * 2 * 4 * 4);
  15. decodePtr = inputBuffer.data();
  16. }
  17. EncodedAudioStream::~EncodedAudioStream() {
  18. this->innerStream->close();
  19. }
  20. void EncodedAudioStream::openWithStream(
  21. std::unique_ptr<bell::ByteStream> byteStream) {
  22. if (this->innerStream) {
  23. this->innerStream->close();
  24. }
  25. this->innerStream = std::move(byteStream);
  26. this->guessDataFormat();
  27. }
  28. bool EncodedAudioStream::vectorStartsWith(std::vector<uint8_t>& vec,
  29. std::vector<uint8_t>& start) {
  30. if (vec.size() < start.size()) {
  31. return false;
  32. }
  33. for (int i = 0; i < start.size(); i++) {
  34. if (vec[i] != start[i]) {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. size_t EncodedAudioStream::decodeFrame(uint8_t* dst) {
  41. if (innerStream->size() != 0 &&
  42. innerStream->position() >= innerStream->size()) {
  43. return 0;
  44. }
  45. switch (this->codec) {
  46. case AudioCodec::AAC:
  47. return decodeFrameAAC(dst);
  48. break;
  49. case AudioCodec::MP3:
  50. return decodeFrameMp3(dst);
  51. break;
  52. default:
  53. break;
  54. }
  55. return 0;
  56. }
  57. size_t EncodedAudioStream::decodeFrameMp3(uint8_t* dst) {
  58. size_t writtenBytes = 0;
  59. int bufSize = MP3_READBUF_SIZE;
  60. int readBytes = innerStream->read(inputBuffer.data() + bytesInBuffer,
  61. bufSize - bytesInBuffer);
  62. if (readBytes > 0) {
  63. bytesInBuffer += readBytes;
  64. decodePtr = inputBuffer.data();
  65. offset = MP3FindSyncWord(inputBuffer.data(), bytesInBuffer);
  66. if (offset != -1) {
  67. bytesInBuffer -= offset;
  68. decodePtr += offset;
  69. int decodeStatus =
  70. MP3Decode(bell::decodersInstance->mp3Decoder, &decodePtr,
  71. &bytesInBuffer, outputBuffer.data(), 0);
  72. MP3GetLastFrameInfo(bell::decodersInstance->mp3Decoder, &mp3FrameInfo);
  73. if (decodeStatus == ERR_MP3_NONE) {
  74. decodedSampleRate = mp3FrameInfo.samprate;
  75. writtenBytes =
  76. (mp3FrameInfo.bitsPerSample / 8) * mp3FrameInfo.outputSamps;
  77. memcpy(dst, outputBuffer.data(), writtenBytes);
  78. } else {
  79. BELL_LOG(info, TAG, "Error in frame, moving two bytes %d",
  80. decodeStatus);
  81. decodePtr += 1;
  82. bytesInBuffer -= 1;
  83. }
  84. } else {
  85. BELL_LOG(info, TAG, "Unexpected error in data, skipping a word");
  86. decodePtr += 3800;
  87. bytesInBuffer -= 3800;
  88. }
  89. memmove(inputBuffer.data(), decodePtr, bytesInBuffer);
  90. }
  91. return writtenBytes;
  92. }
  93. bool EncodedAudioStream::isReadable() {
  94. return this->codec != AudioCodec::NONE;
  95. }
  96. size_t EncodedAudioStream::decodeFrameAAC(uint8_t* dst) {
  97. return 0;
  98. // size_t writtenBytes = 0;
  99. // auto bufSize = AAC_READBUF_SIZE;
  100. // int readBytes = innerStream->read(inputBuffer.data() + bytesInBuffer,
  101. // bufSize - bytesInBuffer);
  102. // if (readBytes > 0) {
  103. // bytesInBuffer += readBytes;
  104. // decodePtr = inputBuffer.data();
  105. // offset = AACFindSyncWord(inputBuffer.data(), bytesInBuffer);
  106. // if (offset != -1) {
  107. // bytesInBuffer -= offset;
  108. // decodePtr += offset;
  109. // int decodeStatus =
  110. // AACDecode(bell::decodersInstance->aacDecoder, &decodePtr,
  111. // &bytesInBuffer, outputBuffer.data());
  112. // AACGetLastFrameInfo(bell::decodersInstance->aacDecoder, &aacFrameInfo);
  113. // if (decodeStatus == ERR_AAC_NONE) {
  114. // decodedSampleRate = aacFrameInfo.sampRateOut;
  115. // writtenBytes =
  116. // (aacFrameInfo.bitsPerSample / 8) * aacFrameInfo.outputSamps;
  117. // memcpy(dst, outputBuffer.data(), writtenBytes);
  118. // } else {
  119. // BELL_LOG(info, TAG, "Error in frame, moving two bytes %d",
  120. // decodeStatus);
  121. // decodePtr += 1;
  122. // bytesInBuffer -= 1;
  123. // }
  124. // } else {
  125. // BELL_LOG(info, TAG, "Unexpected error in data, skipping a word");
  126. // decodePtr += 3800;
  127. // bytesInBuffer -= 3800;
  128. // }
  129. // memmove(inputBuffer.data(), decodePtr, bytesInBuffer);
  130. // }
  131. // return writtenBytes;
  132. }
  133. void EncodedAudioStream::guessDataFormat() {
  134. // Read 14 bytes from the stream
  135. this->innerStream->read(inputBuffer.data(), 14);
  136. bytesInBuffer = 14;
  137. BELL_LOG(info, TAG, "No codec set, reading secret bytes");
  138. if (vectorStartsWith(inputBuffer, this->mp3MagicBytesIdc) ||
  139. vectorStartsWith(inputBuffer, this->mp3MagicBytesUntagged)) {
  140. BELL_LOG(info, TAG, "Detected MP3");
  141. codec = AudioCodec::MP3;
  142. } else if (vectorStartsWith(inputBuffer, this->aacMagicBytes) ||
  143. vectorStartsWith(inputBuffer, this->aacMagicBytes4)) {
  144. BELL_LOG(info, TAG, "Detected AAC");
  145. codec = AudioCodec::AAC;
  146. }
  147. if (codec == AudioCodec::NONE) {
  148. throw std::runtime_error("Codec not supported");
  149. }
  150. }
  151. void EncodedAudioStream::readFully(uint8_t* dst, size_t nbytes) {}