aboutsummaryrefslogtreecommitdiffstats
path: root/OpenAL32/alBuffer.cpp
blob: 94de991c4c2500e07ae74943fdad196e3b3d61a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
/**
 * OpenAL cross platform audio library
 * Copyright (C) 1999-2007 by authors.
 * This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the
 *  Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * Or go to http://www.gnu.org/copyleft/lgpl.html
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif

#include <vector>
#include <algorithm>

#include "alMain.h"
#include "alu.h"
#include "alError.h"
#include "alBuffer.h"
#include "sample_cvt.h"


extern inline void LockBufferList(ALCdevice *device);
extern inline void UnlockBufferList(ALCdevice *device);
extern inline ALsizei FrameSizeFromUserFmt(enum UserFmtChannels chans, enum UserFmtType type);
extern inline ALsizei FrameSizeFromFmt(enum FmtChannels chans, enum FmtType type);

static ALbuffer *AllocBuffer(ALCcontext *context);
static void FreeBuffer(ALCdevice *device, ALbuffer *buffer);
static const ALchar *NameFromUserFmtType(enum UserFmtType type);
static void LoadData(ALCcontext *context, ALbuffer *buffer, ALuint freq, ALsizei size,
                     enum UserFmtChannels SrcChannels, enum UserFmtType SrcType,
                     const ALvoid *data, ALbitfieldSOFT access);
static ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans, enum UserFmtType *type);
static ALsizei SanitizeAlignment(enum UserFmtType type, ALsizei align);

static inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
{
    ALuint lidx = (id-1) >> 6;
    ALsizei slidx = (id-1) & 0x3f;

    if(UNLIKELY(lidx >= VECTOR_SIZE(device->BufferList)))
        return nullptr;
    BufferSubList *sublist = &VECTOR_ELEM(device->BufferList, lidx);
    if(UNLIKELY(sublist->FreeMask & (U64(1)<<slidx)))
        return nullptr;
    return sublist->Buffers + slidx;
}

#define INVALID_STORAGE_MASK ~(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT)
#define MAP_READ_WRITE_FLAGS (AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT)
#define INVALID_MAP_FLAGS ~(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT)

AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    if(UNLIKELY(n < 0))
    {
        alSetError(context.get(), AL_INVALID_VALUE, "Generating %d buffers", n);
        return;
    }

    if(LIKELY(n == 1))
    {
        /* Special handling for the easy and normal case. */
        ALbuffer *buffer = AllocBuffer(context.get());
        if(buffer) buffers[0] = buffer->id;
    }
    else if(n > 1)
    {
        /* Store the allocated buffer IDs in a separate local list, to avoid
         * modifying the user storage in case of failure.
         */
        std::vector<ALuint> ids;
        ids.reserve(n);
        do {
            ALbuffer *buffer = AllocBuffer(context.get());
            if(!buffer)
            {
                alDeleteBuffers(ids.size(), ids.data());
                return;
            }

            ids.emplace_back(buffer->id);
        } while(--n);
        std::copy(ids.begin(), ids.end(), buffers);
    }
}

AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    if(UNLIKELY(n < 0))
    {
        alSetError(context.get(), AL_INVALID_VALUE, "Deleting %d buffers", n);
        return;
    }
    if(UNLIKELY(n == 0))
        return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    /* First try to find any buffers that are invalid or in-use. */
    const ALuint *buffers_end = buffers + n;
    auto invbuf = std::find_if(buffers, buffers_end,
        [device, &context](ALuint bid) -> bool
        {
            if(!bid) return false;
            ALbuffer *ALBuf = LookupBuffer(device, bid);
            if(UNLIKELY(!ALBuf))
            {
                alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", bid);
                return true;
            }
            if(UNLIKELY(ReadRef(&ALBuf->ref) != 0))
            {
                alSetError(context.get(), AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
                return true;
            }
            return false;
        }
    );
    if(LIKELY(invbuf == buffers_end))
    {
        /* All good. Delete non-0 buffer IDs. */
        std::for_each(buffers, buffers_end,
            [device](ALuint bid) -> void
            { if(bid) FreeBuffer(device, LookupBuffer(device, bid)); }
        );
    }
}

AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
{
    ContextRef context{GetContextRef()};
    if(LIKELY(context))
    {
        ALCdevice *device = context->Device;
        std::lock_guard<almtx_t> _{device->BufferLock};
        if(!buffer || LookupBuffer(device, buffer))
            return AL_TRUE;
    }
    return AL_FALSE;
}


AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
{ alBufferStorageSOFT(buffer, format, data, size, freq, 0); }

