converter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "config.h"
  2. #include "converter.h"
  3. #include <algorithm>
  4. #include <cstdint>
  5. #include <iterator>
  6. #include "AL/al.h"
  7. #include "albyte.h"
  8. #include "alu.h"
  9. #include "fpu_modes.h"
  10. #include "mixer/defs.h"
  11. namespace {
  12. /* Base template left undefined. Should be marked =delete, but Clang 3.8.1
  13. * chokes on that given the inline specializations.
  14. */
  15. template<DevFmtType T>
  16. inline ALfloat LoadSample(typename DevFmtTypeTraits<T>::Type val) noexcept;
  17. template<> inline ALfloat LoadSample<DevFmtByte>(DevFmtTypeTraits<DevFmtByte>::Type val) noexcept
  18. { return val * (1.0f/128.0f); }
  19. template<> inline ALfloat LoadSample<DevFmtShort>(DevFmtTypeTraits<DevFmtShort>::Type val) noexcept
  20. { return val * (1.0f/32768.0f); }
  21. template<> inline ALfloat LoadSample<DevFmtInt>(DevFmtTypeTraits<DevFmtInt>::Type val) noexcept
  22. { return static_cast<float>(val) * (1.0f/2147483648.0f); }
  23. template<> inline ALfloat LoadSample<DevFmtFloat>(DevFmtTypeTraits<DevFmtFloat>::Type val) noexcept
  24. { return val; }
  25. template<> inline ALfloat LoadSample<DevFmtUByte>(DevFmtTypeTraits<DevFmtUByte>::Type val) noexcept
  26. { return LoadSample<DevFmtByte>(static_cast<ALbyte>(val - 128)); }
  27. template<> inline ALfloat LoadSample<DevFmtUShort>(DevFmtTypeTraits<DevFmtUShort>::Type val) noexcept
  28. { return LoadSample<DevFmtShort>(static_cast<ALshort>(val - 32768)); }
  29. template<> inline ALfloat LoadSample<DevFmtUInt>(DevFmtTypeTraits<DevFmtUInt>::Type val) noexcept
  30. { return LoadSample<DevFmtInt>(static_cast<ALint>(val - 2147483648u)); }
  31. template<DevFmtType T>
  32. inline void LoadSampleArray(ALfloat *RESTRICT dst, const void *src, const size_t srcstep,
  33. const size_t samples) noexcept
  34. {
  35. using SampleType = typename DevFmtTypeTraits<T>::Type;
  36. const SampleType *ssrc = static_cast<const SampleType*>(src);
  37. for(size_t i{0u};i < samples;i++)
  38. dst[i] = LoadSample<T>(ssrc[i*srcstep]);
  39. }
  40. void LoadSamples(ALfloat *dst, const ALvoid *src, const size_t srcstep, const DevFmtType srctype,
  41. const size_t samples) noexcept
  42. {
  43. #define HANDLE_FMT(T) \
  44. case T: LoadSampleArray<T>(dst, src, srcstep, samples); break
  45. switch(srctype)
  46. {
  47. HANDLE_FMT(DevFmtByte);
  48. HANDLE_FMT(DevFmtUByte);
  49. HANDLE_FMT(DevFmtShort);
  50. HANDLE_FMT(DevFmtUShort);
  51. HANDLE_FMT(DevFmtInt);
  52. HANDLE_FMT(DevFmtUInt);
  53. HANDLE_FMT(DevFmtFloat);
  54. }
  55. #undef HANDLE_FMT
  56. }
  57. template<DevFmtType T>
  58. inline typename DevFmtTypeTraits<T>::Type StoreSample(ALfloat) noexcept;
  59. template<> inline ALfloat StoreSample<DevFmtFloat>(ALfloat val) noexcept
  60. { return val; }
  61. template<> inline ALint StoreSample<DevFmtInt>(ALfloat val) noexcept
  62. { return fastf2i(clampf(val*2147483648.0f, -2147483648.0f, 2147483520.0f)); }
  63. template<> inline ALshort StoreSample<DevFmtShort>(ALfloat val) noexcept
  64. { return static_cast<ALshort>(fastf2i(clampf(val*32768.0f, -32768.0f, 32767.0f))); }
  65. template<> inline ALbyte StoreSample<DevFmtByte>(ALfloat val) noexcept
  66. { return static_cast<ALbyte>(fastf2i(clampf(val*128.0f, -128.0f, 127.0f))); }
  67. /* Define unsigned output variations. */
  68. template<> inline ALuint StoreSample<DevFmtUInt>(ALfloat val) noexcept
  69. { return static_cast<ALuint>(StoreSample<DevFmtInt>(val)) + 2147483648u; }
  70. template<> inline ALushort StoreSample<DevFmtUShort>(ALfloat val) noexcept
  71. { return static_cast<ALushort>(StoreSample<DevFmtShort>(val) + 32768); }
  72. template<> inline ALubyte StoreSample<DevFmtUByte>(ALfloat val) noexcept
  73. { return static_cast<ALubyte>(StoreSample<DevFmtByte>(val) + 128); }
  74. template<DevFmtType T>
  75. inline void StoreSampleArray(void *dst, const ALfloat *RESTRICT src, const size_t dststep,
  76. const size_t samples) noexcept
  77. {
  78. using SampleType = typename DevFmtTypeTraits<T>::Type;
  79. SampleType *sdst = static_cast<SampleType*>(dst);
  80. for(size_t i{0u};i < samples;i++)
  81. sdst[i*dststep] = StoreSample<T>(src[i]);
  82. }
  83. void StoreSamples(ALvoid *dst, const ALfloat *src, const size_t dststep, const DevFmtType dsttype,
  84. const size_t samples) noexcept
  85. {
  86. #define HANDLE_FMT(T) \
  87. case T: StoreSampleArray<T>(dst, src, dststep, samples); break
  88. switch(dsttype)
  89. {
  90. HANDLE_FMT(DevFmtByte);
  91. HANDLE_FMT(DevFmtUByte);
  92. HANDLE_FMT(DevFmtShort);
  93. HANDLE_FMT(DevFmtUShort);
  94. HANDLE_FMT(DevFmtInt);
  95. HANDLE_FMT(DevFmtUInt);
  96. HANDLE_FMT(DevFmtFloat);
  97. }
  98. #undef HANDLE_FMT
  99. }
  100. template<DevFmtType T>
  101. void Mono2Stereo(ALfloat *RESTRICT dst, const void *src, const size_t frames) noexcept
  102. {
  103. using SampleType = typename DevFmtTypeTraits<T>::Type;
  104. const SampleType *ssrc = static_cast<const SampleType*>(src);
  105. for(size_t i{0u};i < frames;i++)
  106. dst[i*2 + 1] = dst[i*2 + 0] = LoadSample<T>(ssrc[i]) * 0.707106781187f;
  107. }
  108. template<DevFmtType T>
  109. void Stereo2Mono(ALfloat *RESTRICT dst, const void *src, const size_t frames) noexcept
  110. {
  111. using SampleType = typename DevFmtTypeTraits<T>::Type;
  112. const SampleType *ssrc = static_cast<const SampleType*>(src);
  113. for(size_t i{0u};i < frames;i++)
  114. dst[i] = (LoadSample<T>(ssrc[i*2 + 0])+LoadSample<T>(ssrc[i*2 + 1])) *
  115. 0.707106781187f;
  116. }
  117. } // namespace
  118. SampleConverterPtr CreateSampleConverter(DevFmtType srcType, DevFmtType dstType, size_t numchans,
  119. ALuint srcRate, ALuint dstRate, Resampler resampler)
  120. {
  121. if(numchans < 1 || srcRate < 1 || dstRate < 1)
  122. return nullptr;
  123. SampleConverterPtr converter{new (FamCount{numchans}) SampleConverter{numchans}};
  124. converter->mSrcType = srcType;
  125. converter->mDstType = dstType;
  126. converter->mSrcTypeSize = BytesFromDevFmt(srcType);
  127. converter->mDstTypeSize = BytesFromDevFmt(dstType);
  128. converter->mSrcPrepCount = 0;
  129. converter->mFracOffset = 0;
  130. /* Have to set the mixer FPU mode since that's what the resampler code expects. */
  131. FPUCtl mixer_mode{};
  132. auto step = static_cast<ALuint>(
  133. mind(srcRate*double{FRACTIONONE}/dstRate + 0.5, MAX_PITCH*FRACTIONONE));
  134. converter->mIncrement = maxu(step, 1);
  135. if(converter->mIncrement == FRACTIONONE)
  136. converter->mResample = Resample_<CopyTag,CTag>;
  137. else
  138. converter->mResample = PrepareResampler(resampler, converter->mIncrement,
  139. &converter->mState);
  140. return converter;
  141. }
  142. ALuint SampleConverter::availableOut(ALuint srcframes) const
  143. {
  144. ALint prepcount{mSrcPrepCount};
  145. if(prepcount < 0)
  146. {
  147. /* Negative prepcount means we need to skip that many input samples. */
  148. if(static_cast<ALuint>(-prepcount) >= srcframes)
  149. return 0;
  150. srcframes -= static_cast<ALuint>(-prepcount);
  151. prepcount = 0;
  152. }
  153. if(srcframes < 1)
  154. {
  155. /* No output samples if there's no input samples. */
  156. return 0;
  157. }
  158. if(prepcount < MAX_RESAMPLER_PADDING
  159. && static_cast<ALuint>(MAX_RESAMPLER_PADDING - prepcount) >= srcframes)
  160. {
  161. /* Not enough input samples to generate an output sample. */
  162. return 0;
  163. }
  164. auto DataSize64 = static_cast<uint64_t>(prepcount);
  165. DataSize64 += srcframes;
  166. DataSize64 -= MAX_RESAMPLER_PADDING;
  167. DataSize64 <<= FRACTIONBITS;
  168. DataSize64 -= mFracOffset;
  169. /* If we have a full prep, we can generate at least one sample. */
  170. return static_cast<ALuint>(clampu64((DataSize64 + mIncrement-1)/mIncrement, 1, BUFFERSIZE));
  171. }
  172. ALuint SampleConverter::convert(const ALvoid **src, ALuint *srcframes, ALvoid *dst, ALuint dstframes)
  173. {
  174. const ALuint SrcFrameSize{static_cast<ALuint>(mChan.size()) * mSrcTypeSize};
  175. const ALuint DstFrameSize{static_cast<ALuint>(mChan.size()) * mDstTypeSize};
  176. const ALuint increment{mIncrement};
  177. auto SamplesIn = static_cast<const al::byte*>(*src);
  178. ALuint NumSrcSamples{*srcframes};
  179. FPUCtl mixer_mode{};
  180. ALuint pos{0};
  181. while(pos < dstframes && NumSrcSamples > 0)
  182. {
  183. ALint prepcount{mSrcPrepCount};
  184. if(prepcount < 0)
  185. {
  186. /* Negative prepcount means we need to skip that many input samples. */
  187. if(static_cast<ALuint>(-prepcount) >= NumSrcSamples)
  188. {
  189. mSrcPrepCount = static_cast<ALint>(NumSrcSamples) + prepcount;
  190. NumSrcSamples = 0;
  191. break;
  192. }
  193. SamplesIn += SrcFrameSize*static_cast<ALuint>(-prepcount);
  194. NumSrcSamples -= static_cast<ALuint>(-prepcount);
  195. mSrcPrepCount = 0;
  196. continue;
  197. }
  198. ALuint toread{minu(NumSrcSamples, BUFFERSIZE - MAX_RESAMPLER_PADDING)};
  199. if(prepcount < MAX_RESAMPLER_PADDING
  200. && static_cast<ALuint>(MAX_RESAMPLER_PADDING - prepcount) >= toread)
  201. {
  202. /* Not enough input samples to generate an output sample. Store
  203. * what we're given for later.
  204. */
  205. for(size_t chan{0u};chan < mChan.size();chan++)
  206. LoadSamples(&mChan[chan].PrevSamples[prepcount], SamplesIn + mSrcTypeSize*chan,
  207. mChan.size(), mSrcType, toread);
  208. mSrcPrepCount = prepcount + static_cast<ALint>(toread);
  209. NumSrcSamples = 0;
  210. break;
  211. }
  212. ALfloat *RESTRICT SrcData{mSrcSamples};
  213. ALfloat *RESTRICT DstData{mDstSamples};
  214. ALuint DataPosFrac{mFracOffset};
  215. auto DataSize64 = static_cast<uint64_t>(prepcount);
  216. DataSize64 += toread;
  217. DataSize64 -= MAX_RESAMPLER_PADDING;
  218. DataSize64 <<= FRACTIONBITS;
  219. DataSize64 -= DataPosFrac;
  220. /* If we have a full prep, we can generate at least one sample. */
  221. auto DstSize = static_cast<ALuint>(
  222. clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE));
  223. DstSize = minu(DstSize, dstframes-pos);
  224. for(size_t chan{0u};chan < mChan.size();chan++)
  225. {
  226. const al::byte *SrcSamples{SamplesIn + mSrcTypeSize*chan};
  227. al::byte *DstSamples = static_cast<al::byte*>(dst) + mDstTypeSize*chan;
  228. /* Load the previous samples into the source data first, then the
  229. * new samples from the input buffer.
  230. */
  231. std::copy_n(mChan[chan].PrevSamples, prepcount, SrcData);
  232. LoadSamples(SrcData + prepcount, SrcSamples, mChan.size(), mSrcType, toread);
  233. /* Store as many prep samples for next time as possible, given the
  234. * number of output samples being generated.
  235. */
  236. ALuint SrcDataEnd{(DstSize*increment + DataPosFrac)>>FRACTIONBITS};
  237. if(SrcDataEnd >= static_cast<ALuint>(prepcount)+toread)
  238. std::fill(std::begin(mChan[chan].PrevSamples),
  239. std::end(mChan[chan].PrevSamples), 0.0f);
  240. else
  241. {
  242. const size_t len{minz(al::size(mChan[chan].PrevSamples),
  243. static_cast<ALuint>(prepcount)+toread-SrcDataEnd)};
  244. std::copy_n(SrcData+SrcDataEnd, len, mChan[chan].PrevSamples);
  245. std::fill(std::begin(mChan[chan].PrevSamples)+len,
  246. std::end(mChan[chan].PrevSamples), 0.0f);
  247. }
  248. /* Now resample, and store the result in the output buffer. */
  249. const ALfloat *ResampledData{mResample(&mState, SrcData+(MAX_RESAMPLER_PADDING>>1),
  250. DataPosFrac, increment, {DstData, DstSize})};
  251. StoreSamples(DstSamples, ResampledData, mChan.size(), mDstType, DstSize);
  252. }
  253. /* Update the number of prep samples still available, as well as the
  254. * fractional offset.
  255. */
  256. DataPosFrac += increment*DstSize;
  257. mSrcPrepCount = mini(prepcount + static_cast<ALint>(toread - (DataPosFrac>>FRACTIONBITS)),
  258. MAX_RESAMPLER_PADDING);
  259. mFracOffset = DataPosFrac & FRACTIONMASK;
  260. /* Update the src and dst pointers in case there's still more to do. */
  261. SamplesIn += SrcFrameSize*(DataPosFrac>>FRACTIONBITS);
  262. NumSrcSamples -= minu(NumSrcSamples, (DataPosFrac>>FRACTIONBITS));
  263. dst = static_cast<al::byte*>(dst) + DstFrameSize*DstSize;
  264. pos += DstSize;
  265. }
  266. *src = SamplesIn;
  267. *srcframes = NumSrcSamples;
  268. return pos;
  269. }
  270. void ChannelConverter::convert(const ALvoid *src, ALfloat *dst, ALuint frames) const
  271. {
  272. if(mSrcChans == DevFmtStereo && mDstChans == DevFmtMono)
  273. {
  274. switch(mSrcType)
  275. {
  276. #define HANDLE_FMT(T) case T: Stereo2Mono<T>(dst, src, frames); break
  277. HANDLE_FMT(DevFmtByte);
  278. HANDLE_FMT(DevFmtUByte);
  279. HANDLE_FMT(DevFmtShort);
  280. HANDLE_FMT(DevFmtUShort);
  281. HANDLE_FMT(DevFmtInt);
  282. HANDLE_FMT(DevFmtUInt);
  283. HANDLE_FMT(DevFmtFloat);
  284. #undef HANDLE_FMT
  285. }
  286. }
  287. else if(mSrcChans == DevFmtMono && mDstChans == DevFmtStereo)
  288. {
  289. switch(mSrcType)
  290. {
  291. #define HANDLE_FMT(T) case T: Mono2Stereo<T>(dst, src, frames); break
  292. HANDLE_FMT(DevFmtByte);
  293. HANDLE_FMT(DevFmtUByte);
  294. HANDLE_FMT(DevFmtShort);
  295. HANDLE_FMT(DevFmtUShort);
  296. HANDLE_FMT(DevFmtInt);
  297. HANDLE_FMT(DevFmtUInt);
  298. HANDLE_FMT(DevFmtFloat);
  299. #undef HANDLE_FMT
  300. }
  301. }
  302. else
  303. LoadSamples(dst, src, 1u, mSrcType, frames * ChannelsFromDevFmt(mSrcChans, 0));
  304. }