1
0

StreamEncoderOpus.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * StreamEncoderOpus.cpp
  3. * ---------------------
  4. * Purpose: Exporting streamed music files.
  5. * Notes : none
  6. * Authors: Joern Heusipp
  7. * OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "StreamEncoder.h"
  12. #include "StreamEncoderOpus.h"
  13. #include <sstream>
  14. #include "Mptrack.h"
  15. #include <deque>
  16. #if defined(MPT_WITH_OPUS) && defined(MPT_WITH_OPUSENC)
  17. #include <opusenc.h>
  18. #endif
  19. OPENMPT_NAMESPACE_BEGIN
  20. static Encoder::Traits BuildTraits()
  21. {
  22. Encoder::Traits traits;
  23. #if defined(MPT_WITH_OPUS) && defined(MPT_WITH_OPUSENC)
  24. traits.fileExtension = P_("opus");
  25. traits.fileShortDescription = U_("Opus");
  26. traits.fileDescription = U_("Ogg Opus");
  27. traits.encoderSettingsName = U_("Opus");
  28. traits.canTags = true;
  29. traits.maxChannels = 4;
  30. traits.samplerates = mpt::make_vector(opus_all_samplerates);
  31. traits.modes = Encoder::ModeCBR | Encoder::ModeVBR;
  32. traits.bitrates = mpt::make_vector(opus_bitrates);
  33. traits.defaultSamplerate = 48000;
  34. traits.defaultChannels = 2;
  35. traits.defaultMode = Encoder::ModeVBR;
  36. traits.defaultBitrate = 128;
  37. #endif
  38. return traits;
  39. }
  40. #if defined(MPT_WITH_OPUS) && defined(MPT_WITH_OPUSENC)
  41. class OpusStreamWriter : public StreamWriterBase
  42. {
  43. private:
  44. OpusEncCallbacks ope_callbacks;
  45. OggOpusComments *ope_comments;
  46. OggOpusEnc *ope_encoder;
  47. std::vector<std::pair<std::string, std::string> > opus_comments;
  48. private:
  49. static int CallbackWrite(void *user_data, const unsigned char *ptr, opus_int32 len)
  50. {
  51. return reinterpret_cast<OpusStreamWriter*>(user_data)->CallbackWriteImpl(ptr, len);
  52. }
  53. static int CallbackClose(void *user_data)
  54. {
  55. return reinterpret_cast<OpusStreamWriter*>(user_data)->CallbackCloseImpl();
  56. }
  57. int CallbackWriteImpl(const unsigned char *ptr, opus_int32 len)
  58. {
  59. if(len < 0)
  60. {
  61. return 1;
  62. }
  63. if(!ptr && len > 0)
  64. {
  65. return 1;
  66. }
  67. buf.assign(ptr, ptr + len);
  68. WriteBuffer();
  69. return 0;
  70. }
  71. int CallbackCloseImpl()
  72. {
  73. return 0;
  74. }
  75. private:
  76. void AddCommentField(const std::string &field, const mpt::ustring &data)
  77. {
  78. if(!field.empty() && !data.empty())
  79. {
  80. opus_comments.push_back(std::make_pair(field, mpt::ToCharset(mpt::Charset::UTF8, data)));
  81. }
  82. }
  83. public:
  84. OpusStreamWriter(std::ostream &stream, const Encoder::Settings &settings, const FileTags &tags)
  85. : StreamWriterBase(stream)
  86. {
  87. ope_callbacks.write = &CallbackWrite;
  88. ope_callbacks.close = &CallbackClose;
  89. opus_comments.clear();
  90. bool opus_cbr = (settings.Mode == Encoder::ModeCBR);
  91. int opus_bitrate = settings.Bitrate * 1000;
  92. if(settings.Tags)
  93. {
  94. AddCommentField("ENCODER", tags.encoder);
  95. AddCommentField("SOURCEMEDIA", U_("tracked music file"));
  96. AddCommentField("TITLE", tags.title );
  97. AddCommentField("ARTIST", tags.artist );
  98. AddCommentField("ALBUM", tags.album );
  99. AddCommentField("DATE", tags.year );
  100. AddCommentField("COMMENT", tags.comments );
  101. AddCommentField("GENRE", tags.genre );
  102. AddCommentField("CONTACT", tags.url );
  103. AddCommentField("BPM", tags.bpm ); // non-standard
  104. AddCommentField("TRACKNUMBER", tags.trackno );
  105. }
  106. int ope_error = 0;
  107. ope_comments = ope_comments_create();
  108. if(settings.Tags && ope_comments)
  109. {
  110. for(const auto & comment : opus_comments)
  111. {
  112. ope_comments_add(ope_comments, comment.first.c_str(), comment.second.c_str());
  113. }
  114. }
  115. ope_encoder = ope_encoder_create_callbacks(&ope_callbacks, this, ope_comments, settings.Samplerate, settings.Channels, settings.Channels > 2 ? 1 : 0, &ope_error);
  116. opus_int32 ctl_serial = mpt::random<uint32>(theApp.PRNG());
  117. ope_encoder_ctl(ope_encoder, OPE_SET_SERIALNO(ctl_serial));
  118. opus_int32 ctl_bitrate = opus_bitrate;
  119. ope_encoder_ctl(ope_encoder, OPUS_SET_BITRATE(ctl_bitrate));
  120. if(opus_cbr)
  121. {
  122. opus_int32 ctl_vbr = 0;
  123. ope_encoder_ctl(ope_encoder, OPUS_SET_VBR(ctl_vbr));
  124. } else
  125. {
  126. opus_int32 ctl_vbr = 1;
  127. ope_encoder_ctl(ope_encoder, OPUS_SET_VBR(ctl_vbr));
  128. opus_int32 ctl_vbrcontraint = 0;
  129. ope_encoder_ctl(ope_encoder, OPUS_SET_VBR_CONSTRAINT(ctl_vbrcontraint));
  130. }
  131. opus_int32 complexity = settings.Details.OpusComplexity;
  132. if(complexity >= 0)
  133. {
  134. ope_encoder_ctl(ope_encoder, OPUS_SET_COMPLEXITY(complexity));
  135. }
  136. ope_encoder_flush_header(ope_encoder);
  137. }
  138. void WriteInterleaved(size_t count, const float *interleaved) override
  139. {
  140. while(count > 0)
  141. {
  142. ope_encoder_write_float(ope_encoder, interleaved, mpt::saturate_cast<int>(count));
  143. count -= static_cast<size_t>(mpt::saturate_cast<int>(count));
  144. }
  145. }
  146. void WriteFinalize() override
  147. {
  148. ope_encoder_drain(ope_encoder);
  149. }
  150. virtual ~OpusStreamWriter()
  151. {
  152. ope_encoder_destroy(ope_encoder);
  153. ope_encoder = NULL;
  154. ope_comments_destroy(ope_comments);
  155. ope_comments = NULL;
  156. }
  157. };
  158. #endif // MPT_WITH_OGG
  159. OggOpusEncoder::OggOpusEncoder()
  160. {
  161. SetTraits(BuildTraits());
  162. }
  163. bool OggOpusEncoder::IsAvailable() const
  164. {
  165. #if defined(MPT_WITH_OPUS) && defined(MPT_WITH_OPUSENC)
  166. return true;
  167. #else
  168. return false;
  169. #endif
  170. }
  171. std::unique_ptr<IAudioStreamEncoder> OggOpusEncoder::ConstructStreamEncoder(std::ostream &file, const Encoder::Settings &settings, const FileTags &tags) const
  172. {
  173. if(!IsAvailable())
  174. {
  175. return nullptr;
  176. }
  177. #if defined(MPT_WITH_OPUS) && defined(MPT_WITH_OPUSENC)
  178. return std::make_unique<OpusStreamWriter>(file, settings, tags);
  179. #else
  180. return nullptr;
  181. #endif
  182. }
  183. OPENMPT_NAMESPACE_END