/
AppleUSBAudioClip.cpp
3047 lines (2564 loc) · 87.9 KB
/
AppleUSBAudioClip.cpp
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
/*
* Copyright (c) 1998-2010 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <libkern/OSTypes.h>
#include <IOKit/IOReturn.h>
#include <IOKit/IOLib.h>
class IOMemoryDescriptor;
#include <IOKit/audio/IOAudioTypes.h>
#include "AppleUSBAudioClip.h"
#include "AppleUSBAudioCommon.h"
extern "C" {
// floating point types
typedef float Float32;
typedef double Float64;
UInt32 CalculateOffset (UInt64 nanoseconds, UInt32 sampleRate) {
return (UInt32)(((double)sampleRate / 1000000000.0) * nanoseconds);
}
inline static SInt16 Endian16_Swap(SInt16 inValue)
{
return (((((UInt16)inValue)<<8) & 0xFF00) | ((((UInt16)inValue)>>8) & 0x00FF));
}
inline static SInt32 Endian32_Swap(SInt32 inValue)
{
return (((((UInt32)inValue)<<24) & 0xFF000000) | ((((UInt32)inValue)<< 8) & 0x00FF0000) | ((((UInt32)inValue)>> 8) & 0x0000FF00) | ((((UInt32)inValue)>>24) & 0x000000FF));
}
#if defined(__ppc__)
// aml new routines [3034710]
void Int8ToFloat32( SInt8 *src, float *dest, unsigned int count );
void NativeInt16ToFloat32( signed short *src, float *dest, unsigned int count, int bitDepth );
void SwapInt16ToFloat32( signed short *src, float *dest, unsigned int count, int bitDepth );
void NativeInt24ToFloat32( long *src, float *dest, unsigned int count, int bitDepth );
void SwapInt24ToFloat32( long *src, float *dest, unsigned int count, int bitDepth );
void NativeInt32ToFloat32( long *src, float *dest, unsigned int count, int bitDepth );
void SwapInt32ToFloat32( long *src, float *dest, unsigned int count, int bitDepth );
void Float32ToInt8( float *src, SInt8 *dst, unsigned int count );
void Float32ToNativeInt16( float *src, signed short *dst, unsigned int count );
void Float32ToSwapInt16( float *src, signed short *dst, unsigned int count );
void Float32ToNativeInt24( float *src, signed long *dst, unsigned int count );
void Float32ToSwapInt24( float *src, signed long *dst, unsigned int count );
void Float32ToNativeInt32( float *src, signed long *dst, unsigned int count );
void Float32ToSwapInt32( float *src, signed long *dst, unsigned int count );
static inline SInt16 SInt16BigToNativeEndian(SInt16 inValue) { return inValue; }
static inline SInt16 SInt16NativeToBigEndian(SInt16 inValue) { return inValue; }
static inline SInt16 SInt16LittleToNativeEndian(SInt16 inValue) { return Endian16_Swap(inValue); }
static inline SInt16 SInt16NativeToLittleEndian(SInt16 inValue) { return Endian16_Swap(inValue); }
static inline SInt32 SInt32BigToNativeEndian(SInt32 inValue) { return inValue; }
static inline SInt32 SInt32NativeToBigEndian(SInt32 inValue) { return inValue; }
static inline SInt32 SInt32LittleToNativeEndian(SInt32 inValue) { return Endian32_Swap(inValue); }
static inline SInt32 SInt32NativeToLittleEndian(SInt32 inValue) { return Endian32_Swap(inValue); }
#elif defined(__i386__) || defined(__x86_64__)
static inline SInt16 SInt16BigToNativeEndian(SInt16 inValue) { return Endian16_Swap(inValue); }
static inline SInt16 SInt16NativeToBigEndian(SInt16 inValue) { return Endian16_Swap(inValue); }
static inline SInt16 SInt16LittleToNativeEndian(SInt16 inValue) { return inValue; }
static inline SInt16 SInt16NativeToLittleEndian(SInt16 inValue) { return inValue; }
static inline SInt32 SInt32BigToNativeEndian(SInt32 inValue) { return Endian32_Swap(inValue); }
static inline SInt32 SInt32NativeToBigEndian(SInt32 inValue) { return Endian32_Swap(inValue); }
static inline SInt32 SInt32LittleToNativeEndian(SInt32 inValue) { return inValue; }
static inline SInt32 SInt32NativeToLittleEndian(SInt32 inValue) { return inValue; }
#endif
#define kMaxClipSInt8 0.9921875
#define kFloat32ToSInt8 ((Float32)0x80)
#define kMaxClipSInt16 0.9999694824219
#define kFloat32ToSInt16 ((Float32)0x8000)
#define kMaxClipSInt24 0.9999998807907
#define kMaxClipSInt32 0.9999999995343387 // <rdar://7138492>
#define kFloat32ToSInt32 ((Float64)0x80000000)
inline static Float32 ClipFloat32ForSInt8(Float32 inSample)
{
// Float32 maxClip = kMaxSampleSInt8 / (kMaxSampleSInt8 + 1.0);
if(inSample > kMaxClipSInt8) return kMaxClipSInt8;
if(inSample < -1.0) return -1.0;
return inSample;
}
inline static Float32 ClipFloat32ForSInt16(Float32 inSample)
{
// Float32 maxClip = kMaxSampleSInt16 / (kMaxSampleSInt16 + 1.0);
if(inSample > kMaxClipSInt16) return kMaxClipSInt16;
if(inSample < -1.0) return -1.0;
return inSample;
}
inline static Float32 ClipFloat32ForSInt24(Float32 inSample)
{
// Float32 maxClip = kMaxSampleSInt24 / (kMaxSampleSInt24 + 1.0);
if(inSample > kMaxClipSInt24) return kMaxClipSInt24;
if(inSample < -1.0) return -1.0;
return inSample;
}
inline static Float64 ClipFloat32ForSInt32(Float64 inSample) // <rdar://7138492>
{
// Float64 maxClip64 = kMaxSampleSInt32 / (kMaxSampleSInt32 + 1.0);
// Float32 maxClip = maxClip64;
if(inSample > kMaxClipSInt32) return kMaxClipSInt32;
if(inSample < -1.0) return -1.0;
return inSample;
}
// Float32 -> SInt8
#if defined(__i386__) || defined(__x86_64__)
static void ClipFloat32ToSInt8_4(const Float32* inInputBuffer, SInt8* outOutputBuffer, UInt32 inNumberSamples)
{
register UInt32 theLeftOvers = inNumberSamples % 4;
while(inNumberSamples > theLeftOvers)
{
register Float32 theFloat32Value1 = *(inInputBuffer + 0);
register Float32 theFloat32Value2 = *(inInputBuffer + 1);
register Float32 theFloat32Value3 = *(inInputBuffer + 2);
register Float32 theFloat32Value4 = *(inInputBuffer + 3);
inInputBuffer += 4;
theFloat32Value1 = ClipFloat32ForSInt8(theFloat32Value1);
theFloat32Value2 = ClipFloat32ForSInt8(theFloat32Value2);
theFloat32Value3 = ClipFloat32ForSInt8(theFloat32Value3);
theFloat32Value4 = ClipFloat32ForSInt8(theFloat32Value4);
*(outOutputBuffer + 0) = (SInt8)(theFloat32Value1 * kFloat32ToSInt8);
*(outOutputBuffer + 1) = (SInt8)(theFloat32Value2 * kFloat32ToSInt8);
*(outOutputBuffer + 2) = (SInt8)(theFloat32Value3 * kFloat32ToSInt8);
*(outOutputBuffer + 3) = (SInt8)(theFloat32Value4 * kFloat32ToSInt8);
outOutputBuffer += 4;
inNumberSamples -= 4;
}
while(inNumberSamples > 0)
{
register Float32 theFloat32Value = *inInputBuffer;
++inInputBuffer;
theFloat32Value = ClipFloat32ForSInt8(theFloat32Value);
*outOutputBuffer = (SInt8)(theFloat32Value * kFloat32ToSInt8);
++outOutputBuffer;
--inNumberSamples;
}
}
// Float32 -> SInt16
static void ClipFloat32ToSInt16LE_4(const Float32* inInputBuffer, SInt16* outOutputBuffer, UInt32 inNumberSamples)
{
register UInt32 theLeftOvers = inNumberSamples % 4;
while(inNumberSamples > theLeftOvers)
{
register Float32 theFloat32Value1 = *(inInputBuffer + 0);
register Float32 theFloat32Value2 = *(inInputBuffer + 1);
register Float32 theFloat32Value3 = *(inInputBuffer + 2);
register Float32 theFloat32Value4 = *(inInputBuffer + 3);
inInputBuffer += 4;
theFloat32Value1 = ClipFloat32ForSInt16(theFloat32Value1);
theFloat32Value2 = ClipFloat32ForSInt16(theFloat32Value2);
theFloat32Value3 = ClipFloat32ForSInt16(theFloat32Value3);
theFloat32Value4 = ClipFloat32ForSInt16(theFloat32Value4);
*(outOutputBuffer + 0) = SInt16NativeToLittleEndian((SInt16)(theFloat32Value1 * kFloat32ToSInt16));
*(outOutputBuffer + 1) = SInt16NativeToLittleEndian((SInt16)(theFloat32Value2 * kFloat32ToSInt16));
*(outOutputBuffer + 2) = SInt16NativeToLittleEndian((SInt16)(theFloat32Value3 * kFloat32ToSInt16));
*(outOutputBuffer + 3) = SInt16NativeToLittleEndian((SInt16)(theFloat32Value4 * kFloat32ToSInt16));
outOutputBuffer += 4;
inNumberSamples -= 4;
}
while(inNumberSamples > 0)
{
register Float32 theFloat32Value = *inInputBuffer;
++inInputBuffer;
theFloat32Value = ClipFloat32ForSInt16(theFloat32Value);
*outOutputBuffer = SInt16NativeToLittleEndian((SInt16)(theFloat32Value * kFloat32ToSInt16));
++outOutputBuffer;
--inNumberSamples;
}
}
// Float32 -> SInt24
// we use the MaxSInt32 value because of how we munge the data
static void ClipFloat32ToSInt24LE_4(const Float32* inInputBuffer, SInt32* outOutputBuffer, UInt32 inNumberSamples)
{
register UInt32 theLeftOvers = inNumberSamples % 4;
while(inNumberSamples > theLeftOvers)
{
register Float32 theFloat32Value1 = *(inInputBuffer + 0);
register Float32 theFloat32Value2 = *(inInputBuffer + 1);
register Float32 theFloat32Value3 = *(inInputBuffer + 2);
register Float32 theFloat32Value4 = *(inInputBuffer + 3);
inInputBuffer += 4;
theFloat32Value1 = ClipFloat32ForSInt24(theFloat32Value1);
theFloat32Value2 = ClipFloat32ForSInt24(theFloat32Value2);
theFloat32Value3 = ClipFloat32ForSInt24(theFloat32Value3);
theFloat32Value4 = ClipFloat32ForSInt24(theFloat32Value4);
// Multiply by kFloat32ToSInt32 instead of kFloat32toSInt24 to make the binary operations below work properly.
register UInt32 a = (UInt32)(SInt32)(theFloat32Value1 * kFloat32ToSInt32);
register UInt32 b = (UInt32)(SInt32)(theFloat32Value2 * kFloat32ToSInt32);
register UInt32 c = (UInt32)(SInt32)(theFloat32Value3 * kFloat32ToSInt32);
register UInt32 d = (UInt32)(SInt32)(theFloat32Value4 * kFloat32ToSInt32);
#if defined(__ppc__)
// a b c d
// IN REGISTER: 123X 456X 789X ABCX
// OUT REGISTERS: 3216 5498 7CBA
// OUT MEMORY: 3216 5498 7CBA
// each sample in the 4 registers looks like this: 123X, where X
// is the unused byte we need to munge all four so that they look
// like this in three registers: 3216 5498 7CBA. We want to avoid
// any non-aligned memory writes if at all possible.
register SInt32 theOutputValue1 = ((a << 16) & 0xFF000000) | (a & 0x00FF0000) | ((a >> 16) & 0x0000FF00) | ((b >> 8) & 0x000000FF); // 3216
register SInt32 theOutputValue2 = ((b << 8) & 0xFF000000) | ((b >> 8) & 0x00FF0000) | (c & 0x0000FF00) | ((c >> 16) & 0x000000FF);
register SInt32 theOutputValue3 = (c & 0xFF000000) | ((d << 8) & 0x00FF0000) | ((d >> 8) & 0x0000FF00) | ((d >> 24) & 0x000000FF);
#elif defined(__i386__) || defined(__x86_64__)
// a b c d a b c d
// IN REGISTER: 123X 456X 789X ABCX abc0 def0 ghi0 jkl0
// OUT REGISTERS: 6123 8945 ABC7 fabc hide jklg
// OUT MEMORY: 3216 5498 7CBA
register SInt32 theOutputValue1 = ((b << 16) & 0xFF000000) | (a >> 8);
register SInt32 theOutputValue2 = ((c << 8) & 0xFFFF0000) | ((b >> 16) & 0x0000FFFF);
register SInt32 theOutputValue3 = (d & 0xFFFFFF00) | ((c >> 24) & 0x000000FF);
#endif
// store everything back to memory
*(outOutputBuffer + 0) = theOutputValue1;
*(outOutputBuffer + 1) = theOutputValue2;
*(outOutputBuffer + 2) = theOutputValue3;
outOutputBuffer += 3;
inNumberSamples -= 4;
}
SInt8* theOutputBuffer = (SInt8*)outOutputBuffer;
while(inNumberSamples > 0)
{
register Float32 theFloat32Value = *inInputBuffer;
++inInputBuffer;
theFloat32Value = ClipFloat32ForSInt24(theFloat32Value);
// Multiply by kFloat32ToSInt32 instead of kFloat32toSInt24 to make the binary operations below work properly.
register SInt32 theSInt32Value = (SInt32)(theFloat32Value * kFloat32ToSInt32);
// Byte swapping will be handled automatically by the CPU if necessary.
*(theOutputBuffer + 0) = (SInt8)((((UInt32)theSInt32Value) >> 8) & 0x000000FF);
*(theOutputBuffer + 1) = (SInt8)((((UInt32)theSInt32Value) >> 16) & 0x000000FF);
*(theOutputBuffer + 2) = (SInt8)((((UInt32)theSInt32Value) >> 24) & 0x000000FF);
theOutputBuffer += 3;
--inNumberSamples;
}
}
// Float32 -> SInt32
static void ClipFloat32ToSInt32LE_4(const Float32* inInputBuffer, SInt32* outOutputBuffer, UInt32 inNumberSamples)
{
register UInt32 theLeftOvers = inNumberSamples % 4;
while(inNumberSamples > theLeftOvers)
{
register Float64 theFloat32Value1 = *(inInputBuffer + 0); // <rdar://7138492>
register Float64 theFloat32Value2 = *(inInputBuffer + 1); // <rdar://7138492>
register Float64 theFloat32Value3 = *(inInputBuffer + 2); // <rdar://7138492>
register Float64 theFloat32Value4 = *(inInputBuffer + 3); // <rdar://7138492>
inInputBuffer += 4;
theFloat32Value1 = ClipFloat32ForSInt32(theFloat32Value1);
theFloat32Value2 = ClipFloat32ForSInt32(theFloat32Value2);
theFloat32Value3 = ClipFloat32ForSInt32(theFloat32Value3);
theFloat32Value4 = ClipFloat32ForSInt32(theFloat32Value4);
*(outOutputBuffer + 0) = SInt32NativeToLittleEndian((SInt32)(theFloat32Value1 * kFloat32ToSInt32));
*(outOutputBuffer + 1) = SInt32NativeToLittleEndian((SInt32)(theFloat32Value2 * kFloat32ToSInt32));
*(outOutputBuffer + 2) = SInt32NativeToLittleEndian((SInt32)(theFloat32Value3 * kFloat32ToSInt32));
*(outOutputBuffer + 3) = SInt32NativeToLittleEndian((SInt32)(theFloat32Value4 * kFloat32ToSInt32));
outOutputBuffer += 4;
inNumberSamples -= 4;
}
while(inNumberSamples > 0)
{
register Float64 theFloat32Value = *inInputBuffer; // <rdar://7138492>
++inInputBuffer;
theFloat32Value = ClipFloat32ForSInt32(theFloat32Value);
*outOutputBuffer = SInt32NativeToLittleEndian((SInt32)(theFloat32Value * kFloat32ToSInt32));
++outOutputBuffer;
--inNumberSamples;
}
}
#endif
IOReturn clipAppleUSBAudioToOutputStream(const void* mixBuf, void* sampleBuf, UInt32 firstSampleFrame, UInt32 numSampleFrames, const IOAudioStreamFormat *streamFormat)
{
if(!streamFormat)
{
return kIOReturnBadArgument;
}
UInt32 theNumberSamples = numSampleFrames * streamFormat->fNumChannels;
UInt32 theFirstSample = firstSampleFrame * streamFormat->fNumChannels;
Float32* theMixBuffer = ((Float32*)mixBuf) + theFirstSample;
// aml, added optimized routines [3034710]
switch(streamFormat->fBitWidth)
{
case 8:
{
SInt8* theOutputBufferSInt8 = ((SInt8*)sampleBuf) + theFirstSample;
#if defined(__ppc__)
Float32ToInt8(theMixBuffer, theOutputBufferSInt8, theNumberSamples);
#elif defined (__i386__) || defined(__x86_64__)
ClipFloat32ToSInt8_4(theMixBuffer, theOutputBufferSInt8, theNumberSamples);
#endif
//ClipFloat32ToSInt8_4(theMixBuffer, theOutputBufferSInt8, theNumberSamples);
}
break;
case 16:
{
SInt16* theOutputBufferSInt16 = ((SInt16*)sampleBuf) + theFirstSample;
#if defined(__ppc__)
Float32ToSwapInt16(theMixBuffer, theOutputBufferSInt16, theNumberSamples);
#elif defined(__i386__) || defined(__x86_64__)
ClipFloat32ToSInt16LE_4(theMixBuffer, theOutputBufferSInt16, theNumberSamples);
#endif
//ClipFloat32ToSInt16LE_4(theMixBuffer, theOutputBufferSInt16, theNumberSamples);
}
break;
case 20:
case 24:
{
SInt32* theOutputBufferSInt24 = (SInt32*)(((UInt8*)sampleBuf) + (theFirstSample * 3));
#if defined(__ppc__)
Float32ToSwapInt24(theMixBuffer, theOutputBufferSInt24, theNumberSamples);
#elif defined(__i386__) || defined(__x86_64__)
ClipFloat32ToSInt24LE_4(theMixBuffer, theOutputBufferSInt24, theNumberSamples);
#endif
//ClipFloat32ToSInt24LE_4(theMixBuffer, theOutputBufferSInt24, theNumberSamples);
}
break;
case 32:
{
SInt32* theOutputBufferSInt32 = ((SInt32*)sampleBuf) + theFirstSample;
#if defined(__ppc__)
Float32ToSwapInt32(theMixBuffer, theOutputBufferSInt32, theNumberSamples);
#elif defined(__i386__) || defined(__x86_64__)
ClipFloat32ToSInt32LE_4(theMixBuffer, theOutputBufferSInt32, theNumberSamples);
#endif
//ClipFloat32ToSInt32LE_4(theMixBuffer, theOutputBufferSInt32, theNumberSamples);
}
break;
};
return kIOReturnSuccess;
}
const float kOneOverMaxSInt8Value = 1.0/128.0f;
const float kOneOverMaxSInt16Value = 1.0/32768.0f;
// const float kOneOverMaxSInt24Value = 1.0/8388608.0f;
const float kOneOverMaxSInt24Value = 0.00000011920928955078125f;
const float kOneOverMaxSInt32Value = 1.0/2147483648.0f;
IOReturn convertFromAppleUSBAudioInputStream_NoWrap (const void *sampleBuf,
void *destBuf,
UInt32 firstSampleFrame,
UInt32 numSampleFrames,
const IOAudioStreamFormat *streamFormat) {
UInt32 numSamplesLeft;
float *floatDestBuf;
floatDestBuf = (float *)destBuf;
numSamplesLeft = numSampleFrames * streamFormat->fNumChannels;
// debugIOLog ("destBuf = %p, firstSampleFrame = %ld, numSampleFrames = %ld", destBuf, firstSampleFrame, numSampleFrames);
switch (streamFormat->fBitWidth)
{
case 8:
SInt8 *inputBuf8;
inputBuf8 = &(((SInt8 *)sampleBuf)[firstSampleFrame * streamFormat->fNumChannels]);
#if defined(__ppc__)
Int8ToFloat32(inputBuf8, floatDestBuf, numSamplesLeft);
#elif defined(__i386__) || defined(__x86_64__)
while (numSamplesLeft-- > 0)
{
*(floatDestBuf++) = (float)(*(inputBuf8++)) * kOneOverMaxSInt8Value;
}
#endif
break;
case 16:
SInt16 *inputBuf16;
inputBuf16 = &(((SInt16 *)sampleBuf)[firstSampleFrame * streamFormat->fNumChannels]);
#if defined(__ppc__)
SwapInt16ToFloat32(inputBuf16, floatDestBuf, numSamplesLeft, 16);
#elif defined(__i386__) || defined(__x86_64__)
while (numSamplesLeft-- > 0)
{
*(floatDestBuf++) = (float)(*(inputBuf16++)) * kOneOverMaxSInt16Value;
}
#endif
break;
case 20:
case 24:
register SInt8 *inputBuf24;
// Multiply by 3 because 20 and 24 bit samples are packed into only three bytes, so we have to index bytes, not shorts or longs
inputBuf24 = &(((SInt8 *)sampleBuf)[firstSampleFrame * streamFormat->fNumChannels * 3]);
#if defined(__ppc__)
SwapInt24ToFloat32((long *)inputBuf24, floatDestBuf, numSamplesLeft, 24);
#elif defined(__i386__) || defined(__x86_64__)
register SInt32 inputSample;
// [rdar://4311684] - Fixed 24-bit input convert routine. /thw
while (numSamplesLeft-- > 1)
{
inputSample = (* (UInt32 *)inputBuf24) & 0x00FFFFFF;
// Sign extend if necessary
if (inputSample > 0x7FFFFF)
{
inputSample |= 0xFF000000;
}
inputBuf24 += 3;
*(floatDestBuf++) = (float)inputSample * kOneOverMaxSInt24Value;
}
// Convert last sample. The following line does the same work as above without going over the edge of the buffer.
inputSample = SInt32 ((UInt32 (*(UInt16 *) inputBuf24) & 0x0000FFFF) | (SInt32 (*(inputBuf24 + 2)) << 16));
*(floatDestBuf++) = (float)inputSample * kOneOverMaxSInt24Value;
#endif
break;
case 32:
register SInt32 *inputBuf32;
inputBuf32 = &(((SInt32 *)sampleBuf)[firstSampleFrame * streamFormat->fNumChannels]);
#if defined(__ppc__)
SwapInt32ToFloat32(inputBuf32, floatDestBuf, numSamplesLeft, 32);
#elif defined(__i386__) || defined(__x86_64__)
while (numSamplesLeft-- > 0) {
*(floatDestBuf++) = (float)(*(inputBuf32++)) * kOneOverMaxSInt32Value;
}
#endif
break;
}
return kIOReturnSuccess;
}
}
// aml new routines [3034710]
#pragma mark ¥¥¥ New clipping routines
#if defined(__ppc__)
// this behaves incorrectly in Float32ToSwapInt24 if not declared volatile
#define __lwbrx( index, base ) ({ register long result; __asm__ __volatile__("lwbrx %0, %1, %2" : "=r" (result) : "b%" (index), "r" (base) : "memory" ); result; } )
#define __lhbrx(index, base) \
({ register signed short lhbrxResult; \
__asm__ ("lhbrx %0, %1, %2" : "=r" (lhbrxResult) : "b%" (index), "r" (base) : "memory"); \
/*return*/ lhbrxResult; } )
// dsw: make signed to get sign-extension
#define __rlwimi( rA, rS, cnt, mb, me ) \
({ __asm__ __volatile__( "rlwimi %0, %2, %3, %4, %5" : "=r" (rA) : "0" (rA), "r" (rS), "n" (cnt), "n" (mb), "n" (me) ); /*return*/ rA; })
#define __stwbrx( value, index, base ) \
__asm__( "stwbrx %0, %1, %2" : : "r" (value), "b%" (index), "r" (base) : "memory" )
#define __rlwimi_volatile( rA, rS, cnt, mb, me ) \
({ __asm__ __volatile__( "rlwimi %0, %2, %3, %4, %5" : "=r" (rA) : "0" (rA), "r" (rS), "n" (cnt), "n" (mb), "n" (me) ); /*return*/ rA; })
#define __stfiwx( value, offset, addr ) \
asm( "stfiwx %0, %1, %2" : /*no result*/ : "f" (value), "b%" (offset), "r" (addr) : "memory" )
static inline double __fctiw( register double B )
{
register double result;
asm( "fctiw %0, %1" : "=f" (result) : "f" (B) );
return result;
}
// aml, adding 8 bit version
void Int8ToFloat32( SInt8 *src, float *dest, unsigned int count )
{
register float bias;
register long exponentMask = ((0x97UL - 8) << 23) | 0x8000; //FP exponent + bias for sign
register long int0, int1, int2, int3;
register float float0, float1, float2, float3;
register unsigned long loopCount;
union
{
float f;
long i;
}exponent;
exponent.i = exponentMask;
bias = exponent.f;
src--;
if( count >= 8 )
{
//Software Cycle 1
int0 = (++src)[0];
//Software Cycle 2
int1 = (++src)[0];
int0 += exponentMask;
//Software Cycle 3
int2 = (++src)[0];
int1 += exponentMask;
((long*) dest)[0] = int0;
//Software Cycle 4
int3 = (++src)[0];
int2 += exponentMask;
((long*) dest)[1] = int1;
//delay one loop for the store to complete
//Software Cycle 5
int0 = (++src)[0];
int3 += exponentMask;
((long*) dest)[2] = int2;
float0 = dest[0];
//Software Cycle 6
int1 = (++src)[0];
int0 += exponentMask;
((long*) dest)[3] = int3;
float1 = dest[1];
float0 -= bias;
//Software Cycle 7
int2 = (++src)[0];
int1 += exponentMask;
((long*) dest)[4] = int0;
float2 = dest[2];
float1 -= bias;
dest--;
//Software Cycle 8
int3 = (++src)[0];
int2 += exponentMask;
((long*) dest)[6] = int1;
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;
count -= 8;
loopCount = count / 4;
count &= 3;
while( loopCount-- )
{
//Software Cycle A
int0 = (++src)[0];
int3 += exponentMask;
((long*) dest)[6] = int2;
float0 = dest[4];
float3 -= bias;
(++dest)[0] = float1;
//Software Cycle B
int1 = (++src)[0];
int0 += exponentMask;
((long*) dest)[6] = int3;
float1 = dest[4];
float0 -= bias;
(++dest)[0] = float2;
//Software Cycle C
int2 = (++src)[0];
int1 += exponentMask;
((long*) dest)[6] = int0;
float2 = dest[4];
float1 -= bias;
(++dest)[0] = float3;
//Software Cycle D
int3 = (++src)[0];
int2 += exponentMask;
((long*) dest)[6] = int1;
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;
}
//Software Cycle 7
int3 += exponentMask;
((long*) dest)[6] = int2;
float0 = dest[4];
float3 -= bias;
(++dest)[0] = float1;
//Software Cycle 6
((long*) dest)[6] = int3;
float1 = dest[4];
float0 -= bias;
(++dest)[0] = float2;
//Software Cycle 5
float2 = dest[4];
float1 -= bias;
(++dest)[0] = float3;
//Software Cycle 4
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;
//Software Cycle 3
float3 -= bias;
(++dest)[0] = float1;
//Software Cycle 2
(++dest)[0] = float2;
//Software Cycle 1
(++dest)[0] = float3;
dest++;
}
while( count-- )
{
register long value = (++src)[0];
value += exponentMask;
((long*) dest)[0] = value;
dest[0] -= bias;
dest++;
}
}
// bitDepth may be less than 16, e.g. for low-aligned 12 bit samples
void NativeInt16ToFloat32( signed short *src, float *dest, unsigned int count, int bitDepth )
{
register float bias;
register long exponentMask = ((0x97UL - bitDepth) << 23) | 0x8000; //FP exponent + bias for sign
register long int0, int1, int2, int3;
register float float0, float1, float2, float3;
register unsigned long loopCount;
union
{
float f;
long i;
} exponent;
exponent.i = exponentMask;
bias = exponent.f;
src--;
if( count >= 8 )
{
//Software Cycle 1
int0 = (++src)[0];
//Software Cycle 2
int1 = (++src)[0];
int0 += exponentMask;
//Software Cycle 3
int2 = (++src)[0];
int1 += exponentMask;
((long*) dest)[0] = int0;
//Software Cycle 4
int3 = (++src)[0];
int2 += exponentMask;
((long*) dest)[1] = int1;
//delay one loop for the store to complete
//Software Cycle 5
int0 = (++src)[0];
int3 += exponentMask;
((long*) dest)[2] = int2;
float0 = dest[0];
//Software Cycle 6
int1 = (++src)[0];
int0 += exponentMask;
((long*) dest)[3] = int3;
float1 = dest[1];
float0 -= bias;
//Software Cycle 7
int2 = (++src)[0];
int1 += exponentMask;
((long*) dest)[4] = int0;
float2 = dest[2];
float1 -= bias;
dest--;
//Software Cycle 8
int3 = (++src)[0];
int2 += exponentMask;
((long*) dest)[6] = int1;
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;
count -= 8;
loopCount = count / 4;
count &= 3;
while( loopCount-- )
{
//Software Cycle A
int0 = (++src)[0];
int3 += exponentMask;
((long*) dest)[6] = int2;
float0 = dest[4];
float3 -= bias;
(++dest)[0] = float1;
//Software Cycle B
int1 = (++src)[0];
int0 += exponentMask;
((long*) dest)[6] = int3;
float1 = dest[4];
float0 -= bias;
(++dest)[0] = float2;
//Software Cycle C
int2 = (++src)[0];
int1 += exponentMask;
((long*) dest)[6] = int0;
float2 = dest[4];
float1 -= bias;
(++dest)[0] = float3;
//Software Cycle D
int3 = (++src)[0];
int2 += exponentMask;
((long*) dest)[6] = int1;
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;
}
//Software Cycle 7
int3 += exponentMask;
((long*) dest)[6] = int2;
float0 = dest[4];
float3 -= bias;
(++dest)[0] = float1;
//Software Cycle 6
((long*) dest)[6] = int3;
float1 = dest[4];
float0 -= bias;
(++dest)[0] = float2;
//Software Cycle 5
float2 = dest[4];
float1 -= bias;
(++dest)[0] = float3;
//Software Cycle 4
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;
//Software Cycle 3
float3 -= bias;
(++dest)[0] = float1;
//Software Cycle 2
(++dest)[0] = float2;
//Software Cycle 1
(++dest)[0] = float3;
dest++;
}
while( count-- )
{
register long value = (++src)[0];
value += exponentMask;
((long*) dest)[0] = value;
dest[0] -= bias;
dest++;
}
}
// bitDepth may be less than 16, e.g. for low-aligned 12 bit samples
void SwapInt16ToFloat32( signed short *src, float *dest, unsigned int count, int bitDepth )
{
register float bias;
register long exponentMask = ((0x97UL - bitDepth) << 23) | 0x8000; //FP exponent + bias for sign
register long int0, int1, int2, int3;
register float float0, float1, float2, float3;
register unsigned long loopCount;
union
{
float f;
long i;
}exponent;
exponent.i = exponentMask;
bias = exponent.f;
src--;
if( count >= 8 )
{
//Software Cycle 1
int0 = __lhbrx(0, ++src);
//Software Cycle 2
int1 = __lhbrx(0, ++src);
int0 += exponentMask;
//Software Cycle 3
int2 = __lhbrx(0, ++src);
int1 += exponentMask;
((long*) dest)[0] = int0;
//Software Cycle 4
int3 = __lhbrx(0, ++src);
int2 += exponentMask;
((long*) dest)[1] = int1;
//delay one loop for the store to complete
//Software Cycle 5
int0 = __lhbrx(0, ++src);
int3 += exponentMask;
((long*) dest)[2] = int2;
float0 = dest[0];
//Software Cycle 6
int1 = __lhbrx(0, ++src);
int0 += exponentMask;
((long*) dest)[3] = int3;
float1 = dest[1];
float0 -= bias;
//Software Cycle 7
int2 = __lhbrx(0, ++src);
int1 += exponentMask;
((long*) dest)[4] = int0;
float2 = dest[2];
float1 -= bias;
dest--;
//Software Cycle 8
int3 = __lhbrx(0, ++src);
int2 += exponentMask;
((long*) dest)[6] = int1;
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;
count -= 8;
loopCount = count / 4;
count &= 3;
while( loopCount-- )
{
//Software Cycle A
int0 = __lhbrx(0, ++src);
int3 += exponentMask;
((long*) dest)[6] = int2;
float0 = dest[4];
float3 -= bias;
(++dest)[0] = float1;
//Software Cycle B
int1 = __lhbrx(0, ++src);
int0 += exponentMask;
((long*) dest)[6] = int3;
float1 = dest[4];
float0 -= bias;
(++dest)[0] = float2;
//Software Cycle C
int2 = __lhbrx(0, ++src);
int1 += exponentMask;
((long*) dest)[6] = int0;
float2 = dest[4];
float1 -= bias;
(++dest)[0] = float3;
//Software Cycle D
int3 = __lhbrx(0, ++src);
int2 += exponentMask;
((long*) dest)[6] = int1;
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;
}
//Software Cycle 7
int3 += exponentMask;
((long*) dest)[6] = int2;
float0 = dest[4];
float3 -= bias;
(++dest)[0] = float1;
//Software Cycle 6
((long*) dest)[6] = int3;
float1 = dest[4];
float0 -= bias;
(++dest)[0] = float2;
//Software Cycle 5
float2 = dest[4];
float1 -= bias;
(++dest)[0] = float3;
//Software Cycle 4
float3 = dest[4];
float2 -= bias;
(++dest)[0] = float0;