OPUSDecoder.cpp 1.2 KB

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