1
0

StreamEncoder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * StreamEncoder.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 "mpt/io/base.hpp"
  13. #include "mpt/io/io.hpp"
  14. #include "mpt/io/io_stdstream.hpp"
  15. #include "mpt/io_write/buffer.hpp"
  16. #include "openmpt/soundbase/SampleEncode.hpp"
  17. #include "openmpt/soundbase/SampleFormat.hpp"
  18. #include <ostream>
  19. OPENMPT_NAMESPACE_BEGIN
  20. StreamWriterBase::StreamWriterBase(std::ostream &stream)
  21. : f(stream)
  22. , fStart(f.tellp())
  23. {
  24. return;
  25. }
  26. StreamWriterBase::~StreamWriterBase()
  27. {
  28. return;
  29. }
  30. template <mpt::endian endian, typename Tsample>
  31. static inline std::pair<bool, std::size_t> WriteInterleavedImpl(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const Tsample *interleaved)
  32. {
  33. MPT_ASSERT(endian == format.endian);
  34. MPT_ASSERT(format.GetSampleFormat() == SampleFormatTraits<Tsample>::sampleFormat());
  35. bool success = true;
  36. std::size_t written = 0;
  37. MPT_MAYBE_CONSTANT_IF(endian == mpt::get_endian() && format.encoding != Encoder::Format::Encoding::Alaw && format.encoding != Encoder::Format::Encoding::ulaw)
  38. {
  39. if(!mpt::IO::WriteRaw(f, reinterpret_cast<const std::byte*>(interleaved), frameCount * channels * format.GetSampleFormat().GetSampleSize()))
  40. {
  41. success = false;
  42. }
  43. written += frameCount * channels * format.GetSampleFormat().GetSampleSize();
  44. } else
  45. {
  46. std::array<std::byte, mpt::IO::BUFFERSIZE_TINY> fbuf;
  47. mpt::IO::WriteBuffer<std::ostream> bf{f, fbuf};
  48. if(format.encoding == Encoder::Format::Encoding::Alaw)
  49. {
  50. if constexpr(std::is_same<Tsample, int16>::value)
  51. {
  52. SC::EncodeALaw conv;
  53. for(std::size_t frame = 0; frame < frameCount; ++frame)
  54. {
  55. for(uint16 channel = 0; channel < channels; ++channel)
  56. {
  57. std::byte sampledata = conv(interleaved[channel]);
  58. mpt::IO::WriteRaw(bf, &sampledata, 1);
  59. written += 1;
  60. }
  61. interleaved += channels;
  62. }
  63. }
  64. } else if(format.encoding == Encoder::Format::Encoding::ulaw)
  65. {
  66. if constexpr(std::is_same<Tsample, int16>::value)
  67. {
  68. SC::EncodeuLaw conv;
  69. for(std::size_t frame = 0; frame < frameCount; ++frame)
  70. {
  71. for(uint16 channel = 0; channel < channels; ++channel)
  72. {
  73. std::byte sampledata = conv(interleaved[channel]);
  74. mpt::IO::WriteRaw(bf, &sampledata, 1);
  75. written += 1;
  76. }
  77. interleaved += channels;
  78. }
  79. }
  80. } else
  81. {
  82. if constexpr(std::is_floating_point<Tsample>::value)
  83. {
  84. std::vector<typename mpt::make_float_endian<endian, Tsample>::type> frameData(channels);
  85. for(std::size_t frame = 0; frame < frameCount; ++frame)
  86. {
  87. for(uint16 channel = 0; channel < channels; ++channel)
  88. {
  89. frameData[channel] = typename mpt::make_float_endian<endian, Tsample>::type(interleaved[channel]);
  90. }
  91. mpt::IO::WriteRaw(bf, reinterpret_cast<const std::byte*>(frameData.data()), channels * format.GetSampleFormat().GetSampleSize());
  92. written += channels * format.GetSampleFormat().GetSampleSize();
  93. interleaved += channels;
  94. }
  95. } else if constexpr(std::is_same<Tsample, int24>::value)
  96. {
  97. MPT_ASSERT(!mpt::endian_is_weird());
  98. for(std::size_t frame = 0; frame < frameCount; ++frame)
  99. {
  100. for(uint16 channel = 0; channel < channels; ++channel)
  101. {
  102. std::array<std::byte, 3> data;
  103. std::memcpy(data.data(), interleaved, 3);
  104. MPT_MAYBE_CONSTANT_IF(endian != mpt::get_endian())
  105. {
  106. std::reverse(data.begin(), data.end());
  107. }
  108. mpt::IO::Write(bf, data);
  109. written += data.size();
  110. interleaved += 3;
  111. }
  112. }
  113. } else
  114. {
  115. std::vector<typename mpt::make_endian<endian, Tsample>::type> frameData(channels);
  116. for(std::size_t frame = 0; frame < frameCount; ++frame)
  117. {
  118. for(uint16 channel = 0; channel < channels; ++channel)
  119. {
  120. frameData[channel] = interleaved[channel];
  121. }
  122. mpt::IO::WriteRaw(bf, reinterpret_cast<const std::byte*>(frameData.data()), channels * format.GetSampleFormat().GetSampleSize());
  123. written += channels * format.GetSampleFormat().GetSampleSize();
  124. interleaved += channels;
  125. }
  126. }
  127. }
  128. if(bf.HasWriteError())
  129. {
  130. success = false;
  131. }
  132. }
  133. return std::make_pair(success, written);
  134. }
  135. std::pair<bool, std::size_t> WriteInterleavedLE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const double *interleaved)
  136. {
  137. MPT_ASSERT(format.endian == mpt::endian::little);
  138. return WriteInterleavedImpl<mpt::endian::little>(f, channels, format, frameCount, interleaved);
  139. }
  140. std::pair<bool, std::size_t> WriteInterleavedLE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const float *interleaved)
  141. {
  142. MPT_ASSERT(format.endian == mpt::endian::little);
  143. return WriteInterleavedImpl<mpt::endian::little>(f, channels, format, frameCount, interleaved);
  144. }
  145. std::pair<bool, std::size_t> WriteInterleavedLE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const int32 *interleaved)
  146. {
  147. MPT_ASSERT(format.endian == mpt::endian::little);
  148. return WriteInterleavedImpl<mpt::endian::little>(f, channels, format, frameCount, interleaved);
  149. }
  150. std::pair<bool, std::size_t> WriteInterleavedLE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const int24 *interleaved)
  151. {
  152. MPT_ASSERT(format.endian == mpt::endian::little);
  153. return WriteInterleavedImpl<mpt::endian::little>(f, channels, format, frameCount, interleaved);
  154. }
  155. std::pair<bool, std::size_t> WriteInterleavedLE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const int16 *interleaved)
  156. {
  157. MPT_ASSERT(format.endian == mpt::endian::little);
  158. return WriteInterleavedImpl<mpt::endian::little>(f, channels, format, frameCount, interleaved);
  159. }
  160. std::pair<bool, std::size_t> WriteInterleavedLE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const int8 *interleaved)
  161. {
  162. MPT_ASSERT(format.endian == mpt::endian::little);
  163. return WriteInterleavedImpl<mpt::endian::little>(f, channels, format, frameCount, interleaved);
  164. }
  165. std::pair<bool, std::size_t> WriteInterleavedLE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const uint8 *interleaved)
  166. {
  167. MPT_ASSERT(format.endian == mpt::endian::little);
  168. return WriteInterleavedImpl<mpt::endian::little>(f, channels, format, frameCount, interleaved);
  169. }
  170. std::pair<bool, std::size_t> WriteInterleavedBE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const double *interleaved)
  171. {
  172. MPT_ASSERT(format.endian == mpt::endian::big);
  173. return WriteInterleavedImpl<mpt::endian::big>(f, channels, format, frameCount, interleaved);
  174. }
  175. std::pair<bool, std::size_t> WriteInterleavedBE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const float *interleaved)
  176. {
  177. MPT_ASSERT(format.endian == mpt::endian::big);
  178. return WriteInterleavedImpl<mpt::endian::big>(f, channels, format, frameCount, interleaved);
  179. }
  180. std::pair<bool, std::size_t> WriteInterleavedBE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const int32 *interleaved)
  181. {
  182. MPT_ASSERT(format.endian == mpt::endian::big);
  183. return WriteInterleavedImpl<mpt::endian::big>(f, channels, format, frameCount, interleaved);
  184. }
  185. std::pair<bool, std::size_t> WriteInterleavedBE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const int24 *interleaved)
  186. {
  187. MPT_ASSERT(format.endian == mpt::endian::big);
  188. return WriteInterleavedImpl<mpt::endian::big>(f, channels, format, frameCount, interleaved);
  189. }
  190. std::pair<bool, std::size_t> WriteInterleavedBE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const int16 *interleaved)
  191. {
  192. MPT_ASSERT(format.endian == mpt::endian::big);
  193. return WriteInterleavedImpl<mpt::endian::big>(f, channels, format, frameCount, interleaved);
  194. }
  195. std::pair<bool, std::size_t> WriteInterleavedBE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const int8 *interleaved)
  196. {
  197. MPT_ASSERT(format.endian == mpt::endian::big);
  198. return WriteInterleavedImpl<mpt::endian::big>(f, channels, format, frameCount, interleaved);
  199. }
  200. std::pair<bool, std::size_t> WriteInterleavedBE(std::ostream &f, uint16 channels, Encoder::Format format, std::size_t frameCount, const uint8 *interleaved)
  201. {
  202. MPT_ASSERT(format.endian == mpt::endian::big);
  203. return WriteInterleavedImpl<mpt::endian::big>(f, channels, format, frameCount, interleaved);
  204. }
  205. SampleFormat StreamWriterBase::GetSampleFormat() const
  206. {
  207. return SampleFormat::Float32;
  208. }
  209. void StreamWriterBase::WriteInterleaved(std::size_t frameCount, const double *interleaved)
  210. {
  211. MPT_UNREFERENCED_PARAMETER(frameCount);
  212. MPT_UNREFERENCED_PARAMETER(interleaved);
  213. MPT_ASSERT_NOTREACHED();
  214. }
  215. void StreamWriterBase::WriteInterleaved(std::size_t frameCount, const float *interleaved)
  216. {
  217. MPT_UNREFERENCED_PARAMETER(frameCount);
  218. MPT_UNREFERENCED_PARAMETER(interleaved);
  219. MPT_ASSERT_NOTREACHED();
  220. }
  221. void StreamWriterBase::WriteInterleaved(std::size_t frameCount, const int32 *interleaved)
  222. {
  223. MPT_UNREFERENCED_PARAMETER(frameCount);
  224. MPT_UNREFERENCED_PARAMETER(interleaved);
  225. MPT_ASSERT_NOTREACHED();
  226. }
  227. void StreamWriterBase::WriteInterleaved(std::size_t frameCount, const int24 *interleaved)
  228. {
  229. MPT_UNREFERENCED_PARAMETER(frameCount);
  230. MPT_UNREFERENCED_PARAMETER(interleaved);
  231. MPT_ASSERT_NOTREACHED();
  232. }
  233. void StreamWriterBase::WriteInterleaved(std::size_t frameCount, const int16 *interleaved)
  234. {
  235. MPT_UNREFERENCED_PARAMETER(frameCount);
  236. MPT_UNREFERENCED_PARAMETER(interleaved);
  237. MPT_ASSERT_NOTREACHED();
  238. }
  239. void StreamWriterBase::WriteInterleaved(std::size_t frameCount, const int8 *interleaved)
  240. {
  241. MPT_UNREFERENCED_PARAMETER(frameCount);
  242. MPT_UNREFERENCED_PARAMETER(interleaved);
  243. MPT_ASSERT_NOTREACHED();
  244. }
  245. void StreamWriterBase::WriteInterleaved(std::size_t frameCount, const uint8 *interleaved)
  246. {
  247. MPT_UNREFERENCED_PARAMETER(frameCount);
  248. MPT_UNREFERENCED_PARAMETER(interleaved);
  249. MPT_ASSERT_NOTREACHED();
  250. }
  251. void StreamWriterBase::WriteCues(const std::vector<uint64> &cues)
  252. {
  253. MPT_UNREFERENCED_PARAMETER(cues);
  254. }
  255. void StreamWriterBase::WriteFinalize()
  256. {
  257. return;
  258. }
  259. void StreamWriterBase::WriteBuffer()
  260. {
  261. if(!f)
  262. {
  263. return;
  264. }
  265. if(buf.empty())
  266. {
  267. return;
  268. }
  269. f.write(buf.data(), buf.size());
  270. buf.resize(0);
  271. }
  272. void EncoderFactoryBase::SetTraits(const Encoder::Traits &traits)
  273. {
  274. m_Traits = traits;
  275. }
  276. bool EncoderFactoryBase::IsBitrateSupported(int samplerate, int channels, int bitrate) const
  277. {
  278. MPT_UNREFERENCED_PARAMETER(samplerate);
  279. MPT_UNREFERENCED_PARAMETER(channels);
  280. MPT_UNREFERENCED_PARAMETER(bitrate);
  281. return true;
  282. }
  283. mpt::ustring EncoderFactoryBase::DescribeQuality(float quality) const
  284. {
  285. return MPT_UFORMAT("VBR {}%")(static_cast<int>(quality * 100.0f));
  286. }
  287. mpt::ustring EncoderFactoryBase::DescribeBitrateVBR(int bitrate) const
  288. {
  289. return MPT_UFORMAT("VBR {} kbit")(bitrate);
  290. }
  291. mpt::ustring EncoderFactoryBase::DescribeBitrateABR(int bitrate) const
  292. {
  293. return MPT_UFORMAT("ABR {} kbit")(bitrate);
  294. }
  295. mpt::ustring EncoderFactoryBase::DescribeBitrateCBR(int bitrate) const
  296. {
  297. return MPT_UFORMAT("CBR {} kbit")(bitrate);
  298. }
  299. OPENMPT_NAMESPACE_END