AL_API void AL_APIENTRY alBufferStorageSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    enum UserFmtChannels srcchannels = UserFmtMono;
    enum UserFmtType srctype = UserFmtUByte;
    ALbuffer *albuf;

    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(size < 0))
        alSetError(context.get(), AL_INVALID_VALUE, "Negative storage size %d", size);
    else if(UNLIKELY(freq < 1))
        alSetError(context.get(), AL_INVALID_VALUE, "Invalid sample rate %d", freq);
    else if(UNLIKELY((flags&INVALID_STORAGE_MASK) != 0))
        alSetError(context.get(), AL_INVALID_VALUE, "Invalid storage flags 0x%x",
                   flags&INVALID_STORAGE_MASK);
    else if(UNLIKELY((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS)))
        alSetError(context.get(), AL_INVALID_VALUE,
                   "Declaring persistently mapped storage without read or write access");
    else if(UNLIKELY(DecomposeUserFormat(format, &srcchannels, &srctype) == AL_FALSE))
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid format 0x%04x", format);
    else
        LoadData(context.get(), albuf, freq, size, srcchannels, srctype, data, flags);
}

AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length, ALbitfieldSOFT access)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return nullptr;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    ALbuffer *albuf;
    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY((access&INVALID_MAP_FLAGS) != 0))
        alSetError(context.get(), AL_INVALID_VALUE, "Invalid map flags 0x%x", access&INVALID_MAP_FLAGS);
    else if(UNLIKELY(!(access&MAP_READ_WRITE_FLAGS)))
        alSetError(context.get(), AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
                   buffer);
    else
    {
        ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
        if(UNLIKELY(ReadRef(&albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT)))
            alSetError(context.get(), AL_INVALID_OPERATION,
                       "Mapping in-use buffer %u without persistent mapping", buffer);
        else if(UNLIKELY(albuf->MappedAccess != 0))
            alSetError(context.get(), AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer);
        else if(UNLIKELY((unavailable&AL_MAP_READ_BIT_SOFT)))
            alSetError(context.get(), AL_INVALID_VALUE,
                       "Mapping buffer %u for reading without read access", buffer);
        else if(UNLIKELY((unavailable&AL_MAP_WRITE_BIT_SOFT)))
            alSetError(context.get(), AL_INVALID_VALUE,
                       "Mapping buffer %u for writing without write access", buffer);
        else if(UNLIKELY((unavailable&AL_MAP_PERSISTENT_BIT_SOFT)))
            alSetError(context.get(), AL_INVALID_VALUE,
                       "Mapping buffer %u persistently without persistent access", buffer);
        else if(UNLIKELY(offset < 0 || offset >= albuf->OriginalSize ||
                         length <= 0 || length > albuf->OriginalSize - offset))
            alSetError(context.get(), AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
                       offset, length, buffer);
        else
        {
            void *retval = (ALbyte*)albuf->data + offset;
            albuf->MappedAccess = access;
            albuf->MappedOffset = offset;
            albuf->MappedSize = length;
            return retval;
        }
    }

    return nullptr;
}

AL_API void AL_APIENTRY alUnmapBufferSOFT(ALuint buffer)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    ALbuffer *albuf;
    if((albuf=LookupBuffer(device, buffer)) == nullptr)
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(albuf->MappedAccess == 0)
        alSetError(context.get(), AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer);
    else
    {
        albuf->MappedAccess = 0;
        albuf->MappedOffset = 0;
        albuf->MappedSize = 0;
    }
}

AL_API void AL_APIENTRY alFlushMappedBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    ALbuffer *albuf;
    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT)))
        alSetError(context.get(), AL_INVALID_OPERATION,
                   "Flushing buffer %u while not mapped for writing", buffer);
    else if(UNLIKELY(offset < albuf->MappedOffset ||
                     offset >= albuf->MappedOffset+albuf->MappedSize ||
                     length <= 0 || length > albuf->MappedOffset+albuf->MappedSize-offset))
        alSetError(context.get(), AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u",
                   offset, length, buffer);
    else
    {
        /* FIXME: Need to use some method of double-buffering for the mixer and
         * app to hold separate memory, which can be safely transfered
         * asynchronously. Currently we just say the app shouldn't write where
         * OpenAL's reading, and hope for the best...
         */
        ATOMIC_THREAD_FENCE(almemory_order_seq_cst);
    }
}

AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    enum UserFmtChannels srcchannels = UserFmtMono;
    enum UserFmtType srctype = UserFmtUByte;
    ALbuffer *albuf;

    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(DecomposeUserFormat(format, &srcchannels, &srctype) == AL_FALSE))
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid format 0x%04x", format);
    else
    {
        ALsizei unpack_align, align;
        ALsizei byte_align;
        ALsizei frame_size;
        ALsizei num_chans;

        unpack_align = ATOMIC_LOAD_SEQ(&albuf->UnpackAlign);
        align = SanitizeAlignment(srctype, unpack_align);
        if(UNLIKELY(align < 1))
            alSetError(context.get(), AL_INVALID_VALUE, "Invalid unpack alignment %d", unpack_align);
        else if(UNLIKELY((long)srcchannels != (long)albuf->FmtChannels ||
                         srctype != albuf->OriginalType))
            alSetError(context.get(), AL_INVALID_ENUM, "Unpacking data with mismatched format");
        else if(UNLIKELY(align != albuf->OriginalAlign))
            alSetError(context.get(), AL_INVALID_VALUE,
                       "Unpacking data with alignment %u does not match original alignment %u",
                       align, albuf->OriginalAlign);
        else if(UNLIKELY(albuf->MappedAccess != 0))
            alSetError(context.get(), AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u",
                       buffer);
        else
        {
            num_chans = ChannelsFromFmt(albuf->FmtChannels);
            frame_size = num_chans * BytesFromFmt(albuf->FmtType);
            if(albuf->OriginalType == UserFmtIMA4)
                byte_align = ((align-1)/2 + 4) * num_chans;
            else if(albuf->OriginalType == UserFmtMSADPCM)
                byte_align = ((align-2)/2 + 7) * num_chans;
            else
                byte_align = align * frame_size;

            if(UNLIKELY(offset < 0 || length < 0 || offset > albuf->OriginalSize ||
                        length > albuf->OriginalSize-offset))
                alSetError(context.get(), AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
                            offset, length, buffer);
            else if(UNLIKELY((offset%byte_align) != 0))
                alSetError(context.get(), AL_INVALID_VALUE,
                    "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
                    offset, byte_align, align);
            else if(UNLIKELY((length%byte_align) != 0))
                alSetError(context.get(), AL_INVALID_VALUE,
                    "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
                    length, byte_align, align);
            else
            {
                /* offset -> byte offset, length -> sample count */
                offset = offset/byte_align * align * frame_size;
                length = length/byte_align * align;

                void *dst = static_cast<ALbyte*>(albuf->data) + offset;
                if(srctype == UserFmtIMA4 && albuf->FmtType == FmtShort)
                    Convert_ALshort_ALima4(static_cast<ALshort*>(dst),
                        static_cast<const ALubyte*>(data), num_chans, length, align);
                else if(srctype == UserFmtMSADPCM && albuf->FmtType == FmtShort)
                    Convert_ALshort_ALmsadpcm(static_cast<ALshort*>(dst),
                        static_cast<const ALubyte*>(data), num_chans, length, align);
                else
                {
                    assert((long)srctype == (long)albuf->FmtType);
                    memcpy(dst, data, length*frame_size);
                }
            }
        }
    }
}


AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint UNUSED(buffer),
  ALuint UNUSED(samplerate), ALenum UNUSED(internalformat), ALsizei UNUSED(samples),
  ALenum UNUSED(channels), ALenum UNUSED(type), const ALvoid *UNUSED(data))
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    alSetError(context.get(), AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
}

AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint UNUSED(buffer),
  ALsizei UNUSED(offset), ALsizei UNUSED(samples),
  ALenum UNUSED(channels), ALenum UNUSED(type), const ALvoid *UNUSED(data))
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    alSetError(context.get(), AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
}

AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint UNUSED(buffer),
  ALsizei UNUSED(offset), ALsizei UNUSED(samples),
  ALenum UNUSED(channels), ALenum UNUSED(type), ALvoid *UNUSED(data))
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    alSetError(context.get(), AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
}

AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum UNUSED(format))
{
    ContextRef context{GetContextRef()};
    if(!context) return AL_FALSE;

    alSetError(context.get(), AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
    return AL_FALSE;
}


AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat UNUSED(value))
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else switch(param)
    {
    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param, ALfloat UNUSED(value1), ALfloat UNUSED(value2), ALfloat UNUSED(value3))
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else switch(param)
    {
    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!values))
        alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
    else switch(param)
    {
    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    ALbuffer *albuf;
    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else switch(param)
    {
    case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
        if(UNLIKELY(value < 0))
            alSetError(context.get(), AL_INVALID_VALUE, "Invalid unpack block alignment %d", value);
        else
            ATOMIC_STORE_SEQ(&albuf->UnpackAlign, value);
        break;

    case AL_PACK_BLOCK_ALIGNMENT_SOFT:
        if(UNLIKELY(value < 0))
            alSetError(context.get(), AL_INVALID_VALUE, "Invalid pack block alignment %d", value);
        else
            ATOMIC_STORE_SEQ(&albuf->PackAlign, value);
        break;

    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alBuffer3i(ALuint buffer, ALenum param, ALint UNUSED(value1), ALint UNUSED(value2), ALint UNUSED(value3))
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else switch(param)
    {
    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
{
    if(values)
    {
        switch(param)
        {
        case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
        case AL_PACK_BLOCK_ALIGNMENT_SOFT:
            alBufferi(buffer, param, values[0]);
            return;
        }
    }

    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    ALbuffer *albuf;
    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!values))
        alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
    else switch(param)
    {
    case AL_LOOP_POINTS_SOFT:
        if(UNLIKELY(ReadRef(&albuf->ref) != 0))
            alSetError(context.get(), AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
                       buffer);
        else if(UNLIKELY(values[0] >= values[1] || values[0] < 0 || values[1] > albuf->SampleLen))
            alSetError(context.get(), AL_INVALID_VALUE, "Invalid loop point range %d -> %d o buffer %u",
                       values[0], values[1], buffer);
        else
        {
            albuf->LoopStart = values[0];
            albuf->LoopEnd = values[1];
        }
        break;

    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x",
                   param);
    }
}


AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    ALbuffer *albuf;
    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!value))
        alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
    else switch(param)
    {
    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!value1 || !value2 || !value3))
        alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
    else switch(param)
    {
    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
{
    switch(param)
    {
    case AL_SEC_LENGTH_SOFT:
        alGetBufferf(buffer, param, values);
        return;
    }

    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!values))
        alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
    else switch(param)
    {
    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
    }
}


AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    ALbuffer *albuf;
    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!value))
        alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
    else switch(param)
    {
    case AL_FREQUENCY:
        *value = albuf->Frequency;
        break;

    case AL_BITS:
        *value = BytesFromFmt(albuf->FmtType) * 8;
        break;

    case AL_CHANNELS:
        *value = ChannelsFromFmt(albuf->FmtChannels);
        break;

    case AL_SIZE:
        *value = albuf->SampleLen * FrameSizeFromFmt(albuf->FmtChannels,
                                                     albuf->FmtType);
        break;

    case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
        *value = ATOMIC_LOAD_SEQ(&albuf->UnpackAlign);
        break;

    case AL_PACK_BLOCK_ALIGNMENT_SOFT:
        *value = ATOMIC_LOAD_SEQ(&albuf->PackAlign);
        break;

    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
{
    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!value1 || !value2 || !value3))
        alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
    else switch(param)
    {
    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
    }
}


AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
{
    switch(param)
    {
    case AL_FREQUENCY:
    case AL_BITS:
    case AL_CHANNELS:
    case AL_SIZE:
    case AL_INTERNAL_FORMAT_SOFT:
    case AL_BYTE_LENGTH_SOFT:
    case AL_SAMPLE_LENGTH_SOFT:
    case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
    case AL_PACK_BLOCK_ALIGNMENT_SOFT:
        alGetBufferi(buffer, param, values);
        return;
    }

    ContextRef context{GetContextRef()};
    if(UNLIKELY(!context)) return;

    ALCdevice *device = context->Device;
    std::lock_guard<almtx_t> _{device->BufferLock};

    ALbuffer *albuf;
    if(UNLIKELY((albuf=LookupBuffer(device, buffer)) == nullptr))
        alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
    else if(UNLIKELY(!values))
        alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
    else switch(param)
    {
    case AL_LOOP_POINTS_SOFT:
        values[0] = albuf->LoopStart;
        values[1] = albuf->LoopEnd;
        break;

    default:
        alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x",
                   param);
    }
}


static const ALchar *NameFromUserFmtType(enum UserFmtType type)
{
    switch(type)
    {
        case UserFmtUByte: return "Unsigned Byte";
        case UserFmtShort: return "Signed Short";
        case UserFmtFloat: return "Float32";
        case UserFmtDouble: return "Float64";
        case UserFmtMulaw: return "muLaw";
        case UserFmtAlaw: return "aLaw";
        case UserFmtIMA4: return "IMA4 ADPCM";
        case UserFmtMSADPCM: return "MSADPCM";
    }
    return "<internal type error>";
}

/*
 * LoadData
 *
 * Loads the specified data into the buffer, using the specified format.
 */
