OpusVorbisDecoder.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. MIT License
  3. Copyright (c) 2016 Błażej Szczygieł
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "OpusVorbisDecoder.hpp"
  21. #include <vorbis/codec.h>
  22. #include <opus/opus.h>
  23. #include <string.h>
  24. struct VorbisDecoder
  25. {
  26. vorbis_info info;
  27. vorbis_dsp_state dspState;
  28. vorbis_block block;
  29. ogg_packet op;
  30. bool hasDSPState, hasBlock;
  31. };
  32. /**/
  33. OpusVorbisDecoder::OpusVorbisDecoder(const WebMDemuxer &demuxer) :
  34. m_vorbis(NULL), m_opus(NULL),
  35. m_numSamples(0)
  36. {
  37. switch (demuxer.getAudioCodec())
  38. {
  39. case WebMDemuxer::AUDIO_VORBIS:
  40. m_channels = demuxer.getChannels();
  41. if (openVorbis(demuxer))
  42. return;
  43. break;
  44. case WebMDemuxer::AUDIO_OPUS:
  45. m_channels = demuxer.getChannels();
  46. if (openOpus(demuxer))
  47. return;
  48. break;
  49. default:
  50. return;
  51. }
  52. close();
  53. }
  54. OpusVorbisDecoder::~OpusVorbisDecoder()
  55. {
  56. close();
  57. }
  58. bool OpusVorbisDecoder::isOpen() const
  59. {
  60. return (m_vorbis || m_opus);
  61. }
  62. bool OpusVorbisDecoder::getPCMS16(WebMFrame &frame, short *buffer, int &numOutSamples)
  63. {
  64. if (m_vorbis)
  65. {
  66. m_vorbis->op.packet = frame.buffer;
  67. m_vorbis->op.bytes = frame.bufferSize;
  68. if (vorbis_synthesis(&m_vorbis->block, &m_vorbis->op))
  69. return false;
  70. if (vorbis_synthesis_blockin(&m_vorbis->dspState, &m_vorbis->block))
  71. return false;
  72. const int maxSamples = getBufferSamples();
  73. int samplesCount, count = 0;
  74. float **pcm;
  75. while ((samplesCount = vorbis_synthesis_pcmout(&m_vorbis->dspState, &pcm)))
  76. {
  77. const int toConvert = samplesCount <= maxSamples ? samplesCount : maxSamples;
  78. for (int c = 0; c < m_channels; ++c)
  79. {
  80. float *samples = pcm[c];
  81. for (int i = 0, j = c; i < toConvert; ++i, j += m_channels)
  82. {
  83. int sample = samples[i] * 32767.0f;
  84. if (sample > 32767)
  85. sample = 32767;
  86. else if (sample < -32768)
  87. sample = -32768;
  88. buffer[count + j] = sample;
  89. }
  90. }
  91. vorbis_synthesis_read(&m_vorbis->dspState, toConvert);
  92. count += toConvert;
  93. }
  94. numOutSamples = count;
  95. return true;
  96. }
  97. else if (m_opus)
  98. {
  99. const int samples = opus_decode(m_opus, frame.buffer, frame.bufferSize, buffer, m_numSamples, 0);
  100. if (samples >= 0)
  101. {
  102. numOutSamples = samples;
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. bool OpusVorbisDecoder::openVorbis(const WebMDemuxer &demuxer)
  109. {
  110. size_t extradataSize = 0;
  111. const unsigned char *extradata = demuxer.getAudioExtradata(extradataSize);
  112. if (extradataSize < 3 || !extradata || extradata[0] != 2)
  113. return false;
  114. size_t headerSize[3] = {0};
  115. size_t offset = 1;
  116. /* Calculate three headers sizes */
  117. for (int i = 0; i < 2; ++i)
  118. {
  119. for (;;)
  120. {
  121. if (offset >= extradataSize)
  122. return false;
  123. headerSize[i] += extradata[offset];
  124. if (extradata[offset++] < 0xFF)
  125. break;
  126. }
  127. }
  128. headerSize[2] = extradataSize - (headerSize[0] + headerSize[1] + offset);
  129. if (headerSize[0] + headerSize[1] + headerSize[2] + offset != extradataSize)
  130. return false;
  131. ogg_packet op[3];
  132. memset(op, 0, sizeof op);
  133. op[0].packet = (unsigned char *)extradata + offset;
  134. op[0].bytes = headerSize[0];
  135. op[0].b_o_s = 1;
  136. op[1].packet = (unsigned char *)extradata + offset + headerSize[0];
  137. op[1].bytes = headerSize[1];
  138. op[2].packet = (unsigned char *)extradata + offset + headerSize[0] + headerSize[1];
  139. op[2].bytes = headerSize[2];
  140. m_vorbis = new VorbisDecoder;
  141. m_vorbis->hasDSPState = m_vorbis->hasBlock = false;
  142. vorbis_info_init(&m_vorbis->info);
  143. /* Upload three Vorbis headers into libvorbis */
  144. vorbis_comment vc;
  145. vorbis_comment_init(&vc);
  146. for (int i = 0; i < 3; ++i)
  147. {
  148. if (vorbis_synthesis_headerin(&m_vorbis->info, &vc, &op[i]))
  149. {
  150. vorbis_comment_clear(&vc);
  151. return false;
  152. }
  153. }
  154. vorbis_comment_clear(&vc);
  155. if (vorbis_synthesis_init(&m_vorbis->dspState, &m_vorbis->info))
  156. return false;
  157. m_vorbis->hasDSPState = true;
  158. if (m_vorbis->info.channels != m_channels || m_vorbis->info.rate != demuxer.getSampleRate())
  159. return false;
  160. if (vorbis_block_init(&m_vorbis->dspState, &m_vorbis->block))
  161. return false;
  162. m_vorbis->hasBlock = true;
  163. memset(&m_vorbis->op, 0, sizeof m_vorbis->op);
  164. m_numSamples = 4096 / m_channels;
  165. return true;
  166. }
  167. bool OpusVorbisDecoder::openOpus(const WebMDemuxer &demuxer)
  168. {
  169. int opusErr = 0;
  170. m_opus = opus_decoder_create(demuxer.getSampleRate(), m_channels, &opusErr);
  171. if (!opusErr)
  172. {
  173. m_numSamples = demuxer.getSampleRate() * 0.06 + 0.5; //Maximum frame size (for 60 ms frame)
  174. return true;
  175. }
  176. return false;
  177. }
  178. void OpusVorbisDecoder::close()
  179. {
  180. if (m_vorbis)
  181. {
  182. if (m_vorbis->hasBlock)
  183. vorbis_block_clear(&m_vorbis->block);
  184. if (m_vorbis->hasDSPState)
  185. vorbis_dsp_clear(&m_vorbis->dspState);
  186. vorbis_info_clear(&m_vorbis->info);
  187. delete m_vorbis;
  188. }
  189. if (m_opus)
  190. opus_decoder_destroy(m_opus);
  191. }