CUOpusCoder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include "CUOpusCoder.h"
  2. #include "CoreMinimal.h"
  3. #define DEBUG_OPUS_LOG 0
  4. FCUOpusCoder::FCUOpusCoder()
  5. {
  6. #if WITH_OPUS
  7. Encoder = nullptr;
  8. Decoder = nullptr;
  9. #endif
  10. SampleRate = 16000;
  11. Channels = 1;
  12. BitRate = 24000;
  13. MaxPacketSize = (3 * 1276);
  14. SetFrameSizeMs(60);
  15. bResetBetweenEncoding = true;
  16. bApplicationVoip = true;
  17. bLowestPossibleLatency = false;
  18. }
  19. FCUOpusCoder::~FCUOpusCoder()
  20. {
  21. #if WITH_OPUS
  22. if (Encoder)
  23. {
  24. opus_encoder_destroy(Encoder);
  25. Encoder = nullptr;
  26. }
  27. if (Decoder)
  28. {
  29. opus_decoder_destroy(Decoder);
  30. Decoder = nullptr;
  31. }
  32. #endif
  33. }
  34. void FCUOpusCoder::SetSampleRate(int32 InSamplesPerSec)
  35. {
  36. SampleRate = InSamplesPerSec;
  37. SetFrameSizeMs(FrameSizeMs);
  38. ResetCoderIfInitialized();
  39. }
  40. void FCUOpusCoder::SetChannels(int32 InChannels)
  41. {
  42. Channels = InChannels;
  43. ResetCoderIfInitialized();
  44. }
  45. void FCUOpusCoder::SetBitrate(int32 InBitrate)
  46. {
  47. BitRate = InBitrate;
  48. ResetCoderIfInitialized();
  49. }
  50. void FCUOpusCoder::SetFrameSizeMs(int32 Ms)
  51. {
  52. FrameSizeMs = Ms;
  53. FrameSize = (SampleRate * FrameSizeMs) / 1000;
  54. MaxFrameSize = FrameSize * 6;
  55. ResetCoderIfInitialized();
  56. }
  57. bool FCUOpusCoder::EncodeStream(const TArray<uint8>& InPCMBytes, FCUOpusMinimalStream& OutStream)
  58. {
  59. if (bResetBetweenEncoding)
  60. {
  61. ResetCoderIfInitialized();
  62. }
  63. if (!InitEncoderIfNeeded())
  64. {
  65. return false;
  66. }
  67. #if WITH_OPUS
  68. #if DEBUG_OPUS_LOG
  69. DebugLogEncoder();
  70. #endif
  71. int32 BytesLeft = InPCMBytes.Num();
  72. int32 Offset = 0;
  73. int32 BytesWritten = 0;
  74. const int32 BytesPerFrame = FrameSize * Channels * sizeof(opus_int16);
  75. TArray<uint8> TempBuffer;
  76. TArray<uint8> FinalPacket;
  77. TempBuffer.SetNumUninitialized(MaxPacketSize);
  78. int32 EncodedBytes = 0;
  79. opus_int16* PCMDataPtr = 0;
  80. while (BytesLeft > 0)
  81. {
  82. //Final packet requires zero padding
  83. if(BytesLeft<BytesPerFrame)
  84. {
  85. FinalPacket.Append(InPCMBytes.GetData() + Offset, BytesLeft);
  86. FinalPacket.AddZeroed(BytesPerFrame - BytesLeft);
  87. PCMDataPtr = (opus_int16*)FinalPacket.GetData();
  88. }
  89. else
  90. {
  91. PCMDataPtr = (opus_int16*)(InPCMBytes.GetData() + Offset);
  92. }
  93. EncodedBytes = opus_encode(Encoder, (const opus_int16*)PCMDataPtr, FrameSize, TempBuffer.GetData(), MaxPacketSize);
  94. #if DEBUG_OPUS_LOG
  95. DebugLogFrame(TempBuffer.GetData(), EncodedBytes, SampleRate, true);
  96. #endif
  97. if (EncodedBytes < 0)
  98. {
  99. UE_LOG(LogTemp, Warning, TEXT("opus_encode err: %s"), opus_strerror(EncodedBytes));
  100. return false;
  101. }
  102. OutStream.CompressedBytes.Append(TempBuffer.GetData(), EncodedBytes);
  103. OutStream.PacketSizes.Add(EncodedBytes);
  104. Offset += BytesPerFrame;
  105. BytesLeft -= BytesPerFrame;
  106. BytesWritten += EncodedBytes;
  107. }
  108. #if DEBUG_OPUS_LOG
  109. UE_LOG(LogTemp, Log, TEXT("Total packets encoded: %d, total bytes: %d=%d"), OutStream.PacketSizes.Num(), BytesWritten, OutStream.CompressedBytes.Num());
  110. #endif
  111. #endif //with opus
  112. return true;
  113. }
  114. bool FCUOpusCoder::DecodeStream(const FCUOpusMinimalStream& InStream, TArray<uint8>& OutPCMFrame)
  115. {
  116. if (!InitDecoderIfNeeded())
  117. {
  118. return false;
  119. }
  120. #if WITH_OPUS
  121. #if DEBUG_OPUS_LOG
  122. DebugLogDecoder();
  123. #endif
  124. const int32 BytesPerFrame = FrameSize * Channels * sizeof(opus_int16);
  125. TArray<uint8> TempBuffer;
  126. TempBuffer.SetNum(MaxFrameSize);
  127. int32 CompressedOffset = 0;
  128. for (int FrameIndex = 0; CompressedOffset < InStream.CompressedBytes.Num(); FrameIndex++)
  129. {
  130. int32 DecodedSamples = opus_decode(Decoder, InStream.CompressedBytes.GetData() + CompressedOffset, InStream.PacketSizes[FrameIndex], (opus_int16*)TempBuffer.GetData(), FrameSize, 0);
  131. #if DEBUG_OPUS_LOG
  132. DebugLogFrame(InStream.CompressedBytes.GetData(), InStream.PacketSizes[FrameIndex], SampleRate, false);
  133. UE_LOG(LogTemp, Log, TEXT("Decoded Samples: %d"), DecodedSamples);
  134. #endif
  135. if (DecodedSamples > 0)
  136. {
  137. OutPCMFrame.Append(TempBuffer.GetData(), DecodedSamples*Channels*sizeof(opus_int16));
  138. }
  139. else if (DecodedSamples < 0)
  140. {
  141. UE_LOG(LogTemp, Log, TEXT("%s"), opus_strerror(DecodedSamples));
  142. return false;
  143. }
  144. CompressedOffset += InStream.PacketSizes[FrameIndex];
  145. }
  146. #if DEBUG_OPUS_LOG
  147. UE_LOG(LogTemp, Log, TEXT("decoded into %d bytes"), OutPCMFrame.Num());
  148. #endif
  149. #endif
  150. return true;
  151. }
  152. static void WriteUInt32ToByteArrayLE(TArray<uint8>& InByteArray, int32& Index, const uint32 Value)
  153. {
  154. InByteArray[Index++] = (uint8)(Value >> 0);
  155. InByteArray[Index++] = (uint8)(Value >> 8);
  156. InByteArray[Index++] = (uint8)(Value >> 16);
  157. InByteArray[Index++] = (uint8)(Value >> 24);
  158. }
  159. bool FCUOpusCoder::SerializeMinimal(const FCUOpusMinimalStream& InStream, TArray<uint8>& OutSerializedBytes)
  160. {
  161. //Preset array size
  162. OutSerializedBytes.SetNumUninitialized(sizeof(int32) + (InStream.PacketSizes.Num() * sizeof(int16)) + InStream.CompressedBytes.Num());
  163. //Write total number of packets as int32 first
  164. int32 Index = 0;
  165. WriteUInt32ToByteArrayLE(OutSerializedBytes, Index, InStream.PacketSizes.Num());
  166. //write the compressed frame sizes
  167. int32 Offset = sizeof(int32);
  168. FMemory::Memcpy(&OutSerializedBytes[Offset], InStream.PacketSizes.GetData(), InStream.PacketSizes.Num() * sizeof(int16));
  169. //write the compressed bytes
  170. Offset += (InStream.PacketSizes.Num() * sizeof(int16));
  171. FMemory::Memcpy(&OutSerializedBytes[Offset], InStream.CompressedBytes.GetData(), InStream.CompressedBytes.Num());
  172. return true;
  173. }
  174. bool FCUOpusCoder::DeserializeMinimal(const TArray<uint8>& InSerializedMinimalBytes, FCUOpusMinimalStream& OutStream)
  175. {
  176. int32 PacketCount = InSerializedMinimalBytes[0];
  177. //get our packet info
  178. OutStream.PacketSizes.SetNumUninitialized(PacketCount);
  179. int32 Offset = sizeof(int32);
  180. FMemory::Memcpy(OutStream.PacketSizes.GetData(), &InSerializedMinimalBytes[Offset], PacketCount*sizeof(int16));
  181. //get our compressed data
  182. Offset += ((PacketCount) * sizeof(int16));
  183. int32 RemainingBytes = InSerializedMinimalBytes.Num() - Offset;
  184. OutStream.CompressedBytes.Append(&InSerializedMinimalBytes[Offset], RemainingBytes);
  185. return true;
  186. }
  187. int32 FCUOpusCoder::EncodeFrame(const TArray<uint8>& InPCMFrame, TArray<uint8>& OutCompressed)
  188. {
  189. #if WITH_OPUS
  190. return opus_encode(Encoder, (const opus_int16*)InPCMFrame.GetData(), FrameSize, OutCompressed.GetData(), MaxPacketSize);
  191. #endif
  192. return 0;
  193. }
  194. int32 FCUOpusCoder::DecodeFrame(const TArray<uint8>& InCompressedFrame, TArray<uint8>& OutPCMFrame)
  195. {
  196. #if WITH_OPUS
  197. return opus_decode(Decoder, InCompressedFrame.GetData(), InCompressedFrame.Num(), (opus_int16*)OutPCMFrame.GetData(), FrameSize, 0);
  198. #endif
  199. return 0;
  200. }
  201. bool FCUOpusCoder::InitEncoderIfNeeded()
  202. {
  203. #if WITH_OPUS
  204. if (!Encoder)
  205. {
  206. int32 ErrorCode;
  207. if (!Encoder)
  208. {
  209. int32 ApplicationCode = OPUS_APPLICATION_AUDIO;
  210. if (bApplicationVoip)
  211. {
  212. ApplicationCode = OPUS_APPLICATION_VOIP;
  213. }
  214. if (bLowestPossibleLatency)
  215. {
  216. ApplicationCode = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
  217. }
  218. Encoder = opus_encoder_create(SampleRate, Channels, ApplicationCode, &ErrorCode);
  219. if (ErrorCode < 0)
  220. {
  221. UE_LOG(LogTemp, Warning, TEXT("opus_encoder_create err: %d"), ErrorCode);
  222. return false;
  223. }
  224. //Turn on some settings
  225. //opus_encoder_ctl(Encoder, OPUS_SET_BITRATE(BitRate));
  226. /*opus_encoder_ctl(Encoder, OPUS_SET_VBR(1)); //variable bit rate encoding
  227. opus_encoder_ctl(Encoder, OPUS_SET_VBR_CONSTRAINT(0)); //constrained VBR
  228. opus_encoder_ctl(Encoder, OPUS_SET_COMPLEXITY(1)); //complexity
  229. opus_encoder_ctl(Encoder, OPUS_SET_INBAND_FEC(0)); //forward error correction
  230. */
  231. }
  232. }
  233. #endif
  234. return true;
  235. }
  236. bool FCUOpusCoder::InitDecoderIfNeeded()
  237. {
  238. #if WITH_OPUS
  239. if (!Decoder)
  240. {
  241. int32 ErrorCode;
  242. Decoder = opus_decoder_create(SampleRate, Channels, &ErrorCode);
  243. if (ErrorCode < 0)
  244. {
  245. UE_LOG(LogTemp, Warning, TEXT("opus_decoder_create err: %d"), ErrorCode);
  246. return false;
  247. }
  248. }
  249. #endif
  250. return true;
  251. }
  252. void FCUOpusCoder::ResetCoderIfInitialized()
  253. {
  254. #if WITH_OPUS
  255. if (Encoder)
  256. {
  257. opus_encoder_destroy(Encoder);
  258. Encoder = nullptr;
  259. InitEncoderIfNeeded();
  260. }
  261. if (Decoder)
  262. {
  263. opus_decoder_destroy(Decoder);
  264. Decoder = nullptr;
  265. InitDecoderIfNeeded();
  266. }
  267. #endif
  268. }
  269. //Debug utilities
  270. void FCUOpusCoder::DebugLogEncoder()
  271. {
  272. #if WITH_OPUS
  273. int32 ErrCode = 0;
  274. int32 BitRateLocal = 0;
  275. int32 Vbr = 0;
  276. int32 SampleRateLocal = 0;
  277. int32 Application = 0;
  278. int32 Signal = 0;
  279. int32 Complexity = 0;
  280. ErrCode = opus_encoder_ctl(Encoder, OPUS_GET_BITRATE(&BitRateLocal));
  281. ErrCode = opus_encoder_ctl(Encoder, OPUS_GET_VBR(&Vbr));
  282. ErrCode = opus_encoder_ctl(Encoder, OPUS_GET_SAMPLE_RATE(&SampleRateLocal));
  283. ErrCode = opus_encoder_ctl(Encoder, OPUS_GET_APPLICATION(&Application));
  284. ErrCode = opus_encoder_ctl(Encoder, OPUS_GET_SIGNAL(&Signal));
  285. ErrCode = opus_encoder_ctl(Encoder, OPUS_GET_COMPLEXITY(&Complexity));
  286. UE_LOG(LogTemp, Log, TEXT("Opus Encoder Details"));
  287. UE_LOG(LogTemp, Log, TEXT("- Application: %d"), Application);
  288. UE_LOG(LogTemp, Log, TEXT("- Signal: %d"), Signal);
  289. UE_LOG(LogTemp, Log, TEXT("- BitRate: %d"), BitRateLocal);
  290. UE_LOG(LogTemp, Log, TEXT("- SampleRate: %d"), SampleRateLocal);
  291. UE_LOG(LogTemp, Log, TEXT("- Vbr: %d"), Vbr);
  292. UE_LOG(LogTemp, Log, TEXT("- Complexity: %d"), Complexity);
  293. #endif
  294. }
  295. void FCUOpusCoder::DebugLogDecoder()
  296. {
  297. #if WITH_OPUS
  298. int32 ErrCode = 0;
  299. int32 Gain = 0;
  300. int32 Pitch = 0;
  301. ErrCode = opus_decoder_ctl(Decoder, OPUS_GET_GAIN(&Gain));
  302. ErrCode = opus_decoder_ctl(Decoder, OPUS_GET_PITCH(&Pitch));
  303. UE_LOG(LogTemp, Log, TEXT("Opus Decoder Details"));
  304. UE_LOG(LogTemp, Log, TEXT("- Gain: %d"), Gain);
  305. UE_LOG(LogTemp, Log, TEXT("- Pitch: %d"), Pitch);
  306. #endif
  307. }
  308. void FCUOpusCoder::DebugLogFrame(const uint8* PacketData, uint32 PacketLength, uint32 InSampleRate, bool bEncode)
  309. {
  310. #if WITH_OPUS
  311. // Frame Encoding see http://tools.ietf.org/html/rfc6716#section-3.1
  312. int32 NumFrames = opus_packet_get_nb_frames(PacketData, PacketLength);
  313. if (NumFrames == OPUS_BAD_ARG || NumFrames == OPUS_INVALID_PACKET)
  314. {
  315. UE_LOG(LogTemp, Warning, TEXT("opus_packet_get_nb_frames: Invalid voice packet data!"));
  316. }
  317. int32 NumSamples = opus_packet_get_nb_samples(PacketData, PacketLength, InSampleRate);
  318. if (NumSamples == OPUS_BAD_ARG || NumSamples == OPUS_INVALID_PACKET)
  319. {
  320. UE_LOG(LogTemp, Warning, TEXT("opus_packet_get_nb_samples: Invalid voice packet data!"));
  321. }
  322. int32 NumSamplesPerFrame = opus_packet_get_samples_per_frame(PacketData, InSampleRate);
  323. int32 Bandwidth = opus_packet_get_bandwidth(PacketData);
  324. const TCHAR* BandwidthStr = nullptr;
  325. switch (Bandwidth)
  326. {
  327. case OPUS_BANDWIDTH_NARROWBAND: // Narrowband (4kHz bandpass)
  328. BandwidthStr = TEXT("NB");
  329. break;
  330. case OPUS_BANDWIDTH_MEDIUMBAND: // Mediumband (6kHz bandpass)
  331. BandwidthStr = TEXT("MB");
  332. break;
  333. case OPUS_BANDWIDTH_WIDEBAND: // Wideband (8kHz bandpass)
  334. BandwidthStr = TEXT("WB");
  335. break;
  336. case OPUS_BANDWIDTH_SUPERWIDEBAND: // Superwideband (12kHz bandpass)
  337. BandwidthStr = TEXT("SWB");
  338. break;
  339. case OPUS_BANDWIDTH_FULLBAND: // Fullband (20kHz bandpass)
  340. BandwidthStr = TEXT("FB");
  341. break;
  342. case OPUS_INVALID_PACKET:
  343. default:
  344. BandwidthStr = TEXT("Invalid");
  345. break;
  346. }
  347. uint8 TOC = 0;
  348. const uint8* frames[48];
  349. int16 size[48];
  350. int32 payload_offset = 0;
  351. int32 NumFramesParsed = opus_packet_parse(PacketData, PacketLength, &TOC, frames, size, &payload_offset);
  352. int32 TOCEncoding = (TOC & 0xf8) >> 3;
  353. bool TOCStereo = (TOC & 0x4) != 0 ? true : false;
  354. int32 TOCMode = TOC & 0x3;
  355. if (bEncode)
  356. {
  357. UE_LOG(LogTemp, Log, TEXT("PacketLength: %d NumFrames: %d NumSamples: %d Bandwidth: %s Encoding: %d Stereo: %d FrameDesc: %d"),
  358. PacketLength, NumFrames, NumSamples, BandwidthStr, TOCEncoding, TOCStereo, TOCMode);
  359. }
  360. else
  361. {
  362. UE_LOG(LogTemp, Log, TEXT("PacketLength: %d NumFrames: %d NumSamples: %d Bandwidth: %s Encoding: %d Stereo: %d FrameDesc: %d"),
  363. PacketLength, NumFrames, NumSamples, BandwidthStr, TOCEncoding, TOCStereo, TOCMode);
  364. }
  365. #endif
  366. }