static void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALuint freq, ALsizei size, enum UserFmtChannels SrcChannels, enum UserFmtType SrcType, const ALvoid *data, ALbitfieldSOFT access)
{
    enum FmtChannels DstChannels = FmtMono;
    enum FmtType DstType = FmtUByte;
    ALsizei NumChannels, FrameSize;
    ALsizei SrcByteAlign;
    ALsizei unpackalign;
    ALsizei newsize;
    ALsizei frames;
    ALsizei align;

    if(UNLIKELY(ReadRef(&ALBuf->ref) != 0 || ALBuf->MappedAccess != 0))
        SETERR_RETURN(context, AL_INVALID_OPERATION,, "Modifying storage for in-use buffer %u",
                      ALBuf->id);

    /* Currently no channel configurations need to be converted. */
    switch(SrcChannels)
    {
        case UserFmtMono: DstChannels = FmtMono; break;
        case UserFmtStereo: DstChannels = FmtStereo; break;
        case UserFmtRear: DstChannels = FmtRear; break;
        case UserFmtQuad: DstChannels = FmtQuad; break;
        case UserFmtX51: DstChannels = FmtX51; break;
        case UserFmtX61: DstChannels = FmtX61; break;
        case UserFmtX71: DstChannels = FmtX71; break;
        case UserFmtBFormat2D: DstChannels = FmtBFormat2D; break;
        case UserFmtBFormat3D: DstChannels = FmtBFormat3D; break;
    }
    if(UNLIKELY((long)SrcChannels != (long)DstChannels))
        SETERR_RETURN(context, AL_INVALID_ENUM,, "Invalid format");

    /* IMA4 and MSADPCM convert to 16-bit short. */
    switch(SrcType)
    {
        case UserFmtUByte: DstType = FmtUByte; break;
        case UserFmtShort: DstType = FmtShort; break;
        case UserFmtFloat: DstType = FmtFloat; break;
        case UserFmtDouble: DstType = FmtDouble; break;
        case UserFmtAlaw: DstType = FmtAlaw; break;
        case UserFmtMulaw: DstType = FmtMulaw; break;
        case UserFmtIMA4: DstType = FmtShort; break;
        case UserFmtMSADPCM: DstType = FmtShort; break;
    }

    /* TODO: Currently we can only map samples when they're not converted. To
     * allow it would need some kind of double-buffering to hold onto a copy of
     * the original data.
     */
    if((access&MAP_READ_WRITE_FLAGS))
    {
        if(UNLIKELY((long)SrcType != (long)DstType))
            SETERR_RETURN(context, AL_INVALID_VALUE,, "%s samples cannot be mapped",
                          NameFromUserFmtType(SrcType));
    }

    unpackalign = ATOMIC_LOAD_SEQ(&ALBuf->UnpackAlign);
    if(UNLIKELY((align=SanitizeAlignment(SrcType, unpackalign)) < 1))
        SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid unpack alignment %d for %s samples",
                      unpackalign, NameFromUserFmtType(SrcType));

    if((access&AL_PRESERVE_DATA_BIT_SOFT))
    {
        /* Can only preserve data with the same format and alignment. */
        if(UNLIKELY(ALBuf->FmtChannels != DstChannels || ALBuf->OriginalType != SrcType))
            SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched format");
        if(UNLIKELY(ALBuf->OriginalAlign != align))
            SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched alignment");
    }

    /* Convert the input/source size in bytes to sample frames using the unpack
     * block alignment.
     */
    if(SrcType == UserFmtIMA4)
        SrcByteAlign = ((align-1)/2 + 4) * ChannelsFromUserFmt(SrcChannels);
    else if(SrcType == UserFmtMSADPCM)
        SrcByteAlign = ((align-2)/2 + 7) * ChannelsFromUserFmt(SrcChannels);
    else
        SrcByteAlign = align * FrameSizeFromUserFmt(SrcChannels, SrcType);
    if(UNLIKELY((size%SrcByteAlign) != 0))
        SETERR_RETURN(context, AL_INVALID_VALUE,,
            "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
            size, SrcByteAlign, align);

    if(UNLIKELY(size / SrcByteAlign > INT_MAX / align))
        SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
            "Buffer size overflow, %d blocks x %d samples per block", size/SrcByteAlign, align);
    frames = size / SrcByteAlign * align;

    /* Convert the sample frames to the number of bytes needed for internal
     * storage.
     */
    NumChannels = ChannelsFromFmt(DstChannels);
    FrameSize = NumChannels * BytesFromFmt(DstType);
    if(UNLIKELY(frames > INT_MAX/FrameSize))
        SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
            "Buffer size overflow, %d frames x %d bytes per frame", frames, FrameSize);
    newsize = frames*FrameSize;

    /* Round up to the next 16-byte multiple. This could reallocate only when
     * increasing or the new size is less than half the current, but then the
     * buffer's AL_SIZE would not be very reliable for accounting buffer memory
     * usage, and reporting the real size could cause problems for apps that
     * use AL_SIZE to try to get the buffer's play length.
     */
    if(LIKELY(newsize <= INT_MAX-15))
        newsize = (newsize+15) & ~0xf;
    if(newsize != ALBuf->BytesAlloc)
    {
        void *temp = al_malloc(16, (size_t)newsize);
        if(UNLIKELY(!temp && newsize))
            SETERR_RETURN(context, AL_OUT_OF_MEMORY,, "Failed to allocate %d bytes of storage",
                          newsize);
        if((access&AL_PRESERVE_DATA_BIT_SOFT))
        {
            ALsizei tocopy = mini(newsize, ALBuf->BytesAlloc);
            if(tocopy > 0) memcpy(temp, ALBuf->data, tocopy);
        }
        al_free(ALBuf->data);
        ALBuf->data = temp;
        ALBuf->BytesAlloc = newsize;
    }

    if(SrcType == UserFmtIMA4)
    {
        assert(DstType == FmtShort);
        if(data != nullptr && ALBuf->data != nullptr)
            Convert_ALshort_ALima4(static_cast<ALshort*>(ALBuf->data),
                static_cast<const ALubyte*>(data), NumChannels, frames, align);
        ALBuf->OriginalAlign = align;
    }
    else if(SrcType == UserFmtMSADPCM)
    {
        assert(DstType == FmtShort);
        if(data != nullptr && ALBuf->data != nullptr)
            Convert_ALshort_ALmsadpcm(static_cast<ALshort*>(ALBuf->data),
                static_cast<const ALubyte*>(data), NumChannels, frames, align);
        ALBuf->OriginalAlign = align;
    }
    else
    {
        assert((long)SrcType == (long)DstType);
        if(data != nullptr && ALBuf->data != nullptr)
            memcpy(ALBuf->data, data, frames*FrameSize);
        ALBuf->OriginalAlign = 1;
    }
    ALBuf->OriginalSize = size;
    ALBuf->OriginalType = SrcType;

    ALBuf->Frequency = freq;
    ALBuf->FmtChannels = DstChannels;
    ALBuf->FmtType = DstType;
    ALBuf->Access = access;

    ALBuf->SampleLen = frames;
    ALBuf->LoopStart = 0;
    ALBuf->LoopEnd = ALBuf->SampleLen;
}


