SampleFormatOpus.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * SampleFormatOpus.cpp
  3. * --------------------
  4. * Purpose: Opus sample import.
  5. * Notes :
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "Sndfile.h"
  11. #ifndef MODPLUG_NO_FILESAVE
  12. #include "../common/mptFileIO.h"
  13. #endif
  14. #include "../common/misc_util.h"
  15. #include "Tagging.h"
  16. #include "Loaders.h"
  17. #include "../common/FileReader.h"
  18. #include "modsmp_ctrl.h"
  19. #include "openmpt/soundbase/Copy.hpp"
  20. #include "../soundlib/ModSampleCopy.h"
  21. //#include "mpt/crc/crc.hpp"
  22. #include "OggStream.h"
  23. #ifdef MPT_WITH_OGG
  24. #if MPT_COMPILER_CLANG
  25. #pragma clang diagnostic push
  26. #pragma clang diagnostic ignored "-Wreserved-id-macro"
  27. #endif // MPT_COMPILER_CLANG
  28. #include <ogg/ogg.h>
  29. #if MPT_COMPILER_CLANG
  30. #pragma clang diagnostic pop
  31. #endif // MPT_COMPILER_CLANG
  32. #endif // MPT_WITH_OGG
  33. #if defined(MPT_WITH_OPUSFILE)
  34. #include <opusfile.h>
  35. #endif // MPT_WITH_OPUSFILE
  36. OPENMPT_NAMESPACE_BEGIN
  37. ////////////////////////////////////////////////////////////////////////////////
  38. // Opus
  39. #if defined(MPT_WITH_OPUSFILE)
  40. static mpt::ustring UStringFromOpus(const char *str)
  41. {
  42. return str ? mpt::ToUnicode(mpt::Charset::UTF8, str) : mpt::ustring();
  43. }
  44. static FileTags GetOpusFileTags(OggOpusFile *of)
  45. {
  46. FileTags tags;
  47. const OpusTags *ot = op_tags(of, -1);
  48. if(!ot)
  49. {
  50. return tags;
  51. }
  52. tags.encoder = UStringFromOpus(opus_tags_query(ot, "ENCODER", 0));
  53. tags.title = UStringFromOpus(opus_tags_query(ot, "TITLE", 0));
  54. tags.comments = UStringFromOpus(opus_tags_query(ot, "DESCRIPTION", 0));
  55. tags.bpm = UStringFromOpus(opus_tags_query(ot, "BPM", 0)); // non-standard
  56. tags.artist = UStringFromOpus(opus_tags_query(ot, "ARTIST", 0));
  57. tags.album = UStringFromOpus(opus_tags_query(ot, "ALBUM", 0));
  58. tags.trackno = UStringFromOpus(opus_tags_query(ot, "TRACKNUMBER", 0));
  59. tags.year = UStringFromOpus(opus_tags_query(ot, "DATE", 0));
  60. tags.url = UStringFromOpus(opus_tags_query(ot, "CONTACT", 0));
  61. tags.genre = UStringFromOpus(opus_tags_query(ot, "GENRE", 0));
  62. return tags;
  63. }
  64. #endif // MPT_WITH_OPUSFILE
  65. bool CSoundFile::ReadOpusSample(SAMPLEINDEX sample, FileReader &file)
  66. {
  67. file.Rewind();
  68. #if defined(MPT_WITH_OPUSFILE)
  69. int rate = 0;
  70. int channels = 0;
  71. std::vector<int16> raw_sample_data;
  72. std::string sampleName;
  73. FileReader initial = file.GetChunk(65536); // 512 is recommended by libopusfile
  74. if(op_test(NULL, initial.GetRawData<unsigned char>().data(), initial.GetLength()) != 0)
  75. {
  76. return false;
  77. }
  78. OggOpusFile *of = op_open_memory(file.GetRawData<unsigned char>().data(), file.GetLength(), NULL);
  79. if(!of)
  80. {
  81. return false;
  82. }
  83. rate = 48000;
  84. channels = op_channel_count(of, -1);
  85. if(rate <= 0 || channels <= 0)
  86. {
  87. op_free(of);
  88. of = NULL;
  89. return false;
  90. }
  91. if(channels > 2 || op_link_count(of) != 1)
  92. {
  93. // We downmix multichannel to stereo as recommended by Opus specification in
  94. // case we are not able to handle > 2 channels.
  95. // We also decode chained files as stereo even if they start with a mono
  96. // stream, which simplifies handling of link boundaries for us.
  97. channels = 2;
  98. }
  99. sampleName = mpt::ToCharset(GetCharsetInternal(), GetSampleNameFromTags(GetOpusFileTags(of)));
  100. if(auto length = op_pcm_total(of, 0); length != OP_EINVAL)
  101. raw_sample_data.reserve(std::min(MAX_SAMPLE_LENGTH, mpt::saturate_cast<SmpLength>(length)) * channels);
  102. std::vector<int16> decodeBuf(120 * 48000 / 1000); // 120ms (max Opus packet), 48kHz
  103. bool eof = false;
  104. while(!eof)
  105. {
  106. int framesRead = 0;
  107. if(channels == 2)
  108. {
  109. framesRead = op_read_stereo(of, &(decodeBuf[0]), static_cast<int>(decodeBuf.size()));
  110. } else if(channels == 1)
  111. {
  112. framesRead = op_read(of, &(decodeBuf[0]), static_cast<int>(decodeBuf.size()), NULL);
  113. }
  114. if(framesRead > 0)
  115. {
  116. mpt::append(raw_sample_data, decodeBuf.begin(), decodeBuf.begin() + (framesRead * channels));
  117. } else if(framesRead == 0)
  118. {
  119. eof = true;
  120. } else if(framesRead == OP_HOLE)
  121. {
  122. // continue
  123. } else
  124. {
  125. // other errors are fatal, stop decoding
  126. eof = true;
  127. }
  128. if((raw_sample_data.size() / channels) > MAX_SAMPLE_LENGTH)
  129. {
  130. break;
  131. }
  132. }
  133. op_free(of);
  134. of = NULL;
  135. if(raw_sample_data.empty())
  136. {
  137. return false;
  138. }
  139. DestroySampleThreadsafe(sample);
  140. ModSample &mptSample = Samples[sample];
  141. mptSample.Initialize();
  142. mptSample.nC5Speed = rate;
  143. mptSample.nLength = std::min(MAX_SAMPLE_LENGTH, mpt::saturate_cast<SmpLength>(raw_sample_data.size() / channels));
  144. mptSample.uFlags.set(CHN_16BIT);
  145. mptSample.uFlags.set(CHN_STEREO, channels == 2);
  146. if(!mptSample.AllocateSample())
  147. {
  148. return false;
  149. }
  150. if(raw_sample_data.size() / channels > MAX_SAMPLE_LENGTH)
  151. {
  152. AddToLog(LogWarning, U_("Sample has been truncated!"));
  153. }
  154. std::copy(raw_sample_data.begin(), raw_sample_data.begin() + mptSample.nLength * channels, mptSample.sample16());
  155. mptSample.Convert(MOD_TYPE_IT, GetType());
  156. mptSample.PrecomputeLoops(*this, false);
  157. m_szNames[sample] = sampleName;
  158. return true;
  159. #else // !MPT_WITH_OPUSFILE
  160. MPT_UNREFERENCED_PARAMETER(sample);
  161. MPT_UNREFERENCED_PARAMETER(file);
  162. return false;
  163. #endif // MPT_WITH_OPUSFILE
  164. }
  165. OPENMPT_NAMESPACE_END