OPUSDecoder.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "OPUSDecoder.h"
  2. #include "opus.h"
  3. using namespace bell;
  4. #define MAX_FRAME_SIZE 6 * 960
  5. #define MAX_CHANNELS 2
  6. // dummy structure, just to get access to channels
  7. struct OpusDecoder {
  8. int dummy1;
  9. int dummy2;
  10. int channels;
  11. };
  12. OPUSDecoder::OPUSDecoder() {
  13. opus = nullptr;
  14. pcmData = (int16_t*)malloc(MAX_FRAME_SIZE * MAX_CHANNELS * sizeof(int16_t));
  15. }
  16. OPUSDecoder::~OPUSDecoder() {
  17. if (opus)
  18. opus_decoder_destroy(opus);
  19. free(pcmData);
  20. }
  21. bool OPUSDecoder::setup(uint32_t sampleRate, uint8_t channelCount,
  22. uint8_t bitDepth) {
  23. if (opus)
  24. opus_decoder_destroy(opus);
  25. opus = opus_decoder_create((int32_t)sampleRate, channelCount, &lastErrno);
  26. return !lastErrno;
  27. }
  28. uint8_t* OPUSDecoder::decode(uint8_t* inData, uint32_t& inLen,
  29. uint32_t& outLen) {
  30. if (!inData)
  31. return nullptr;
  32. outLen =
  33. opus_decode(opus, static_cast<unsigned char*>(inData),
  34. static_cast<int32_t>(inLen), pcmData, MAX_FRAME_SIZE, false);
  35. outLen *= opus->channels * sizeof(int16_t);
  36. inLen = 0;
  37. return (uint8_t*)pcmData;
  38. }