VorbisDecoder.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "VorbisDecoder.h"
  2. #include "AudioCodecs.h"
  3. using namespace bell;
  4. extern "C" {
  5. extern vorbis_dsp_state* vorbis_dsp_create(vorbis_info* vi);
  6. extern void vorbis_dsp_destroy(vorbis_dsp_state* v);
  7. extern int vorbis_dsp_restart(vorbis_dsp_state* v);
  8. extern int vorbis_dsp_headerin(vorbis_info* vi, vorbis_comment* vc,
  9. ogg_packet* op);
  10. extern int vorbis_dsp_synthesis(vorbis_dsp_state* vd, ogg_packet* op,
  11. int decodep);
  12. extern int vorbis_dsp_pcmout(vorbis_dsp_state* v, ogg_int16_t* pcm,
  13. int samples);
  14. extern int vorbis_dsp_read(vorbis_dsp_state* v, int samples);
  15. }
  16. #define VORBIS_BUF_SAMPLES 1024
  17. #define VORBIS_BUF_CHANNELS 2
  18. VorbisDecoder::VorbisDecoder() {
  19. vi = new vorbis_info;
  20. vorbis_info_init(vi);
  21. vc = new vorbis_comment;
  22. vorbis_comment_init(vc);
  23. op.packet = new ogg_reference;
  24. op.packet->buffer = new ogg_buffer;
  25. op.packet->buffer->refcount = 0;
  26. op.packet->buffer->ptr.owner = nullptr;
  27. op.packet->buffer->ptr.next = nullptr;
  28. op.packet->begin = 0;
  29. op.packet->next = nullptr;
  30. op.granulepos = -1;
  31. op.packetno = 10;
  32. pcmData = (int16_t*)malloc(VORBIS_BUF_SAMPLES * VORBIS_BUF_CHANNELS *
  33. sizeof(uint16_t));
  34. }
  35. VorbisDecoder::~VorbisDecoder() {
  36. vorbis_info_clear(vi);
  37. vorbis_comment_clear(vc);
  38. if (vd)
  39. vorbis_dsp_destroy(vd);
  40. vd = nullptr;
  41. free(pcmData);
  42. }
  43. bool VorbisDecoder::setup(AudioContainer* container) {
  44. /*
  45. uint32_t setupLen;
  46. uint8_t* setup = container->getSetupData(setupLen, AudioCodec::VORBIS);
  47. if (!setup)
  48. return false;
  49. op.b_o_s = true; // mark this page as beginning of stream
  50. uint32_t bytesLeft = setupLen - 1; // minus header count length (8 bit)
  51. std::vector<uint32_t> headers(setup[0]);
  52. for (uint8_t i = 0; i < setup[0]; i++) {
  53. uint8_t* sizeByte = (uint8_t*)setup + 1 + i;
  54. headers[i] = 0;
  55. while (*sizeByte == 255) {
  56. headers[i] += *(sizeByte++);
  57. bytesLeft--;
  58. }
  59. headers[i] += *sizeByte;
  60. bytesLeft--;
  61. }
  62. // parse all headers from the setup data
  63. for (const auto& headerSize : headers) {
  64. setPacket(setup + setupLen - bytesLeft, headerSize);
  65. bytesLeft -= headerSize;
  66. lastErrno = vorbis_dsp_headerin(vi, vc, &op);
  67. if (lastErrno < 0) {
  68. bytesLeft = 0;
  69. break;
  70. }
  71. }
  72. // parse last header, not present in header table (seems to happen for MP4 containers)
  73. if (bytesLeft) {
  74. setPacket(setup + setupLen - bytesLeft, bytesLeft);
  75. lastErrno = vorbis_dsp_headerin(vi, vc, &op);
  76. }
  77. // disable BOS to allow reading audio data
  78. op.b_o_s = false;
  79. // set up the codec
  80. if (vd)
  81. vorbis_dsp_restart(vd);
  82. else
  83. vd = vorbis_dsp_create(vi);
  84. return !lastErrno;
  85. */
  86. return false;
  87. }
  88. bool VorbisDecoder::setup(uint32_t sampleRate, uint8_t channelCount,
  89. uint8_t bitDepth) {
  90. // manual setup is not allowed
  91. return false;
  92. }
  93. uint8_t* VorbisDecoder::decode(uint8_t* inData, uint32_t& inLen,
  94. uint32_t& outLen) {
  95. if (!inData || !vi)
  96. return nullptr;
  97. setPacket(inData, inLen);
  98. // sources:
  99. // - vorbisfile.c:556
  100. // - vorbisfile.c:1557
  101. lastErrno = vorbis_dsp_synthesis(vd, &op, 1);
  102. if (lastErrno < 0)
  103. return nullptr;
  104. int samples = vorbis_dsp_pcmout(vd, pcmData, VORBIS_BUF_SAMPLES);
  105. outLen = samples;
  106. if (samples) {
  107. if (samples > 0) {
  108. vorbis_dsp_read(vd, samples);
  109. outLen = samples * 2 * vi->channels;
  110. }
  111. }
  112. inLen = 0;
  113. return (uint8_t*)pcmData;
  114. }
  115. void VorbisDecoder::setPacket(uint8_t* inData, uint32_t inLen) const {
  116. op.packet->buffer->data = static_cast<unsigned char*>(inData);
  117. op.packet->buffer->size = static_cast<long>(inLen);
  118. op.packet->length = static_cast<long>(inLen);
  119. }