1 /* Copyright: © Copyright 2005 Apple Computer, Inc. All rights reserved.
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /*=============================================================================
39 CAStreamBasicDescription.cpp
41 =============================================================================*/
43 #include "CAConditionalMacros.h"
45 #include "CAStreamBasicDescription.h"
48 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
49 #include <CoreFoundation/CFByteOrder.h>
51 #include <CFByteOrder.h>
54 #pragma mark This file needs to compile on more earlier versions of the OS, so please keep that in mind when editing it
56 const AudioStreamBasicDescription
CAStreamBasicDescription::sEmpty
= { 0.0, 0, 0, 0, 0, 0, 0, 0, 0 };
58 CAStreamBasicDescription::CAStreamBasicDescription(double inSampleRate
, UInt32 inFormatID
,
59 UInt32 inBytesPerPacket
, UInt32 inFramesPerPacket
,
60 UInt32 inBytesPerFrame
, UInt32 inChannelsPerFrame
,
61 UInt32 inBitsPerChannel
, UInt32 inFormatFlags
)
63 mSampleRate
= inSampleRate
;
64 mFormatID
= inFormatID
;
65 mBytesPerPacket
= inBytesPerPacket
;
66 mFramesPerPacket
= inFramesPerPacket
;
67 mBytesPerFrame
= inBytesPerFrame
;
68 mChannelsPerFrame
= inChannelsPerFrame
;
69 mBitsPerChannel
= inBitsPerChannel
;
70 mFormatFlags
= inFormatFlags
;
73 void CAStreamBasicDescription::PrintFormat(FILE *f
, const char *indent
, const char *name
) const
75 fprintf(f
, "%s%s ", indent
, name
);
77 *(UInt32
*)formatID
= CFSwapInt32HostToBig(mFormatID
);
79 fprintf(f
, "%2ld ch, %6.0f Hz, '%-4.4s' (0x%08lX) ",
80 NumberChannels(), mSampleRate
, formatID
,
82 if (mFormatID
== kAudioFormatLinearPCM
) {
83 bool isInt
= !(mFormatFlags
& kLinearPCMFormatFlagIsFloat
);
84 int wordSize
= SampleWordSize();
85 const char *endian
= (wordSize
> 1) ?
86 ((mFormatFlags
& kLinearPCMFormatFlagIsBigEndian
) ? " big-endian" : " little-endian" ) : "";
87 const char *sign
= isInt
?
88 ((mFormatFlags
& kLinearPCMFormatFlagIsSignedInteger
) ? " signed" : " unsigned") : "";
89 const char *floatInt
= isInt
? "integer" : "float";
91 if (wordSize
> 0 && PackednessIsSignificant()) {
92 if (mFormatFlags
& kLinearPCMFormatFlagIsPacked
)
93 sprintf(packed
, "packed in %d bytes", wordSize
);
95 sprintf(packed
, "unpacked in %d bytes", wordSize
);
98 const char *align
= (wordSize
> 0 && AlignmentIsSignificant()) ?
99 ((mFormatFlags
& kLinearPCMFormatFlagIsAlignedHigh
) ? " high-aligned" : " low-aligned") : "";
100 const char *deinter
= (mFormatFlags
& kAudioFormatFlagIsNonInterleaved
) ? ", deinterleaved" : "";
101 const char *commaSpace
= (packed
[0]!='\0') || (align
[0]!='\0') ? ", " : "";
103 fprintf(f
, "%ld-bit%s%s %s%s%s%s%s\n",
104 mBitsPerChannel
, endian
, sign
, floatInt
,
105 commaSpace
, packed
, align
, deinter
);
106 } else if (mFormatID
== 'alac') { // kAudioFormatAppleLossless
108 switch (mFormatFlags
)
110 case 1: // kAppleLosslessFormatFlag_16BitSourceData
113 case 2: // kAppleLosslessFormatFlag_20BitSourceData
116 case 3: // kAppleLosslessFormatFlag_24BitSourceData
119 case 4: // kAppleLosslessFormatFlag_32BitSourceData
124 fprintf(f
, "from %d-bit source, ", sourceBits
);
126 fprintf(f
, "from UNKNOWN source bit depth, ");
128 fprintf(f
, "%ld frames/packet\n", mFramesPerPacket
);
131 fprintf(f
, "%ld bits/channel, %ld bytes/packet, %ld frames/packet, %ld bytes/frame\n",
132 mBitsPerChannel
, mBytesPerPacket
, mFramesPerPacket
, mBytesPerFrame
);
135 void CAStreamBasicDescription::NormalizeLinearPCMFormat(AudioStreamBasicDescription
& ioDescription
)
137 // the only thing that changes is to make mixable linear PCM into the canonical linear PCM format
138 if((ioDescription
.mFormatID
== kAudioFormatLinearPCM
) && ((ioDescription
.mFormatFlags
& kIsNonMixableFlag
) == 0))
140 // the canonical linear PCM format is 32 bit native endian floats
141 ioDescription
.mFormatFlags
= kAudioFormatFlagsNativeFloatPacked
;
142 ioDescription
.mBytesPerPacket
= sizeof(Float32
) * ioDescription
.mChannelsPerFrame
;
143 ioDescription
.mFramesPerPacket
= 1;
144 ioDescription
.mBytesPerFrame
= sizeof(Float32
) * ioDescription
.mChannelsPerFrame
;
145 ioDescription
.mBitsPerChannel
= 8 * sizeof(Float32
);
149 void CAStreamBasicDescription::ResetFormat(AudioStreamBasicDescription
& ioDescription
)
151 ioDescription
.mSampleRate
= 0;
152 ioDescription
.mFormatID
= 0;
153 ioDescription
.mBytesPerPacket
= 0;
154 ioDescription
.mFramesPerPacket
= 0;
155 ioDescription
.mBytesPerFrame
= 0;
156 ioDescription
.mChannelsPerFrame
= 0;
157 ioDescription
.mBitsPerChannel
= 0;
158 ioDescription
.mFormatFlags
= 0;
161 void CAStreamBasicDescription::FillOutFormat(AudioStreamBasicDescription
& ioDescription
, const AudioStreamBasicDescription
& inTemplateDescription
)
163 if(fiszero(ioDescription
.mSampleRate
))
165 ioDescription
.mSampleRate
= inTemplateDescription
.mSampleRate
;
167 if(ioDescription
.mFormatID
== 0)
169 ioDescription
.mFormatID
= inTemplateDescription
.mFormatID
;
171 if(ioDescription
.mFormatFlags
== 0)
173 ioDescription
.mFormatFlags
= inTemplateDescription
.mFormatFlags
;
175 if(ioDescription
.mBytesPerPacket
== 0)
177 ioDescription
.mBytesPerPacket
= inTemplateDescription
.mBytesPerPacket
;
179 if(ioDescription
.mFramesPerPacket
== 0)
181 ioDescription
.mFramesPerPacket
= inTemplateDescription
.mFramesPerPacket
;
183 if(ioDescription
.mBytesPerFrame
== 0)
185 ioDescription
.mBytesPerFrame
= inTemplateDescription
.mBytesPerFrame
;
187 if(ioDescription
.mChannelsPerFrame
== 0)
189 ioDescription
.mChannelsPerFrame
= inTemplateDescription
.mChannelsPerFrame
;
191 if(ioDescription
.mBitsPerChannel
== 0)
193 ioDescription
.mBitsPerChannel
= inTemplateDescription
.mBitsPerChannel
;
197 void CAStreamBasicDescription::GetSimpleName(const AudioStreamBasicDescription
& inDescription
, char* outName
, bool inAbbreviate
)
199 switch(inDescription
.mFormatID
)
201 case kAudioFormatLinearPCM
:
203 const char* theEndianString
= NULL
;
204 if((inDescription
.mFormatFlags
& kAudioFormatFlagIsBigEndian
) != 0)
206 #if TARGET_RT_LITTLE_ENDIAN
207 theEndianString
= "Big Endian";
212 #if TARGET_RT_BIG_ENDIAN
213 theEndianString
= "Little Endian";
217 const char* theKindString
= NULL
;
218 if((inDescription
.mFormatFlags
& kAudioFormatFlagIsFloat
) != 0)
220 theKindString
= (inAbbreviate
? "Float" : "Floating Point");
222 else if((inDescription
.mFormatFlags
& kAudioFormatFlagIsSignedInteger
) != 0)
224 theKindString
= (inAbbreviate
? "SInt" : "Signed Integer");
228 theKindString
= (inAbbreviate
? "UInt" : "Unsigned Integer");
231 const char* thePackingString
= NULL
;
232 if((inDescription
.mFormatFlags
& kAudioFormatFlagIsPacked
) == 0)
234 if((inDescription
.mFormatFlags
& kAudioFormatFlagIsAlignedHigh
) != 0)
236 thePackingString
= "High";
240 thePackingString
= "Low";
244 const char* theMixabilityString
= NULL
;
245 if((inDescription
.mFormatFlags
& kIsNonMixableFlag
) == 0)
247 theMixabilityString
= "Mixable";
251 theMixabilityString
= "Unmixable";
256 if(theEndianString
!= NULL
)
258 if(thePackingString
!= NULL
)
260 sprintf(outName
, "%s %d Ch %s %s %s%d/%s%d", theMixabilityString
, (int)inDescription
.mChannelsPerFrame
, theEndianString
, thePackingString
, theKindString
, (int)inDescription
.mBitsPerChannel
, theKindString
, (int)(inDescription
.mBytesPerFrame
/ inDescription
.mChannelsPerFrame
) * 8);
264 sprintf(outName
, "%s %d Ch %s %s%d", theMixabilityString
, (int)inDescription
.mChannelsPerFrame
, theEndianString
, theKindString
, (int)inDescription
.mBitsPerChannel
);
269 if(thePackingString
!= NULL
)
271 sprintf(outName
, "%s %d Ch %s %s%d/%s%d", theMixabilityString
, (int)inDescription
.mChannelsPerFrame
, thePackingString
, theKindString
, (int)inDescription
.mBitsPerChannel
, theKindString
, (int)((inDescription
.mBytesPerFrame
/ inDescription
.mChannelsPerFrame
) * 8));
275 sprintf(outName
, "%s %d Ch %s%d", theMixabilityString
, (int)inDescription
.mChannelsPerFrame
, theKindString
, (int)inDescription
.mBitsPerChannel
);
281 if(theEndianString
!= NULL
)
283 if(thePackingString
!= NULL
)
285 sprintf(outName
, "%s %d Channel %d Bit %s %s Aligned %s in %d Bits", theMixabilityString
, (int)inDescription
.mChannelsPerFrame
, (int)inDescription
.mBitsPerChannel
, theEndianString
, theKindString
, thePackingString
, (int)(inDescription
.mBytesPerFrame
/ inDescription
.mChannelsPerFrame
) * 8);
289 sprintf(outName
, "%s %d Channel %d Bit %s %s", theMixabilityString
, (int)inDescription
.mChannelsPerFrame
, (int)inDescription
.mBitsPerChannel
, theEndianString
, theKindString
);
294 if(thePackingString
!= NULL
)
296 sprintf(outName
, "%s %d Channel %d Bit %s Aligned %s in %d Bits", theMixabilityString
, (int)inDescription
.mChannelsPerFrame
, (int)inDescription
.mBitsPerChannel
, theKindString
, thePackingString
, (int)(inDescription
.mBytesPerFrame
/ inDescription
.mChannelsPerFrame
) * 8);
300 sprintf(outName
, "%s %d Channel %d Bit %s", theMixabilityString
, (int)inDescription
.mChannelsPerFrame
, (int)inDescription
.mBitsPerChannel
, theKindString
);
307 case kAudioFormatAC3
:
308 strcpy(outName
, "AC-3");
311 case kAudioFormat60958AC3
:
312 strcpy(outName
, "AC-3 for SPDIF");
317 char* the4CCString
= (char*)&inDescription
.mFormatID
;
318 outName
[0] = the4CCString
[0];
319 outName
[1] = the4CCString
[1];
320 outName
[2] = the4CCString
[2];
321 outName
[3] = the4CCString
[3];
329 #include "CALogMacros.h"
331 void CAStreamBasicDescription::PrintToLog(const AudioStreamBasicDescription
& inDesc
)
333 PrintFloat (" Sample Rate: ", inDesc
.mSampleRate
);
334 Print4CharCode (" Format ID: ", inDesc
.mFormatID
);
335 PrintHex (" Format Flags: ", inDesc
.mFormatFlags
);
336 PrintInt (" Bytes per Packet: ", inDesc
.mBytesPerPacket
);
337 PrintInt (" Frames per Packet: ", inDesc
.mFramesPerPacket
);
338 PrintInt (" Bytes per Frame: ", inDesc
.mBytesPerFrame
);
339 PrintInt (" Channels per Frame: ", inDesc
.mChannelsPerFrame
);
340 PrintInt (" Bits per Channel: ", inDesc
.mBitsPerChannel
);
344 bool operator<(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
)
346 bool theAnswer
= false;
349 // note that if either side is 0, that field is skipped
351 // format ID is the first order sort
352 if((!isDone
) && ((x
.mFormatID
!= 0) && (y
.mFormatID
!= 0)))
354 if(x
.mFormatID
!= y
.mFormatID
)
356 // formats are sorted numerically except that linear
357 // PCM is always first
358 if(x
.mFormatID
== kAudioFormatLinearPCM
)
362 else if(y
.mFormatID
== kAudioFormatLinearPCM
)
368 theAnswer
= x
.mFormatID
< y
.mFormatID
;
375 // mixable is always better than non-mixable for linear PCM and should be the second order sort item
376 if((!isDone
) && ((x
.mFormatID
== kAudioFormatLinearPCM
) && (y
.mFormatID
== kAudioFormatLinearPCM
)))
378 if(((x
.mFormatFlags
& kIsNonMixableFlag
) == 0) && ((y
.mFormatFlags
& kIsNonMixableFlag
) != 0))
383 else if(((x
.mFormatFlags
& kIsNonMixableFlag
) != 0) && ((y
.mFormatFlags
& kIsNonMixableFlag
) == 0))
390 // floating point vs integer for linear PCM only
391 if((!isDone
) && ((x
.mFormatID
== kAudioFormatLinearPCM
) && (y
.mFormatID
== kAudioFormatLinearPCM
)))
393 if((x
.mFormatFlags
& kAudioFormatFlagIsFloat
) != (y
.mFormatFlags
& kAudioFormatFlagIsFloat
))
395 // floating point is better than integer
396 theAnswer
= y
.mFormatFlags
& kAudioFormatFlagIsFloat
;
402 if((!isDone
) && ((x
.mBitsPerChannel
!= 0) && (y
.mBitsPerChannel
!= 0)))
404 if(x
.mBitsPerChannel
!= y
.mBitsPerChannel
)
406 // deeper bit depths are higher quality
407 theAnswer
= x
.mBitsPerChannel
< y
.mBitsPerChannel
;
413 if((!isDone
) && fnonzero(x
.mSampleRate
) && fnonzero(y
.mSampleRate
))
415 if(fnotequal(x
.mSampleRate
, y
.mSampleRate
))
417 // higher sample rates are higher quality
418 theAnswer
= x
.mSampleRate
< y
.mSampleRate
;
423 // number of channels
424 if((!isDone
) && ((x
.mChannelsPerFrame
!= 0) && (y
.mChannelsPerFrame
!= 0)))
426 if(x
.mChannelsPerFrame
!= y
.mChannelsPerFrame
)
428 // more channels is higher quality
429 theAnswer
= x
.mChannelsPerFrame
< y
.mChannelsPerFrame
;
437 static bool MatchFormatFlags(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
)
439 UInt32 xFlags
= x
.mFormatFlags
;
440 UInt32 yFlags
= y
.mFormatFlags
;
443 if (x
.mFormatID
== 0 || y
.mFormatID
== 0 || xFlags
== 0 || yFlags
== 0)
446 if (x
.mFormatID
== kAudioFormatLinearPCM
)
448 // knock off the all clear flag
449 xFlags
= xFlags
& ~kAudioFormatFlagsAreAllClear
;
450 yFlags
= yFlags
& ~kAudioFormatFlagsAreAllClear
;
452 // if both kAudioFormatFlagIsPacked bits are set, then we don't care about the kAudioFormatFlagIsAlignedHigh bit.
453 if (xFlags
& yFlags
& kAudioFormatFlagIsPacked
) {
454 xFlags
= xFlags
& ~kAudioFormatFlagIsAlignedHigh
;
455 yFlags
= yFlags
& ~kAudioFormatFlagIsAlignedHigh
;
458 // if both kAudioFormatFlagIsFloat bits are set, then we don't care about the kAudioFormatFlagIsSignedInteger bit.
459 if (xFlags
& yFlags
& kAudioFormatFlagIsFloat
) {
460 xFlags
= xFlags
& ~kAudioFormatFlagIsSignedInteger
;
461 yFlags
= yFlags
& ~kAudioFormatFlagIsSignedInteger
;
464 // if the bit depth is 8 bits or less and the format is packed, we don't care about endianness
465 if((x
.mBitsPerChannel
<= 8) && ((xFlags
& kAudioFormatFlagIsPacked
) == kAudioFormatFlagIsPacked
))
467 xFlags
= xFlags
& ~kAudioFormatFlagIsBigEndian
;
469 if((y
.mBitsPerChannel
<= 8) && ((yFlags
& kAudioFormatFlagIsPacked
) == kAudioFormatFlagIsPacked
))
471 yFlags
= yFlags
& ~kAudioFormatFlagIsBigEndian
;
474 // if the number of channels is 0 or 1, we don't care about non-interleavedness
475 if (x
.mChannelsPerFrame
<= 1 && y
.mChannelsPerFrame
<= 1) {
476 xFlags
&= ~kLinearPCMFormatFlagIsNonInterleaved
;
477 yFlags
&= ~kLinearPCMFormatFlagIsNonInterleaved
;
480 return xFlags
== yFlags
;
483 bool operator==(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
)
485 // the semantics for equality are:
486 // 1) Values must match exactly
487 // 2) wildcard's are ignored in the comparison
489 #define MATCH(name) ((x.name) == 0 || (y.name) == 0 || (x.name) == (y.name))
492 // check the sample rate
493 (fiszero(x
.mSampleRate
) || fiszero(y
.mSampleRate
) || fequal(x
.mSampleRate
, y
.mSampleRate
))
495 // check the format ids
498 // check the format flags
499 && MatchFormatFlags(x
, y
)
501 // check the bytes per packet
502 && MATCH(mBytesPerPacket
)
504 // check the frames per packet
505 && MATCH(mFramesPerPacket
)
507 // check the bytes per frame
508 && MATCH(mBytesPerFrame
)
510 // check the channels per frame
511 && MATCH(mChannelsPerFrame
)
513 // check the channels per frame
514 && MATCH(mBitsPerChannel
) ;
517 bool SanityCheck(const AudioStreamBasicDescription
& x
)
519 return (x
.mSampleRate
>= 0.);