ALsizei BytesFromUserFmt(enum UserFmtType type)
{
    switch(type)
    {
    case UserFmtUByte: return sizeof(ALubyte);
    case UserFmtShort: return sizeof(ALshort);
    case UserFmtFloat: return sizeof(ALfloat);
    case UserFmtDouble: return sizeof(ALdouble);
    case UserFmtMulaw: return sizeof(ALubyte);
    case UserFmtAlaw: return sizeof(ALubyte);
    case UserFmtIMA4: break; /* not handled here */
    case UserFmtMSADPCM: break; /* not handled here */
    }
    return 0;
}
ALsizei ChannelsFromUserFmt(enum UserFmtChannels chans)
{
    switch(chans)
    {
    case UserFmtMono: return 1;
    case UserFmtStereo: return 2;
    case UserFmtRear: return 2;
    case UserFmtQuad: return 4;
    case UserFmtX51: return 6;
    case UserFmtX61: return 7;
    case UserFmtX71: return 8;
    case UserFmtBFormat2D: return 3;
    case UserFmtBFormat3D: return 4;
    }
    return 0;
}
static ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
                                     enum UserFmtType *type)
{
    struct FormatMap {
        ALenum format;
        UserFmtChannels channels;
        UserFmtType type;
    };
    static const std::array<FormatMap,46> list{{
        { AL_FORMAT_MONO8,             UserFmtMono, UserFmtUByte   },
        { AL_FORMAT_MONO16,            UserFmtMono, UserFmtShort   },
        { AL_FORMAT_MONO_FLOAT32,      UserFmtMono, UserFmtFloat   },
        { AL_FORMAT_MONO_DOUBLE_EXT,   UserFmtMono, UserFmtDouble  },
        { AL_FORMAT_MONO_IMA4,         UserFmtMono, UserFmtIMA4    },
        { AL_FORMAT_MONO_MSADPCM_SOFT, UserFmtMono, UserFmtMSADPCM },
        { AL_FORMAT_MONO_MULAW,        UserFmtMono, UserFmtMulaw   },
        { AL_FORMAT_MONO_ALAW_EXT,     UserFmtMono, UserFmtAlaw    },

        { AL_FORMAT_STEREO8,             UserFmtStereo, UserFmtUByte   },
        { AL_FORMAT_STEREO16,            UserFmtStereo, UserFmtShort   },
        { AL_FORMAT_STEREO_FLOAT32,      UserFmtStereo, UserFmtFloat   },
        { AL_FORMAT_STEREO_DOUBLE_EXT,   UserFmtStereo, UserFmtDouble  },
        { AL_FORMAT_STEREO_IMA4,         UserFmtStereo, UserFmtIMA4    },
        { AL_FORMAT_STEREO_MSADPCM_SOFT, UserFmtStereo, UserFmtMSADPCM },
        { AL_FORMAT_STEREO_MULAW,        UserFmtStereo, UserFmtMulaw   },
        { AL_FORMAT_STEREO_ALAW_EXT,     UserFmtStereo, UserFmtAlaw    },

        { AL_FORMAT_REAR8,      UserFmtRear, UserFmtUByte },
        { AL_FORMAT_REAR16,     UserFmtRear, UserFmtShort },
        { AL_FORMAT_REAR32,     UserFmtRear, UserFmtFloat },
        { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },

        { AL_FORMAT_QUAD8_LOKI,  UserFmtQuad, UserFmtUByte },
        { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },

        { AL_FORMAT_QUAD8,      UserFmtQuad, UserFmtUByte },
        { AL_FORMAT_QUAD16,     UserFmtQuad, UserFmtShort },
        { AL_FORMAT_QUAD32,     UserFmtQuad, UserFmtFloat },
        { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },

        { AL_FORMAT_51CHN8,      UserFmtX51, UserFmtUByte },
        { AL_FORMAT_51CHN16,     UserFmtX51, UserFmtShort },
        { AL_FORMAT_51CHN32,     UserFmtX51, UserFmtFloat },
        { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },

        { AL_FORMAT_61CHN8,      UserFmtX61, UserFmtUByte },
        { AL_FORMAT_61CHN16,     UserFmtX61, UserFmtShort },
        { AL_FORMAT_61CHN32,     UserFmtX61, UserFmtFloat },
        { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },

        { AL_FORMAT_71CHN8,      UserFmtX71, UserFmtUByte },
        { AL_FORMAT_71CHN16,     UserFmtX71, UserFmtShort },
        { AL_FORMAT_71CHN32,     UserFmtX71, UserFmtFloat },
        { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },

        { AL_FORMAT_BFORMAT2D_8,       UserFmtBFormat2D, UserFmtUByte },
        { AL_FORMAT_BFORMAT2D_16,      UserFmtBFormat2D, UserFmtShort },
        { AL_FORMAT_BFORMAT2D_FLOAT32, UserFmtBFormat2D, UserFmtFloat },
        { AL_FORMAT_BFORMAT2D_MULAW,   UserFmtBFormat2D, UserFmtMulaw },

        { AL_FORMAT_BFORMAT3D_8,       UserFmtBFormat3D, UserFmtUByte },
        { AL_FORMAT_BFORMAT3D_16,      UserFmtBFormat3D, UserFmtShort },
        { AL_FORMAT_BFORMAT3D_FLOAT32, UserFmtBFormat3D, UserFmtFloat },
        { AL_FORMAT_BFORMAT3D_MULAW,   UserFmtBFormat3D, UserFmtMulaw },
    }};

