OpusDecoder.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // libtgvoip is free and unencumbered public domain software.
  3. // For more information, see http://unlicense.org or the UNLICENSE file
  4. // you should have received with this source code distribution.
  5. //
  6. #include "OpusDecoder.h"
  7. #include "EchoCanceller.h"
  8. #include "audio/Resampler.h"
  9. #include "logging.h"
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <algorithm>
  13. #if TGVOIP_INCLUDE_OPUS_PACKAGE
  14. #include <opus/opus.h>
  15. #else
  16. #include <opus.h>
  17. #endif
  18. #include "VoIPController.h"
  19. #define PACKET_SIZE (960*2)
  20. using namespace tgvoip;
  21. tgvoip::OpusDecoder::OpusDecoder(const std::shared_ptr<MediaStreamItf>& dst, bool isAsync, bool needEC){
  22. dst->SetCallback(OpusDecoder::Callback, this);
  23. Initialize(isAsync, needEC);
  24. }
  25. tgvoip::OpusDecoder::OpusDecoder(const std::unique_ptr<MediaStreamItf>& dst, bool isAsync, bool needEC){
  26. dst->SetCallback(OpusDecoder::Callback, this);
  27. Initialize(isAsync, needEC);
  28. }
  29. tgvoip::OpusDecoder::OpusDecoder(MediaStreamItf* dst, bool isAsync, bool needEC){
  30. dst->SetCallback(OpusDecoder::Callback, this);
  31. Initialize(isAsync, needEC);
  32. }
  33. void tgvoip::OpusDecoder::Initialize(bool isAsync, bool needEC){
  34. async=isAsync;
  35. if(async){
  36. decodedQueue=new BlockingQueue<unsigned char*>(33);
  37. bufferPool=new BufferPool(PACKET_SIZE, 32);
  38. semaphore=new Semaphore(32, 0);
  39. }else{
  40. decodedQueue=NULL;
  41. bufferPool=NULL;
  42. semaphore=NULL;
  43. }
  44. dec=opus_decoder_create(48000, 1, NULL);
  45. if(needEC)
  46. ecDec=opus_decoder_create(48000, 1, NULL);
  47. else
  48. ecDec=NULL;
  49. buffer=(unsigned char *) malloc(8192);
  50. lastDecoded=NULL;
  51. outputBufferSize=0;
  52. echoCanceller=NULL;
  53. frameDuration=20;
  54. consecutiveLostPackets=0;
  55. enableDTX=false;
  56. silentPacketCount=0;
  57. levelMeter=NULL;
  58. nextLen=0;
  59. running=false;
  60. remainingDataLen=0;
  61. processedBuffer=NULL;
  62. prevWasEC=false;
  63. }
  64. tgvoip::OpusDecoder::~OpusDecoder(){
  65. opus_decoder_destroy(dec);
  66. if(ecDec)
  67. opus_decoder_destroy(ecDec);
  68. free(buffer);
  69. if(bufferPool)
  70. delete bufferPool;
  71. if(decodedQueue)
  72. delete decodedQueue;
  73. if(semaphore)
  74. delete semaphore;
  75. }
  76. void tgvoip::OpusDecoder::SetEchoCanceller(EchoCanceller* canceller){
  77. echoCanceller=canceller;
  78. }
  79. size_t tgvoip::OpusDecoder::Callback(unsigned char *data, size_t len, void *param){
  80. return ((OpusDecoder*)param)->HandleCallback(data, len);
  81. }
  82. size_t tgvoip::OpusDecoder::HandleCallback(unsigned char *data, size_t len){
  83. if(async){
  84. if(!running){
  85. memset(data, 0, len);
  86. return 0;
  87. }
  88. if(outputBufferSize==0){
  89. outputBufferSize=len;
  90. int packetsNeeded;
  91. if(len>PACKET_SIZE)
  92. packetsNeeded=len/PACKET_SIZE;
  93. else
  94. packetsNeeded=1;
  95. packetsNeeded*=2;
  96. semaphore->Release(packetsNeeded);
  97. }
  98. assert(outputBufferSize==len && "output buffer size is supposed to be the same throughout callbacks");
  99. if(len==PACKET_SIZE){
  100. lastDecoded=(unsigned char *) decodedQueue->GetBlocking();
  101. if(!lastDecoded)
  102. return 0;
  103. memcpy(data, lastDecoded, PACKET_SIZE);
  104. bufferPool->Reuse(lastDecoded);
  105. semaphore->Release();
  106. if(silentPacketCount>0){
  107. silentPacketCount--;
  108. if(levelMeter)
  109. levelMeter->Update(reinterpret_cast<int16_t *>(data), 0);
  110. return 0;
  111. }
  112. if(echoCanceller){
  113. echoCanceller->SpeakerOutCallback(data, PACKET_SIZE);
  114. }
  115. }else{
  116. LOGE("Opus decoder buffer length != 960 samples");
  117. abort();
  118. }
  119. }else{
  120. if(remainingDataLen==0 && silentPacketCount==0){
  121. int duration=DecodeNextFrame();
  122. remainingDataLen=(size_t) (duration/20*960*2);
  123. }
  124. if(silentPacketCount>0 || remainingDataLen==0 || !processedBuffer){
  125. if(silentPacketCount>0)
  126. silentPacketCount--;
  127. memset(data, 0, 960*2);
  128. if(levelMeter)
  129. levelMeter->Update(reinterpret_cast<int16_t *>(data), 0);
  130. return 0;
  131. }
  132. memcpy(data, processedBuffer, 960*2);
  133. remainingDataLen-=960*2;
  134. if(remainingDataLen>0){
  135. memmove(processedBuffer, processedBuffer+960*2, remainingDataLen);
  136. }
  137. }
  138. if(levelMeter)
  139. levelMeter->Update(reinterpret_cast<int16_t *>(data), len/2);
  140. return len;
  141. }
  142. void tgvoip::OpusDecoder::Start(){
  143. if(!async)
  144. return;
  145. running=true;
  146. thread=new Thread(std::bind(&tgvoip::OpusDecoder::RunThread, this));
  147. thread->SetName("opus_decoder");
  148. thread->SetMaxPriority();
  149. thread->Start();
  150. }
  151. void tgvoip::OpusDecoder::Stop(){
  152. if(!running || !async)
  153. return;
  154. running=false;
  155. semaphore->Release();
  156. thread->Join();
  157. delete thread;
  158. }
  159. void tgvoip::OpusDecoder::RunThread(){
  160. int i;
  161. LOGI("decoder: packets per frame %d", packetsPerFrame);
  162. while(running){
  163. int playbackDuration=DecodeNextFrame();
  164. for(i=0;i<playbackDuration/20;i++){
  165. semaphore->Acquire();
  166. if(!running){
  167. LOGI("==== decoder exiting ====");
  168. return;
  169. }
  170. unsigned char *buf=bufferPool->Get();
  171. if(buf){
  172. if(remainingDataLen>0){
  173. for(effects::AudioEffect*& effect:postProcEffects){
  174. effect->Process(reinterpret_cast<int16_t*>(processedBuffer+(PACKET_SIZE*i)), 960);
  175. }
  176. memcpy(buf, processedBuffer+(PACKET_SIZE*i), PACKET_SIZE);
  177. }else{
  178. //LOGE("Error decoding, result=%d", size);
  179. memset(buf, 0, PACKET_SIZE);
  180. }
  181. decodedQueue->Put(buf);
  182. }else{
  183. LOGW("decoder: no buffers left!");
  184. }
  185. }
  186. }
  187. }
  188. int tgvoip::OpusDecoder::DecodeNextFrame(){
  189. int playbackDuration=0;
  190. bool isEC=false;
  191. size_t len=jitterBuffer->HandleOutput(buffer, 8192, 0, true, playbackDuration, isEC);
  192. bool fec=false;
  193. if(!len){
  194. fec=true;
  195. len=jitterBuffer->HandleOutput(buffer, 8192, 0, false, playbackDuration, isEC);
  196. //if(len)
  197. // LOGV("Trying FEC...");
  198. }
  199. int size;
  200. if(len){
  201. size=opus_decode(isEC ? ecDec : dec, buffer, len, (opus_int16 *) decodeBuffer, packetsPerFrame*960, fec ? 1 : 0);
  202. consecutiveLostPackets=0;
  203. if(prevWasEC!=isEC && size){
  204. // It turns out the waveforms generated by the PLC feature are also great to help smooth out the
  205. // otherwise audible transition between the frames from different decoders. Those are basically an extrapolation
  206. // of the previous successfully decoded data -- which is exactly what we need here.
  207. size=opus_decode(prevWasEC ? ecDec : dec, NULL, 0, (opus_int16*)nextBuffer, packetsPerFrame*960, 0);
  208. if(size){
  209. int16_t* plcSamples=reinterpret_cast<int16_t*>(nextBuffer);
  210. int16_t* samples=reinterpret_cast<int16_t*>(decodeBuffer);
  211. constexpr float coeffs[]={0.999802, 0.995062, 0.984031, 0.966778, 0.943413, 0.914084, 0.878975, 0.838309, 0.792344,
  212. 0.741368, 0.685706, 0.625708, 0.561754, 0.494249, 0.423619, 0.350311, 0.274788, 0.197527, 0.119018, 0.039757};
  213. for(int i=0;i<20;i++){
  214. samples[i]=(int16_t)round((plcSamples[i]*coeffs[i]+(float)samples[i]*(1.0-coeffs[i])));
  215. }
  216. }
  217. }
  218. prevWasEC=isEC;
  219. }else{ // do packet loss concealment
  220. consecutiveLostPackets++;
  221. if(consecutiveLostPackets>2 && enableDTX){
  222. silentPacketCount+=packetsPerFrame;
  223. size=packetsPerFrame*960;
  224. }else{
  225. size=opus_decode(prevWasEC ? ecDec : dec, NULL, 0, (opus_int16 *) decodeBuffer, packetsPerFrame*960, 0);
  226. //LOGV("PLC");
  227. }
  228. }
  229. if(size<0)
  230. LOGW("decoder: opus_decode error %d", size);
  231. remainingDataLen=size;
  232. if(playbackDuration==80){
  233. processedBuffer=buffer;
  234. audio::Resampler::Rescale60To80((int16_t*) decodeBuffer, (int16_t*) processedBuffer);
  235. }else if(playbackDuration==40){
  236. processedBuffer=buffer;
  237. audio::Resampler::Rescale60To40((int16_t*) decodeBuffer, (int16_t*) processedBuffer);
  238. }else{
  239. processedBuffer=decodeBuffer;
  240. }
  241. return playbackDuration;
  242. }
  243. void tgvoip::OpusDecoder::SetFrameDuration(uint32_t duration){
  244. frameDuration=duration;
  245. packetsPerFrame=frameDuration/20;
  246. }
  247. void tgvoip::OpusDecoder::SetJitterBuffer(std::shared_ptr<JitterBuffer> jitterBuffer){
  248. this->jitterBuffer=jitterBuffer;
  249. }
  250. void tgvoip::OpusDecoder::SetDTX(bool enable){
  251. enableDTX=enable;
  252. }
  253. void tgvoip::OpusDecoder::SetLevelMeter(AudioLevelMeter *levelMeter){
  254. this->levelMeter=levelMeter;
  255. }
  256. void tgvoip::OpusDecoder::AddAudioEffect(effects::AudioEffect *effect){
  257. postProcEffects.push_back(effect);
  258. }
  259. void tgvoip::OpusDecoder::RemoveAudioEffect(effects::AudioEffect *effect){
  260. std::vector<effects::AudioEffect*>::iterator i=std::find(postProcEffects.begin(), postProcEffects.end(), effect);
  261. if(i!=postProcEffects.end())
  262. postProcEffects.erase(i);
  263. }