2
0

AACContainer.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "AACContainer.h"
  2. #include <cstring> // for memmove
  3. #include "StreamInfo.h" // for BitWidth, BitWidth::BW_16, SampleRate, Sampl...
  4. #include "aacdec.h" // for AACFindSyncWord
  5. using namespace bell;
  6. #define SYNC_WORLD_LEN 4
  7. AACContainer::AACContainer(std::istream& istr) : bell::AudioContainer(istr) {}
  8. bool AACContainer::fillBuffer() {
  9. if (this->bytesInBuffer < AAC_MAX_FRAME_SIZE * 2) {
  10. this->istr.read((char*)buffer.data() + bytesInBuffer,
  11. buffer.size() - bytesInBuffer);
  12. this->bytesInBuffer += istr.gcount();
  13. }
  14. return this->bytesInBuffer >= AAC_MAX_FRAME_SIZE * 2;
  15. }
  16. std::byte* AACContainer::readSample(uint32_t& len) {
  17. if (!this->fillBuffer()) {
  18. len = 0;
  19. return nullptr;
  20. }
  21. // Align the data if previous read was offseted
  22. if (toConsume > 0 && toConsume <= bytesInBuffer) {
  23. memmove(buffer.data(), buffer.data() + toConsume,
  24. buffer.size() - toConsume);
  25. bytesInBuffer = bytesInBuffer - toConsume;
  26. toConsume = 0;
  27. }
  28. int startOffset =
  29. AACFindSyncWord((uint8_t*)this->buffer.data(), bytesInBuffer);
  30. if (startOffset < 0) {
  31. // Discard word
  32. toConsume = AAC_MAX_FRAME_SIZE;
  33. return nullptr;
  34. }
  35. len = bytesInBuffer - startOffset;
  36. return this->buffer.data() + startOffset;
  37. }
  38. void AACContainer::parseSetupData() {
  39. channels = 2;
  40. sampleRate = bell::SampleRate::SR_44100;
  41. bitWidth = bell::BitWidth::BW_16;
  42. }