    for(const auto &fmt : list)
    {
        if(fmt.format == format)
        {
            *chans = fmt.channels;
            *type  = fmt.type;
            return AL_TRUE;
        }
    }

    return AL_FALSE;
}

ALsizei BytesFromFmt(enum FmtType type)
{
    switch(type)
    {
    case FmtUByte: return sizeof(ALubyte);
    case FmtShort: return sizeof(ALshort);
    case FmtFloat: return sizeof(ALfloat);
    case FmtDouble: return sizeof(ALdouble);
    case FmtMulaw: return sizeof(ALubyte);
    case FmtAlaw: return sizeof(ALubyte);
    }
    return 0;
}
ALsizei ChannelsFromFmt(enum FmtChannels chans)
{
    switch(chans)
    {
    case FmtMono: return 1;
    case FmtStereo: return 2;
    case FmtRear: return 2;
    case FmtQuad: return 4;
    case FmtX51: return 6;
    case FmtX61: return 7;
    case FmtX71: return 8;
    case FmtBFormat2D: return 3;
    case FmtBFormat3D: return 4;
    }
    return 0;
}

static ALsizei SanitizeAlignment(enum UserFmtType type, ALsizei align)
{
    if(align < 0)
        return 0;

    if(align == 0)
    {
        if(type == UserFmtIMA4)
        {
            /* Here is where things vary:
             * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
             * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
             */
            return 65;
        }
        if(type == UserFmtMSADPCM)
            return 64;
        return 1;
    }

    if(type == UserFmtIMA4)
    {
        /* IMA4 block alignment must be a multiple of 8, plus 1. */
        if((align&7) == 1) return align;
        return 0;
    }
    if(type == UserFmtMSADPCM)
    {
        /* MSADPCM block alignment must be a multiple of 2. */
        if((align&1) == 0) return align;
        return 0;
    }

    return align;
}


