2
0

buffer.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include "buffer.h"
  22. #include <algorithm>
  23. #include <array>
  24. #include <atomic>
  25. #include <cassert>
  26. #include <cstdint>
  27. #include <cstdlib>
  28. #include <cstring>
  29. #include <iterator>
  30. #include <limits>
  31. #include <memory>
  32. #include <mutex>
  33. #include <new>
  34. #include <numeric>
  35. #include <utility>
  36. #include "AL/al.h"
  37. #include "AL/alc.h"
  38. #include "AL/alext.h"
  39. #include "albyte.h"
  40. #include "alcmain.h"
  41. #include "alcontext.h"
  42. #include "alexcpt.h"
  43. #include "almalloc.h"
  44. #include "alnumeric.h"
  45. #include "aloptional.h"
  46. #include "atomic.h"
  47. #include "inprogext.h"
  48. #include "opthelpers.h"
  49. namespace {
  50. /* IMA ADPCM Stepsize table */
  51. constexpr int IMAStep_size[89] = {
  52. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
  53. 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
  54. 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
  55. 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
  56. 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
  57. 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
  58. 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
  59. 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
  60. 32767
  61. };
  62. /* IMA4 ADPCM Codeword decode table */
  63. constexpr int IMA4Codeword[16] = {
  64. 1, 3, 5, 7, 9, 11, 13, 15,
  65. -1,-3,-5,-7,-9,-11,-13,-15,
  66. };
  67. /* IMA4 ADPCM Step index adjust decode table */
  68. constexpr int IMA4Index_adjust[16] = {
  69. -1,-1,-1,-1, 2, 4, 6, 8,
  70. -1,-1,-1,-1, 2, 4, 6, 8
  71. };
  72. /* MSADPCM Adaption table */
  73. constexpr int MSADPCMAdaption[16] = {
  74. 230, 230, 230, 230, 307, 409, 512, 614,
  75. 768, 614, 512, 409, 307, 230, 230, 230
  76. };
  77. /* MSADPCM Adaption Coefficient tables */
  78. constexpr int MSADPCMAdaptionCoeff[7][2] = {
  79. { 256, 0 },
  80. { 512, -256 },
  81. { 0, 0 },
  82. { 192, 64 },
  83. { 240, 0 },
  84. { 460, -208 },
  85. { 392, -232 }
  86. };
  87. void DecodeIMA4Block(ALshort *dst, const al::byte *src, size_t numchans, size_t align)
  88. {
  89. ALint sample[MAX_INPUT_CHANNELS]{};
  90. ALint index[MAX_INPUT_CHANNELS]{};
  91. ALuint code[MAX_INPUT_CHANNELS]{};
  92. for(size_t c{0};c < numchans;c++)
  93. {
  94. sample[c] = al::to_integer<int>(src[0]) | (al::to_integer<int>(src[1])<<8);
  95. sample[c] = (sample[c]^0x8000) - 32768;
  96. src += 2;
  97. index[c] = al::to_integer<int>(src[0]) | (al::to_integer<int>(src[1])<<8);
  98. index[c] = clampi((index[c]^0x8000) - 32768, 0, 88);
  99. src += 2;
  100. *(dst++) = static_cast<ALshort>(sample[c]);
  101. }
  102. for(size_t i{1};i < align;i++)
  103. {
  104. if((i&7) == 1)
  105. {
  106. for(size_t c{0};c < numchans;c++)
  107. {
  108. code[c] = al::to_integer<ALuint>(src[0]) | (al::to_integer<ALuint>(src[1])<< 8) |
  109. (al::to_integer<ALuint>(src[2])<<16) | (al::to_integer<ALuint>(src[3])<<24);
  110. src += 4;
  111. }
  112. }
  113. for(size_t c{0};c < numchans;c++)
  114. {
  115. const ALuint nibble{code[c]&0xf};
  116. code[c] >>= 4;
  117. sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
  118. sample[c] = clampi(sample[c], -32768, 32767);
  119. index[c] += IMA4Index_adjust[nibble];
  120. index[c] = clampi(index[c], 0, 88);
  121. *(dst++) = static_cast<ALshort>(sample[c]);
  122. }
  123. }
  124. }
  125. void DecodeMSADPCMBlock(ALshort *dst, const al::byte *src, size_t numchans, size_t align)
  126. {
  127. ALubyte blockpred[MAX_INPUT_CHANNELS]{};
  128. ALint delta[MAX_INPUT_CHANNELS]{};
  129. ALshort samples[MAX_INPUT_CHANNELS][2]{};
  130. for(size_t c{0};c < numchans;c++)
  131. {
  132. blockpred[c] = std::min<ALubyte>(al::to_integer<ALubyte>(src[0]), 6);
  133. ++src;
  134. }
  135. for(size_t c{0};c < numchans;c++)
  136. {
  137. delta[c] = al::to_integer<int>(src[0]) | (al::to_integer<int>(src[1])<<8);
  138. delta[c] = (delta[c]^0x8000) - 32768;
  139. src += 2;
  140. }
  141. for(size_t c{0};c < numchans;c++)
  142. {
  143. samples[c][0] = static_cast<ALshort>(al::to_integer<int>(src[0]) |
  144. (al::to_integer<int>(src[1])<<8));
  145. src += 2;
  146. }
  147. for(size_t c{0};c < numchans;c++)
  148. {
  149. samples[c][1] = static_cast<ALshort>(al::to_integer<int>(src[0]) |
  150. (al::to_integer<int>(src[1])<<8));
  151. src += 2;
  152. }
  153. /* Second sample is written first. */
  154. for(size_t c{0};c < numchans;c++)
  155. *(dst++) = samples[c][1];
  156. for(size_t c{0};c < numchans;c++)
  157. *(dst++) = samples[c][0];
  158. int num{0};
  159. for(size_t i{2};i < align;i++)
  160. {
  161. for(size_t c{0};c < numchans;c++)
  162. {
  163. /* Read the nibble (first is in the upper bits). */
  164. al::byte nibble;
  165. if(!(num++ & 1))
  166. nibble = *src >> 4;
  167. else
  168. nibble = *(src++) & 0x0f;
  169. ALint pred{(samples[c][0]*MSADPCMAdaptionCoeff[blockpred[c]][0] +
  170. samples[c][1]*MSADPCMAdaptionCoeff[blockpred[c]][1]) / 256};
  171. pred += (al::to_integer<int>(nibble^0x08) - 0x08) * delta[c];
  172. pred = clampi(pred, -32768, 32767);
  173. samples[c][1] = samples[c][0];
  174. samples[c][0] = static_cast<ALshort>(pred);
  175. delta[c] = (MSADPCMAdaption[al::to_integer<ALubyte>(nibble)] * delta[c]) / 256;
  176. delta[c] = maxi(16, delta[c]);
  177. *(dst++) = static_cast<ALshort>(pred);
  178. }
  179. }
  180. }
  181. void Convert_ALshort_ALima4(ALshort *dst, const al::byte *src, size_t numchans, size_t len,
  182. size_t align)
  183. {
  184. const size_t byte_align{((align-1)/2 + 4) * numchans};
  185. len /= align;
  186. while(len--)
  187. {
  188. DecodeIMA4Block(dst, src, numchans, align);
  189. src += byte_align;
  190. dst += align*numchans;
  191. }
  192. }
  193. void Convert_ALshort_ALmsadpcm(ALshort *dst, const al::byte *src, size_t numchans, size_t len,
  194. size_t align)
  195. {
  196. const size_t byte_align{((align-2)/2 + 7) * numchans};
  197. len /= align;
  198. while(len--)
  199. {
  200. DecodeMSADPCMBlock(dst, src, numchans, align);
  201. src += byte_align;
  202. dst += align*numchans;
  203. }
  204. }
  205. ALuint BytesFromUserFmt(UserFmtType type)
  206. {
  207. switch(type)
  208. {
  209. case UserFmtUByte: return sizeof(ALubyte);
  210. case UserFmtShort: return sizeof(ALshort);
  211. case UserFmtFloat: return sizeof(ALfloat);
  212. case UserFmtDouble: return sizeof(ALdouble);
  213. case UserFmtMulaw: return sizeof(ALubyte);
  214. case UserFmtAlaw: return sizeof(ALubyte);
  215. case UserFmtIMA4: break; /* not handled here */
  216. case UserFmtMSADPCM: break; /* not handled here */
  217. }
  218. return 0;
  219. }
  220. ALuint ChannelsFromUserFmt(UserFmtChannels chans)
  221. {
  222. switch(chans)
  223. {
  224. case UserFmtMono: return 1;
  225. case UserFmtStereo: return 2;
  226. case UserFmtRear: return 2;
  227. case UserFmtQuad: return 4;
  228. case UserFmtX51: return 6;
  229. case UserFmtX61: return 7;
  230. case UserFmtX71: return 8;
  231. case UserFmtBFormat2D: return 3;
  232. case UserFmtBFormat3D: return 4;
  233. }
  234. return 0;
  235. }
  236. inline ALuint FrameSizeFromUserFmt(UserFmtChannels chans, UserFmtType type)
  237. { return ChannelsFromUserFmt(chans) * BytesFromUserFmt(type); }
  238. constexpr ALbitfieldSOFT INVALID_STORAGE_MASK{~unsigned(AL_MAP_READ_BIT_SOFT |
  239. AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT)};
  240. constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS{AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT};
  241. constexpr ALbitfieldSOFT INVALID_MAP_FLAGS{~unsigned(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT |
  242. AL_MAP_PERSISTENT_BIT_SOFT)};
  243. bool EnsureBuffers(ALCdevice *device, size_t needed)
  244. {
  245. size_t count{std::accumulate(device->BufferList.cbegin(), device->BufferList.cend(), size_t{0},
  246. [](size_t cur, const BufferSubList &sublist) noexcept -> size_t
  247. { return cur + static_cast<ALuint>(POPCNT64(sublist.FreeMask)); }
  248. )};
  249. while(needed > count)
  250. {
  251. if UNLIKELY(device->BufferList.size() >= 1<<25)
  252. return false;
  253. device->BufferList.emplace_back();
  254. auto sublist = device->BufferList.end() - 1;
  255. sublist->FreeMask = ~0_u64;
  256. sublist->Buffers = static_cast<ALbuffer*>(al_calloc(alignof(ALbuffer), sizeof(ALbuffer)*64));
  257. if UNLIKELY(!sublist->Buffers)
  258. {
  259. device->BufferList.pop_back();
  260. return false;
  261. }
  262. count += 64;
  263. }
  264. return true;
  265. }
  266. ALbuffer *AllocBuffer(ALCdevice *device)
  267. {
  268. auto sublist = std::find_if(device->BufferList.begin(), device->BufferList.end(),
  269. [](const BufferSubList &entry) noexcept -> bool
  270. { return entry.FreeMask != 0; }
  271. );
  272. auto lidx = static_cast<ALuint>(std::distance(device->BufferList.begin(), sublist));
  273. auto slidx = static_cast<ALuint>(CTZ64(sublist->FreeMask));
  274. ALbuffer *buffer{::new (sublist->Buffers + slidx) ALbuffer{}};
  275. /* Add 1 to avoid buffer ID 0. */
  276. buffer->id = ((lidx<<6) | slidx) + 1;
  277. sublist->FreeMask &= ~(1_u64 << slidx);
  278. return buffer;
  279. }
  280. void FreeBuffer(ALCdevice *device, ALbuffer *buffer)
  281. {
  282. const ALuint id{buffer->id - 1};
  283. const size_t lidx{id >> 6};
  284. const ALuint slidx{id & 0x3f};
  285. al::destroy_at(buffer);
  286. device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
  287. }
  288. inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
  289. {
  290. const size_t lidx{(id-1) >> 6};
  291. const ALuint slidx{(id-1) & 0x3f};
  292. if UNLIKELY(lidx >= device->BufferList.size())
  293. return nullptr;
  294. BufferSubList &sublist = device->BufferList[lidx];
  295. if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
  296. return nullptr;
  297. return sublist.Buffers + slidx;
  298. }
  299. ALuint SanitizeAlignment(UserFmtType type, ALuint align)
  300. {
  301. if(align == 0)
  302. {
  303. if(type == UserFmtIMA4)
  304. {
  305. /* Here is where things vary:
  306. * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
  307. * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
  308. */
  309. return 65;
  310. }
  311. if(type == UserFmtMSADPCM)
  312. return 64;
  313. return 1;
  314. }
  315. if(type == UserFmtIMA4)
  316. {
  317. /* IMA4 block alignment must be a multiple of 8, plus 1. */
  318. if((align&7) == 1) return static_cast<ALuint>(align);
  319. return 0;
  320. }
  321. if(type == UserFmtMSADPCM)
  322. {
  323. /* MSADPCM block alignment must be a multiple of 2. */
  324. if((align&1) == 0) return static_cast<ALuint>(align);
  325. return 0;
  326. }
  327. return static_cast<ALuint>(align);
  328. }
  329. const ALchar *NameFromUserFmtType(UserFmtType type)
  330. {
  331. switch(type)
  332. {
  333. case UserFmtUByte: return "Unsigned Byte";
  334. case UserFmtShort: return "Signed Short";
  335. case UserFmtFloat: return "Float32";
  336. case UserFmtDouble: return "Float64";
  337. case UserFmtMulaw: return "muLaw";
  338. case UserFmtAlaw: return "aLaw";
  339. case UserFmtIMA4: return "IMA4 ADPCM";
  340. case UserFmtMSADPCM: return "MSADPCM";
  341. }
  342. return "<internal type error>";
  343. }
  344. /** Loads the specified data into the buffer, using the specified format. */
  345. void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size,
  346. UserFmtChannels SrcChannels, UserFmtType SrcType, const al::byte *SrcData,
  347. ALbitfieldSOFT access)
  348. {
  349. if UNLIKELY(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0)
  350. SETERR_RETURN(context, AL_INVALID_OPERATION,, "Modifying storage for in-use buffer %u",
  351. ALBuf->id);
  352. /* Currently no channel configurations need to be converted. */
  353. FmtChannels DstChannels{FmtMono};
  354. switch(SrcChannels)
  355. {
  356. case UserFmtMono: DstChannels = FmtMono; break;
  357. case UserFmtStereo: DstChannels = FmtStereo; break;
  358. case UserFmtRear: DstChannels = FmtRear; break;
  359. case UserFmtQuad: DstChannels = FmtQuad; break;
  360. case UserFmtX51: DstChannels = FmtX51; break;
  361. case UserFmtX61: DstChannels = FmtX61; break;
  362. case UserFmtX71: DstChannels = FmtX71; break;
  363. case UserFmtBFormat2D: DstChannels = FmtBFormat2D; break;
  364. case UserFmtBFormat3D: DstChannels = FmtBFormat3D; break;
  365. }
  366. if UNLIKELY(static_cast<long>(SrcChannels) != static_cast<long>(DstChannels))
  367. SETERR_RETURN(context, AL_INVALID_ENUM, , "Invalid format");
  368. /* IMA4 and MSADPCM convert to 16-bit short. */
  369. FmtType DstType{FmtUByte};
  370. switch(SrcType)
  371. {
  372. case UserFmtUByte: DstType = FmtUByte; break;
  373. case UserFmtShort: DstType = FmtShort; break;
  374. case UserFmtFloat: DstType = FmtFloat; break;
  375. case UserFmtDouble: DstType = FmtDouble; break;
  376. case UserFmtAlaw: DstType = FmtAlaw; break;
  377. case UserFmtMulaw: DstType = FmtMulaw; break;
  378. case UserFmtIMA4: DstType = FmtShort; break;
  379. case UserFmtMSADPCM: DstType = FmtShort; break;
  380. }
  381. /* TODO: Currently we can only map samples when they're not converted. To
  382. * allow it would need some kind of double-buffering to hold onto a copy of
  383. * the original data.
  384. */
  385. if((access&MAP_READ_WRITE_FLAGS))
  386. {
  387. if UNLIKELY(static_cast<long>(SrcType) != static_cast<long>(DstType))
  388. SETERR_RETURN(context, AL_INVALID_VALUE,, "%s samples cannot be mapped",
  389. NameFromUserFmtType(SrcType));
  390. }
  391. const ALuint unpackalign{ALBuf->UnpackAlign};
  392. const ALuint align{SanitizeAlignment(SrcType, unpackalign)};
  393. if UNLIKELY(align < 1)
  394. SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid unpack alignment %u for %s samples",
  395. unpackalign, NameFromUserFmtType(SrcType));
  396. if((access&AL_PRESERVE_DATA_BIT_SOFT))
  397. {
  398. /* Can only preserve data with the same format and alignment. */
  399. if UNLIKELY(ALBuf->mFmtChannels != DstChannels || ALBuf->OriginalType != SrcType)
  400. SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched format");
  401. if UNLIKELY(ALBuf->OriginalAlign != align)
  402. SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched alignment");
  403. }
  404. /* Convert the input/source size in bytes to sample frames using the unpack
  405. * block alignment.
  406. */
  407. const ALuint SrcByteAlign{
  408. (SrcType == UserFmtIMA4) ? ((align-1)/2 + 4) * ChannelsFromUserFmt(SrcChannels) :
  409. (SrcType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * ChannelsFromUserFmt(SrcChannels) :
  410. (align * FrameSizeFromUserFmt(SrcChannels, SrcType))
  411. };
  412. if UNLIKELY((size%SrcByteAlign) != 0)
  413. SETERR_RETURN(context, AL_INVALID_VALUE,,
  414. "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
  415. size, SrcByteAlign, align);
  416. if UNLIKELY(size/SrcByteAlign > std::numeric_limits<ALsizei>::max()/align)
  417. SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
  418. "Buffer size overflow, %d blocks x %d samples per block", size/SrcByteAlign, align);
  419. const ALuint frames{size / SrcByteAlign * align};
  420. /* Convert the sample frames to the number of bytes needed for internal
  421. * storage.
  422. */
  423. ALuint NumChannels{ChannelsFromFmt(DstChannels)};
  424. ALuint FrameSize{NumChannels * BytesFromFmt(DstType)};
  425. if UNLIKELY(frames > std::numeric_limits<size_t>::max()/FrameSize)
  426. SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
  427. "Buffer size overflow, %d frames x %d bytes per frame", frames, FrameSize);
  428. size_t newsize{static_cast<size_t>(frames) * FrameSize};
  429. /* Round up to the next 16-byte multiple. This could reallocate only when
  430. * increasing or the new size is less than half the current, but then the
  431. * buffer's AL_SIZE would not be very reliable for accounting buffer memory
  432. * usage, and reporting the real size could cause problems for apps that
  433. * use AL_SIZE to try to get the buffer's play length.
  434. */
  435. newsize = RoundUp(newsize, 16);
  436. if(newsize != ALBuf->mData.size())
  437. {
  438. auto newdata = al::vector<al::byte,16>(newsize, al::byte{});
  439. if((access&AL_PRESERVE_DATA_BIT_SOFT))
  440. {
  441. const size_t tocopy{minz(newdata.size(), ALBuf->mData.size())};
  442. std::copy_n(ALBuf->mData.begin(), tocopy, newdata.begin());
  443. }
  444. ALBuf->mData = std::move(newdata);
  445. }
  446. if(SrcType == UserFmtIMA4)
  447. {
  448. assert(DstType == FmtShort);
  449. if(SrcData != nullptr && !ALBuf->mData.empty())
  450. Convert_ALshort_ALima4(reinterpret_cast<ALshort*>(ALBuf->mData.data()),
  451. SrcData, NumChannels, frames, align);
  452. ALBuf->OriginalAlign = align;
  453. }
  454. else if(SrcType == UserFmtMSADPCM)
  455. {
  456. assert(DstType == FmtShort);
  457. if(SrcData != nullptr && !ALBuf->mData.empty())
  458. Convert_ALshort_ALmsadpcm(reinterpret_cast<ALshort*>(ALBuf->mData.data()),
  459. SrcData, NumChannels, frames, align);
  460. ALBuf->OriginalAlign = align;
  461. }
  462. else
  463. {
  464. assert(static_cast<long>(SrcType) == static_cast<long>(DstType));
  465. if(SrcData != nullptr && !ALBuf->mData.empty())
  466. std::copy_n(SrcData, frames*FrameSize, ALBuf->mData.begin());
  467. ALBuf->OriginalAlign = 1;
  468. }
  469. ALBuf->OriginalSize = size;
  470. ALBuf->OriginalType = SrcType;
  471. ALBuf->Frequency = static_cast<ALuint>(freq);
  472. ALBuf->mFmtChannels = DstChannels;
  473. ALBuf->mFmtType = DstType;
  474. ALBuf->Access = access;
  475. ALBuf->SampleLen = frames;
  476. ALBuf->LoopStart = 0;
  477. ALBuf->LoopEnd = ALBuf->SampleLen;
  478. }
  479. struct DecompResult { UserFmtChannels channels; UserFmtType type; };
  480. al::optional<DecompResult> DecomposeUserFormat(ALenum format)
  481. {
  482. struct FormatMap {
  483. ALenum format;
  484. UserFmtChannels channels;
  485. UserFmtType type;
  486. };
  487. static const std::array<FormatMap,46> UserFmtList{{
  488. { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
  489. { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
  490. { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
  491. { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
  492. { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
  493. { AL_FORMAT_MONO_MSADPCM_SOFT, UserFmtMono, UserFmtMSADPCM },
  494. { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
  495. { AL_FORMAT_MONO_ALAW_EXT, UserFmtMono, UserFmtAlaw },
  496. { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
  497. { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
  498. { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
  499. { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
  500. { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
  501. { AL_FORMAT_STEREO_MSADPCM_SOFT, UserFmtStereo, UserFmtMSADPCM },
  502. { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
  503. { AL_FORMAT_STEREO_ALAW_EXT, UserFmtStereo, UserFmtAlaw },
  504. { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
  505. { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
  506. { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
  507. { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
  508. { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
  509. { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
  510. { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
  511. { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
  512. { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
  513. { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
  514. { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
  515. { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
  516. { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
  517. { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
  518. { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
  519. { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
  520. { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
  521. { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
  522. { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
  523. { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
  524. { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
  525. { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
  526. { AL_FORMAT_BFORMAT2D_8, UserFmtBFormat2D, UserFmtUByte },
  527. { AL_FORMAT_BFORMAT2D_16, UserFmtBFormat2D, UserFmtShort },
  528. { AL_FORMAT_BFORMAT2D_FLOAT32, UserFmtBFormat2D, UserFmtFloat },
  529. { AL_FORMAT_BFORMAT2D_MULAW, UserFmtBFormat2D, UserFmtMulaw },
  530. { AL_FORMAT_BFORMAT3D_8, UserFmtBFormat3D, UserFmtUByte },
  531. { AL_FORMAT_BFORMAT3D_16, UserFmtBFormat3D, UserFmtShort },
  532. { AL_FORMAT_BFORMAT3D_FLOAT32, UserFmtBFormat3D, UserFmtFloat },
  533. { AL_FORMAT_BFORMAT3D_MULAW, UserFmtBFormat3D, UserFmtMulaw },
  534. }};
  535. for(const auto &fmt : UserFmtList)
  536. {
  537. if(fmt.format == format)
  538. return al::make_optional<DecompResult>({fmt.channels, fmt.type});
  539. }
  540. return al::nullopt;
  541. }
  542. } // namespace
  543. AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
  544. START_API_FUNC
  545. {
  546. ContextRef context{GetContextRef()};
  547. if UNLIKELY(!context) return;
  548. if UNLIKELY(n < 0)
  549. context->setError(AL_INVALID_VALUE, "Generating %d buffers", n);
  550. if UNLIKELY(n <= 0) return;
  551. ALCdevice *device{context->mDevice.get()};
  552. std::lock_guard<std::mutex> _{device->BufferLock};
  553. if(!EnsureBuffers(device, static_cast<ALuint>(n)))
  554. {
  555. context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d buffer%s", n, (n==1)?"":"s");
  556. return;
  557. }
  558. if LIKELY(n == 1)
  559. {
  560. /* Special handling for the easy and normal case. */
  561. ALbuffer *buffer{AllocBuffer(device)};
  562. buffers[0] = buffer->id;
  563. }
  564. else
  565. {
  566. /* Store the allocated buffer IDs in a separate local list, to avoid
  567. * modifying the user storage in case of failure.
  568. */
  569. al::vector<ALuint> ids;
  570. ids.reserve(static_cast<ALuint>(n));
  571. do {
  572. ALbuffer *buffer{AllocBuffer(device)};
  573. ids.emplace_back(buffer->id);
  574. } while(--n);
  575. std::copy(ids.begin(), ids.end(), buffers);
  576. }
  577. }
  578. END_API_FUNC
  579. AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
  580. START_API_FUNC
  581. {
  582. ContextRef context{GetContextRef()};
  583. if UNLIKELY(!context) return;
  584. if UNLIKELY(n < 0)
  585. context->setError(AL_INVALID_VALUE, "Deleting %d buffers", n);
  586. if UNLIKELY(n <= 0) return;
  587. ALCdevice *device{context->mDevice.get()};
  588. std::lock_guard<std::mutex> _{device->BufferLock};
  589. /* First try to find any buffers that are invalid or in-use. */
  590. auto validate_buffer = [device, &context](const ALuint bid) -> bool
  591. {
  592. if(!bid) return true;
  593. ALbuffer *ALBuf{LookupBuffer(device, bid)};
  594. if UNLIKELY(!ALBuf)
  595. {
  596. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", bid);
  597. return false;
  598. }
  599. if UNLIKELY(ReadRef(ALBuf->ref) != 0)
  600. {
  601. context->setError(AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
  602. return false;
  603. }
  604. return true;
  605. };
  606. const ALuint *buffers_end = buffers + n;
  607. auto invbuf = std::find_if_not(buffers, buffers_end, validate_buffer);
  608. if UNLIKELY(invbuf != buffers_end) return;
  609. /* All good. Delete non-0 buffer IDs. */
  610. auto delete_buffer = [device](const ALuint bid) -> void
  611. {
  612. ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr};
  613. if(buffer) FreeBuffer(device, buffer);
  614. };
  615. std::for_each(buffers, buffers_end, delete_buffer);
  616. }
  617. END_API_FUNC
  618. AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
  619. START_API_FUNC
  620. {
  621. ContextRef context{GetContextRef()};
  622. if LIKELY(context)
  623. {
  624. ALCdevice *device{context->mDevice.get()};
  625. std::lock_guard<std::mutex> _{device->BufferLock};
  626. if(!buffer || LookupBuffer(device, buffer))
  627. return AL_TRUE;
  628. }
  629. return AL_FALSE;
  630. }
  631. END_API_FUNC
  632. AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
  633. START_API_FUNC
  634. { alBufferStorageSOFT(buffer, format, data, size, freq, 0); }
  635. END_API_FUNC
  636. AL_API void AL_APIENTRY alBufferStorageSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags)
  637. START_API_FUNC
  638. {
  639. ContextRef context{GetContextRef()};
  640. if UNLIKELY(!context) return;
  641. ALCdevice *device{context->mDevice.get()};
  642. std::lock_guard<std::mutex> _{device->BufferLock};
  643. ALbuffer *albuf = LookupBuffer(device, buffer);
  644. if UNLIKELY(!albuf)
  645. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  646. else if UNLIKELY(size < 0)
  647. context->setError(AL_INVALID_VALUE, "Negative storage size %d", size);
  648. else if UNLIKELY(freq < 1)
  649. context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
  650. else if UNLIKELY((flags&INVALID_STORAGE_MASK) != 0)
  651. context->setError(AL_INVALID_VALUE, "Invalid storage flags 0x%x",
  652. flags&INVALID_STORAGE_MASK);
  653. else if UNLIKELY((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS))
  654. context->setError(AL_INVALID_VALUE,
  655. "Declaring persistently mapped storage without read or write access");
  656. else
  657. {
  658. auto usrfmt = DecomposeUserFormat(format);
  659. if UNLIKELY(!usrfmt)
  660. context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
  661. else
  662. LoadData(context.get(), albuf, freq, static_cast<ALuint>(size), usrfmt->channels,
  663. usrfmt->type, static_cast<const al::byte*>(data), flags);
  664. }
  665. }
  666. END_API_FUNC
  667. AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length, ALbitfieldSOFT access)
  668. START_API_FUNC
  669. {
  670. ContextRef context{GetContextRef()};
  671. if UNLIKELY(!context) return nullptr;
  672. ALCdevice *device{context->mDevice.get()};
  673. std::lock_guard<std::mutex> _{device->BufferLock};
  674. ALbuffer *albuf = LookupBuffer(device, buffer);
  675. if UNLIKELY(!albuf)
  676. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  677. else if UNLIKELY((access&INVALID_MAP_FLAGS) != 0)
  678. context->setError(AL_INVALID_VALUE, "Invalid map flags 0x%x", access&INVALID_MAP_FLAGS);
  679. else if UNLIKELY(!(access&MAP_READ_WRITE_FLAGS))
  680. context->setError(AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
  681. buffer);
  682. else
  683. {
  684. ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
  685. if UNLIKELY(ReadRef(albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT))
  686. context->setError(AL_INVALID_OPERATION,
  687. "Mapping in-use buffer %u without persistent mapping", buffer);
  688. else if UNLIKELY(albuf->MappedAccess != 0)
  689. context->setError(AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer);
  690. else if UNLIKELY((unavailable&AL_MAP_READ_BIT_SOFT))
  691. context->setError(AL_INVALID_VALUE,
  692. "Mapping buffer %u for reading without read access", buffer);
  693. else if UNLIKELY((unavailable&AL_MAP_WRITE_BIT_SOFT))
  694. context->setError(AL_INVALID_VALUE,
  695. "Mapping buffer %u for writing without write access", buffer);
  696. else if UNLIKELY((unavailable&AL_MAP_PERSISTENT_BIT_SOFT))
  697. context->setError(AL_INVALID_VALUE,
  698. "Mapping buffer %u persistently without persistent access", buffer);
  699. else if UNLIKELY(offset < 0 || length <= 0
  700. || static_cast<ALuint>(offset) >= albuf->OriginalSize
  701. || static_cast<ALuint>(length) > albuf->OriginalSize - static_cast<ALuint>(offset))
  702. context->setError(AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
  703. offset, length, buffer);
  704. else
  705. {
  706. void *retval = albuf->mData.data() + offset;
  707. albuf->MappedAccess = access;
  708. albuf->MappedOffset = offset;
  709. albuf->MappedSize = length;
  710. return retval;
  711. }
  712. }
  713. return nullptr;
  714. }
  715. END_API_FUNC
  716. AL_API void AL_APIENTRY alUnmapBufferSOFT(ALuint buffer)
  717. START_API_FUNC
  718. {
  719. ContextRef context{GetContextRef()};
  720. if UNLIKELY(!context) return;
  721. ALCdevice *device{context->mDevice.get()};
  722. std::lock_guard<std::mutex> _{device->BufferLock};
  723. ALbuffer *albuf = LookupBuffer(device, buffer);
  724. if UNLIKELY(!albuf)
  725. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  726. else if UNLIKELY(albuf->MappedAccess == 0)
  727. context->setError(AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer);
  728. else
  729. {
  730. albuf->MappedAccess = 0;
  731. albuf->MappedOffset = 0;
  732. albuf->MappedSize = 0;
  733. }
  734. }
  735. END_API_FUNC
  736. AL_API void AL_APIENTRY alFlushMappedBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length)
  737. START_API_FUNC
  738. {
  739. ContextRef context{GetContextRef()};
  740. if UNLIKELY(!context) return;
  741. ALCdevice *device{context->mDevice.get()};
  742. std::lock_guard<std::mutex> _{device->BufferLock};
  743. ALbuffer *albuf = LookupBuffer(device, buffer);
  744. if UNLIKELY(!albuf)
  745. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  746. else if UNLIKELY(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT))
  747. context->setError(AL_INVALID_OPERATION, "Flushing buffer %u while not mapped for writing",
  748. buffer);
  749. else if UNLIKELY(offset < albuf->MappedOffset || length <= 0
  750. || offset >= albuf->MappedOffset+albuf->MappedSize
  751. || length > albuf->MappedOffset+albuf->MappedSize-offset)
  752. context->setError(AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u", offset,
  753. length, buffer);
  754. else
  755. {
  756. /* FIXME: Need to use some method of double-buffering for the mixer and
  757. * app to hold separate memory, which can be safely transfered
  758. * asynchronously. Currently we just say the app shouldn't write where
  759. * OpenAL's reading, and hope for the best...
  760. */
  761. std::atomic_thread_fence(std::memory_order_seq_cst);
  762. }
  763. }
  764. END_API_FUNC
  765. AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
  766. START_API_FUNC
  767. {
  768. ContextRef context{GetContextRef()};
  769. if UNLIKELY(!context) return;
  770. ALCdevice *device{context->mDevice.get()};
  771. std::lock_guard<std::mutex> _{device->BufferLock};
  772. ALbuffer *albuf = LookupBuffer(device, buffer);
  773. if UNLIKELY(!albuf)
  774. {
  775. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  776. return;
  777. }
  778. auto usrfmt = DecomposeUserFormat(format);
  779. if UNLIKELY(!usrfmt)
  780. {
  781. context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
  782. return;
  783. }
  784. ALuint unpack_align{albuf->UnpackAlign};
  785. ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)};
  786. if UNLIKELY(align < 1)
  787. context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align);
  788. else if UNLIKELY(long{usrfmt->channels} != long{albuf->mFmtChannels}
  789. || usrfmt->type != albuf->OriginalType)
  790. context->setError(AL_INVALID_ENUM, "Unpacking data with mismatched format");
  791. else if UNLIKELY(align != albuf->OriginalAlign)
  792. context->setError(AL_INVALID_VALUE,
  793. "Unpacking data with alignment %u does not match original alignment %u", align,
  794. albuf->OriginalAlign);
  795. else if UNLIKELY(albuf->MappedAccess != 0)
  796. context->setError(AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u", buffer);
  797. else
  798. {
  799. ALuint num_chans{ChannelsFromFmt(albuf->mFmtChannels)};
  800. ALuint frame_size{num_chans * BytesFromFmt(albuf->mFmtType)};
  801. ALuint byte_align{
  802. (albuf->OriginalType == UserFmtIMA4) ? ((align-1)/2 + 4) * num_chans :
  803. (albuf->OriginalType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
  804. (align * frame_size)
  805. };
  806. if UNLIKELY(offset < 0 || length < 0 || static_cast<ALuint>(offset) > albuf->OriginalSize
  807. || static_cast<ALuint>(length) > albuf->OriginalSize-static_cast<ALuint>(offset))
  808. context->setError(AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
  809. offset, length, buffer);
  810. else if UNLIKELY((static_cast<ALuint>(offset)%byte_align) != 0)
  811. context->setError(AL_INVALID_VALUE,
  812. "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
  813. offset, byte_align, align);
  814. else if UNLIKELY((static_cast<ALuint>(length)%byte_align) != 0)
  815. context->setError(AL_INVALID_VALUE,
  816. "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
  817. length, byte_align, align);
  818. else
  819. {
  820. /* offset -> byte offset, length -> sample count */
  821. size_t byteoff{static_cast<ALuint>(offset)/byte_align * align * frame_size};
  822. size_t samplen{static_cast<ALuint>(length)/byte_align * align};
  823. void *dst = albuf->mData.data() + byteoff;
  824. if(usrfmt->type == UserFmtIMA4 && albuf->mFmtType == FmtShort)
  825. Convert_ALshort_ALima4(static_cast<ALshort*>(dst),
  826. static_cast<const al::byte*>(data), num_chans, samplen, align);
  827. else if(usrfmt->type == UserFmtMSADPCM && albuf->mFmtType == FmtShort)
  828. Convert_ALshort_ALmsadpcm(static_cast<ALshort*>(dst),
  829. static_cast<const al::byte*>(data), num_chans, samplen, align);
  830. else
  831. {
  832. assert(long{usrfmt->type} == long{albuf->mFmtType});
  833. memcpy(dst, data, size_t{samplen} * frame_size);
  834. }
  835. }
  836. }
  837. }
  838. END_API_FUNC
  839. AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint /*buffer*/, ALuint /*samplerate*/,
  840. ALenum /*internalformat*/, ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/,
  841. const ALvoid* /*data*/)
  842. START_API_FUNC
  843. {
  844. ContextRef context{GetContextRef()};
  845. if UNLIKELY(!context) return;
  846. context->setError(AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
  847. }
  848. END_API_FUNC
  849. AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
  850. ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, const ALvoid* /*data*/)
  851. START_API_FUNC
  852. {
  853. ContextRef context{GetContextRef()};
  854. if UNLIKELY(!context) return;
  855. context->setError(AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
  856. }
  857. END_API_FUNC
  858. AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
  859. ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, ALvoid* /*data*/)
  860. START_API_FUNC
  861. {
  862. ContextRef context{GetContextRef()};
  863. if UNLIKELY(!context) return;
  864. context->setError(AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
  865. }
  866. END_API_FUNC
  867. AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum /*format*/)
  868. START_API_FUNC
  869. {
  870. ContextRef context{GetContextRef()};
  871. if UNLIKELY(!context) return AL_FALSE;
  872. context->setError(AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
  873. return AL_FALSE;
  874. }
  875. END_API_FUNC
  876. AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat /*value*/)
  877. START_API_FUNC
  878. {
  879. ContextRef context{GetContextRef()};
  880. if UNLIKELY(!context) return;
  881. ALCdevice *device{context->mDevice.get()};
  882. std::lock_guard<std::mutex> _{device->BufferLock};
  883. if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
  884. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  885. else switch(param)
  886. {
  887. default:
  888. context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
  889. }
  890. }
  891. END_API_FUNC
  892. AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param,
  893. ALfloat /*value1*/, ALfloat /*value2*/, ALfloat /*value3*/)
  894. START_API_FUNC
  895. {
  896. ContextRef context{GetContextRef()};
  897. if UNLIKELY(!context) return;
  898. ALCdevice *device{context->mDevice.get()};
  899. std::lock_guard<std::mutex> _{device->BufferLock};
  900. if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
  901. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  902. else switch(param)
  903. {
  904. default:
  905. context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
  906. }
  907. }
  908. END_API_FUNC
  909. AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
  910. START_API_FUNC
  911. {
  912. ContextRef context{GetContextRef()};
  913. if UNLIKELY(!context) return;
  914. ALCdevice *device{context->mDevice.get()};
  915. std::lock_guard<std::mutex> _{device->BufferLock};
  916. if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
  917. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  918. else if UNLIKELY(!values)
  919. context->setError(AL_INVALID_VALUE, "NULL pointer");
  920. else switch(param)
  921. {
  922. default:
  923. context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
  924. }
  925. }
  926. END_API_FUNC
  927. AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
  928. START_API_FUNC
  929. {
  930. ContextRef context{GetContextRef()};
  931. if UNLIKELY(!context) return;
  932. ALCdevice *device{context->mDevice.get()};
  933. std::lock_guard<std::mutex> _{device->BufferLock};
  934. ALbuffer *albuf = LookupBuffer(device, buffer);
  935. if UNLIKELY(!albuf)
  936. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  937. else switch(param)
  938. {
  939. case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
  940. if UNLIKELY(value < 0)
  941. context->setError(AL_INVALID_VALUE, "Invalid unpack block alignment %d", value);
  942. else
  943. albuf->UnpackAlign = static_cast<ALuint>(value);
  944. break;
  945. case AL_PACK_BLOCK_ALIGNMENT_SOFT:
  946. if UNLIKELY(value < 0)
  947. context->setError(AL_INVALID_VALUE, "Invalid pack block alignment %d", value);
  948. else
  949. albuf->PackAlign = static_cast<ALuint>(value);
  950. break;
  951. default:
  952. context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
  953. }
  954. }
  955. END_API_FUNC
  956. AL_API void AL_APIENTRY alBuffer3i(ALuint buffer, ALenum param,
  957. ALint /*value1*/, ALint /*value2*/, ALint /*value3*/)
  958. START_API_FUNC
  959. {
  960. ContextRef context{GetContextRef()};
  961. if UNLIKELY(!context) return;
  962. ALCdevice *device{context->mDevice.get()};
  963. std::lock_guard<std::mutex> _{device->BufferLock};
  964. if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
  965. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  966. else switch(param)
  967. {
  968. default:
  969. context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
  970. }
  971. }
  972. END_API_FUNC
  973. AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
  974. START_API_FUNC
  975. {
  976. if(values)
  977. {
  978. switch(param)
  979. {
  980. case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
  981. case AL_PACK_BLOCK_ALIGNMENT_SOFT:
  982. alBufferi(buffer, param, values[0]);
  983. return;
  984. }
  985. }
  986. ContextRef context{GetContextRef()};
  987. if UNLIKELY(!context) return;
  988. ALCdevice *device{context->mDevice.get()};
  989. std::lock_guard<std::mutex> _{device->BufferLock};
  990. ALbuffer *albuf = LookupBuffer(device, buffer);
  991. if UNLIKELY(!albuf)
  992. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  993. else if UNLIKELY(!values)
  994. context->setError(AL_INVALID_VALUE, "NULL pointer");
  995. else switch(param)
  996. {
  997. case AL_LOOP_POINTS_SOFT:
  998. if UNLIKELY(ReadRef(albuf->ref) != 0)
  999. context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
  1000. buffer);
  1001. else if UNLIKELY(values[0] < 0 || values[0] >= values[1]
  1002. || static_cast<ALuint>(values[1]) > albuf->SampleLen)
  1003. context->setError(AL_INVALID_VALUE, "Invalid loop point range %d -> %d on buffer %u",
  1004. values[0], values[1], buffer);
  1005. else
  1006. {
  1007. albuf->LoopStart = static_cast<ALuint>(values[0]);
  1008. albuf->LoopEnd = static_cast<ALuint>(values[1]);
  1009. }
  1010. break;
  1011. default:
  1012. context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
  1013. }
  1014. }
  1015. END_API_FUNC
  1016. AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
  1017. START_API_FUNC
  1018. {
  1019. ContextRef context{GetContextRef()};
  1020. if UNLIKELY(!context) return;
  1021. ALCdevice *device{context->mDevice.get()};
  1022. std::lock_guard<std::mutex> _{device->BufferLock};
  1023. ALbuffer *albuf = LookupBuffer(device, buffer);
  1024. if UNLIKELY(!albuf)
  1025. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  1026. else if UNLIKELY(!value)
  1027. context->setError(AL_INVALID_VALUE, "NULL pointer");
  1028. else switch(param)
  1029. {
  1030. default:
  1031. context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
  1032. }
  1033. }
  1034. END_API_FUNC
  1035. AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
  1036. START_API_FUNC
  1037. {
  1038. ContextRef context{GetContextRef()};
  1039. if UNLIKELY(!context) return;
  1040. ALCdevice *device{context->mDevice.get()};
  1041. std::lock_guard<std::mutex> _{device->BufferLock};
  1042. if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
  1043. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  1044. else if UNLIKELY(!value1 || !value2 || !value3)
  1045. context->setError(AL_INVALID_VALUE, "NULL pointer");
  1046. else switch(param)
  1047. {
  1048. default:
  1049. context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
  1050. }
  1051. }
  1052. END_API_FUNC
  1053. AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
  1054. START_API_FUNC
  1055. {
  1056. switch(param)
  1057. {
  1058. case AL_SEC_LENGTH_SOFT:
  1059. alGetBufferf(buffer, param, values);
  1060. return;
  1061. }
  1062. ContextRef context{GetContextRef()};
  1063. if UNLIKELY(!context) return;
  1064. ALCdevice *device{context->mDevice.get()};
  1065. std::lock_guard<std::mutex> _{device->BufferLock};
  1066. if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
  1067. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  1068. else if UNLIKELY(!values)
  1069. context->setError(AL_INVALID_VALUE, "NULL pointer");
  1070. else switch(param)
  1071. {
  1072. default:
  1073. context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
  1074. }
  1075. }
  1076. END_API_FUNC
  1077. AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
  1078. START_API_FUNC
  1079. {
  1080. ContextRef context{GetContextRef()};
  1081. if UNLIKELY(!context) return;
  1082. ALCdevice *device{context->mDevice.get()};
  1083. std::lock_guard<std::mutex> _{device->BufferLock};
  1084. ALbuffer *albuf = LookupBuffer(device, buffer);
  1085. if UNLIKELY(!albuf)
  1086. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  1087. else if UNLIKELY(!value)
  1088. context->setError(AL_INVALID_VALUE, "NULL pointer");
  1089. else switch(param)
  1090. {
  1091. case AL_FREQUENCY:
  1092. *value = static_cast<ALint>(albuf->Frequency);
  1093. break;
  1094. case AL_BITS:
  1095. *value = static_cast<ALint>(BytesFromFmt(albuf->mFmtType) * 8);
  1096. break;
  1097. case AL_CHANNELS:
  1098. *value = static_cast<ALint>(ChannelsFromFmt(albuf->mFmtChannels));
  1099. break;
  1100. case AL_SIZE:
  1101. *value = static_cast<ALint>(albuf->SampleLen *
  1102. FrameSizeFromFmt(albuf->mFmtChannels, albuf->mFmtType));
  1103. break;
  1104. case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
  1105. *value = static_cast<ALint>(albuf->UnpackAlign);
  1106. break;
  1107. case AL_PACK_BLOCK_ALIGNMENT_SOFT:
  1108. *value = static_cast<ALint>(albuf->PackAlign);
  1109. break;
  1110. default:
  1111. context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
  1112. }
  1113. }
  1114. END_API_FUNC
  1115. AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
  1116. START_API_FUNC
  1117. {
  1118. ContextRef context{GetContextRef()};
  1119. if UNLIKELY(!context) return;
  1120. ALCdevice *device{context->mDevice.get()};
  1121. std::lock_guard<std::mutex> _{device->BufferLock};
  1122. if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
  1123. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  1124. else if UNLIKELY(!value1 || !value2 || !value3)
  1125. context->setError(AL_INVALID_VALUE, "NULL pointer");
  1126. else switch(param)
  1127. {
  1128. default:
  1129. context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
  1130. }
  1131. }
  1132. END_API_FUNC
  1133. AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
  1134. START_API_FUNC
  1135. {
  1136. switch(param)
  1137. {
  1138. case AL_FREQUENCY:
  1139. case AL_BITS:
  1140. case AL_CHANNELS:
  1141. case AL_SIZE:
  1142. case AL_INTERNAL_FORMAT_SOFT:
  1143. case AL_BYTE_LENGTH_SOFT:
  1144. case AL_SAMPLE_LENGTH_SOFT:
  1145. case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
  1146. case AL_PACK_BLOCK_ALIGNMENT_SOFT:
  1147. alGetBufferi(buffer, param, values);
  1148. return;
  1149. }
  1150. ContextRef context{GetContextRef()};
  1151. if UNLIKELY(!context) return;
  1152. ALCdevice *device{context->mDevice.get()};
  1153. std::lock_guard<std::mutex> _{device->BufferLock};
  1154. ALbuffer *albuf = LookupBuffer(device, buffer);
  1155. if UNLIKELY(!albuf)
  1156. context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  1157. else if UNLIKELY(!values)
  1158. context->setError(AL_INVALID_VALUE, "NULL pointer");
  1159. else switch(param)
  1160. {
  1161. case AL_LOOP_POINTS_SOFT:
  1162. values[0] = static_cast<ALint>(albuf->LoopStart);
  1163. values[1] = static_cast<ALint>(albuf->LoopEnd);
  1164. break;
  1165. default:
  1166. context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
  1167. }
  1168. }
  1169. END_API_FUNC
  1170. ALuint BytesFromFmt(FmtType type)
  1171. {
  1172. switch(type)
  1173. {
  1174. case FmtUByte: return sizeof(ALubyte);
  1175. case FmtShort: return sizeof(ALshort);
  1176. case FmtFloat: return sizeof(ALfloat);
  1177. case FmtDouble: return sizeof(ALdouble);
  1178. case FmtMulaw: return sizeof(ALubyte);
  1179. case FmtAlaw: return sizeof(ALubyte);
  1180. }
  1181. return 0;
  1182. }
  1183. ALuint ChannelsFromFmt(FmtChannels chans)
  1184. {
  1185. switch(chans)
  1186. {
  1187. case FmtMono: return 1;
  1188. case FmtStereo: return 2;
  1189. case FmtRear: return 2;
  1190. case FmtQuad: return 4;
  1191. case FmtX51: return 6;
  1192. case FmtX61: return 7;
  1193. case FmtX71: return 8;
  1194. case FmtBFormat2D: return 3;
  1195. case FmtBFormat3D: return 4;
  1196. }
  1197. return 0;
  1198. }
  1199. BufferSubList::~BufferSubList()
  1200. {
  1201. uint64_t usemask{~FreeMask};
  1202. while(usemask)
  1203. {
  1204. ALsizei idx{CTZ64(usemask)};
  1205. al::destroy_at(Buffers+idx);
  1206. usemask &= ~(1_u64 << idx);
  1207. }
  1208. FreeMask = ~usemask;
  1209. al_free(Buffers);
  1210. Buffers = nullptr;
  1211. }