static ALbuffer *AllocBuffer(ALCcontext *context)
{
    ALCdevice *device = context->Device;
    BufferSubList *sublist, *subend;
    ALbuffer *buffer = nullptr;
    ALsizei lidx = 0;
    ALsizei slidx;

    std::unique_lock<almtx_t> buflock{device->BufferLock};

    sublist = VECTOR_BEGIN(device->BufferList);
    subend = VECTOR_END(device->BufferList);
    for(;sublist != subend;++sublist)
    {
        if(sublist->FreeMask)
        {
            slidx = CTZ64(sublist->FreeMask);
            buffer = sublist->Buffers + slidx;
            break;
        }
        ++lidx;
    }
    if(UNLIKELY(!buffer))
    {
        const BufferSubList empty_sublist = { 0, nullptr };
        /* Don't allocate so many list entries that the 32-bit ID could
         * overflow...
         */
        if(UNLIKELY(VECTOR_SIZE(device->BufferList) >= 1<<25))
        {
            buflock.unlock();
            alSetError(context, AL_OUT_OF_MEMORY, "Too many buffers allocated");
            return nullptr;
        }
        lidx = (ALsizei)VECTOR_SIZE(device->BufferList);
        VECTOR_PUSH_BACK(device->BufferList, empty_sublist);
        sublist = &VECTOR_BACK(device->BufferList);
        sublist->FreeMask = ~U64(0);
        sublist->Buffers = reinterpret_cast<ALbuffer*>(al_calloc(16, sizeof(ALbuffer)*64));
        if(UNLIKELY(!sublist->Buffers))
        {
            VECTOR_POP_BACK(device->BufferList);
            buflock.unlock();
            alSetError(context, AL_OUT_OF_MEMORY, "Failed to allocate buffer batch");
            return nullptr;
        }

        slidx = 0;
        buffer = sublist->Buffers + slidx;
    }

    memset(buffer, 0, sizeof(*buffer));

    /* Add 1 to avoid buffer ID 0. */
    buffer->id = ((lidx<<6) | slidx) + 1;

    sublist->FreeMask &= ~(U64(1)<<slidx);

    return buffer;
}

static void FreeBuffer(ALCdevice *device, ALbuffer *buffer)
{
    ALuint id = buffer->id - 1;
    ALsizei lidx = id >> 6;
    ALsizei slidx = id & 0x3f;

    al_free(buffer->data);
    memset(buffer, 0, sizeof(*buffer));

    VECTOR_ELEM(device->BufferList, lidx).FreeMask |= U64(1) << slidx;
}


/*
 *    ReleaseALBuffers()
 *
 *    INTERNAL: Called to destroy any buffers that still exist on the device
 */
ALvoid ReleaseALBuffers(ALCdevice *device)
{
    BufferSubList *sublist = VECTOR_BEGIN(device->BufferList);
    BufferSubList *subend = VECTOR_END(device->BufferList);
    size_t leftover = 0;
    for(;sublist != subend;++sublist)
    {
        ALuint64 usemask = ~sublist->FreeMask;
        while(usemask)
        {
            ALsizei idx = CTZ64(usemask);
            ALbuffer *buffer = sublist->Buffers + idx;

            al_free(buffer->data);
            memset(buffer, 0, sizeof(*buffer));
            ++leftover;

            usemask &= ~(U64(1) << idx);
        }
        sublist->FreeMask = ~usemask;
    }
    if(leftover > 0)
        WARN("(%p) Deleted " SZFMT " Buffer%s\n", device, leftover, (leftover==1)?"":"s");
}