this repo has no description
1/* Copyright: � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
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.
8
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.
23
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.
29
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.
37*/
38/*=============================================================================
39 AFToolsCommon.cpp
40
41=============================================================================*/
42
43#include "AFToolsCommon.h"
44#include "CAAudioFileFormats.h"
45#include <ctype.h>
46#include <CarbonCore/Endian.h>
47
48
49/*
50struct AudioStreamBasicDescription
51{
52 Float64 mSampleRate; // the native sample rate of the audio stream
53 UInt32 mFormatID; // the specific encoding type of audio stream
54 UInt32 mFormatFlags; // flags specific to each format
55 UInt32 mBytesPerPacket; // the number of bytes in a packet
56 UInt32 mFramesPerPacket; // the number of frames in each packet
57 UInt32 mBytesPerFrame; // the number of bytes in a frame
58 UInt32 mChannelsPerFrame; // the number of channels in each frame
59 UInt32 mBitsPerChannel; // the number of bits in each channel
60 UInt32 mReserved; // reserved, pads the structure out to force 8 byte alignment
61};
62*/
63bool ParseStreamDescription(const char *inTextDesc, CAStreamBasicDescription &fmt)
64{
65 const char *p = inTextDesc;
66 int bitdepth = 0;
67 CAAudioFileFormats *theFileFormats = CAAudioFileFormats::Instance();
68
69 memset(&fmt, 0, sizeof(fmt));
70 OSType formatID;
71 int x;
72 if (strchr("-@/#", p[3])) {
73 // special-case for 3-char format ID's ending with a space
74 char fmtstr[4];
75 memcpy(fmtstr, p, 3);
76 fmtstr[3] = ' ';
77 formatID = EndianU32_BtoN(*(UInt32 *)fmtstr);
78 x = 3;
79 } else {
80 x = StrToOSType(p, formatID);
81 }
82 if (theFileFormats->IsKnownDataFormat(formatID)) {
83 p += x;
84 fmt.mFormatID = formatID;
85 if (*p == '-') {
86 ++p;
87 bitdepth = 0;
88 while (isdigit(*p))
89 bitdepth = 10 * bitdepth + *p++ - '0';
90 fmt.mBitsPerChannel = bitdepth;
91 }
92 }
93
94 if (fmt.mFormatID == 0) {
95 // unknown format, assume LPCM
96 if (p[0] == '-') // previously we required a leading dash on PCM formats
97 // pcm
98 ++p;
99 fmt.mFormatID = kAudioFormatLinearPCM;
100 fmt.mFormatFlags = kAudioFormatFlagIsPacked;
101 fmt.mFramesPerPacket = 1;
102 fmt.mChannelsPerFrame = 1;
103 bool isUnsigned = false;
104
105 if (p[0] == 'B' && p[1] == 'E') {
106 fmt.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
107 p += 2;
108 } else if (p[0] == 'L' && p[1] == 'E') {
109 p += 2;
110 } else {
111 // default is native-endian
112#if TARGET_RT_BIG_ENDIAN
113 fmt.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
114#endif
115 }
116 if (p[0] == 'F') {
117 fmt.mFormatFlags |= kAudioFormatFlagIsFloat;
118 ++p;
119 } else {
120 if (p[0] == 'U') {
121 isUnsigned = true;
122 ++p;
123 }
124 if (p[0] == 'I')
125 ++p;
126 else {
127 fprintf(stderr, "The format '%s' is unknown or an unparseable PCM format specifier\n", inTextDesc);
128 goto Bail;
129 }
130 }
131
132 while (isdigit(*p))
133 bitdepth = 10 * bitdepth + *p++ - '0';
134 if (fmt.mFormatFlags & kAudioFormatFlagIsFloat) {
135 if (bitdepth != 32 && bitdepth != 64) {
136 fprintf(stderr, "Valid float bitdepths are 32 and 64\n");
137 goto Bail;
138 }
139 } else {
140 if (bitdepth != 8 && bitdepth != 16 && bitdepth != 24 && bitdepth != 32) {
141 fprintf(stderr, "Valid integer bitdepths are 8, 16, 24, and 32\n");
142 goto Bail;
143 }
144 if (!isUnsigned)
145 fmt.mFormatFlags |= kAudioFormatFlagIsSignedInteger;
146 }
147 fmt.mBitsPerChannel = bitdepth;
148 fmt.mBytesPerPacket = fmt.mBytesPerFrame = bitdepth / 8;
149 }
150 if (*p == '@') {
151 ++p;
152 while (isdigit(*p))
153 fmt.mSampleRate = 10 * fmt.mSampleRate + (*p++ - '0');
154 }
155 if (*p == '/') {
156 UInt32 flags = 0;
157 while (true) {
158 char c = *++p;
159 if (c >= '0' && c <= '9')
160 flags = (flags << 4) | (c - '0');
161 else if (c >= 'A' && c <= 'F')
162 flags = (flags << 4) | (c - 'A' + 10);
163 else if (c >= 'a' && c <= 'f')
164 flags = (flags << 4) | (c - 'a' + 10);
165 else break;
166 }
167 fmt.mFormatFlags = flags;
168 }
169 if (*p == '#') {
170 ++p;
171 while (isdigit(*p))
172 fmt.mFramesPerPacket = 10 * fmt.mFramesPerPacket + (*p++ - '0');
173 }
174 if (*p != '\0')
175 goto Bail;
176 return true;
177
178Bail:
179 fprintf(stderr, "Invalid format string: %s\n", inTextDesc);
180 fprintf(stderr, "Syntax of format strings is: [-][BE|LE]{F|I|UI}{8|16|24|32|64}[@sample_rate_hz][/format_flags]\n");
181 return false;
182}
183
184void PrintAudioFileTypesAndFormats(FILE *outfile)
185{
186 CAAudioFileFormats *theFileFormats = CAAudioFileFormats::Instance();
187
188 for (int i = 0; i < theFileFormats->mNumFileFormats; ++i) {
189 CAAudioFileFormats::FileFormatInfo *ffi = &theFileFormats->mFileFormats[i];
190
191 int j;
192 char buf[20];
193 char fmtName[256] = { 0 };
194 if (ffi->mFileTypeName)
195 CFStringGetCString(ffi->mFileTypeName, fmtName, sizeof(fmtName), kCFStringEncodingUTF8);
196 fprintf(outfile, " '%s' = %s", OSTypeToStr(buf, sizeof(buf), ffi->mFileTypeID), fmtName);
197
198 int next = ffi->NumberOfExtensions();
199 if (next > 0) {
200 fprintf(outfile, " (");
201 for (j = 0; j < next; ++j) {
202 if (j > 0) fprintf(outfile, ", ");
203 fprintf(outfile, ".%s", ffi->GetExtension(j, buf, sizeof(buf)));
204 }
205 fprintf(outfile, ")");
206 }
207
208 fprintf(outfile, "\n data_formats: ");
209 int count = 0;
210 for (j = 0; j < ffi->mNumDataFormats; ++j) {
211 CAAudioFileFormats::DataFormatInfo *dfi = &ffi->mDataFormats[j];
212 if (dfi->mFormatID == kAudioFormatLinearPCM) {
213 for (int k = 0; k < dfi->mNumVariants; ++k) {
214 if (++count == 6) {
215 fprintf(outfile, "\n ");
216 count = 0;
217 }
218 AudioStreamBasicDescription *asbd = &dfi->mVariants[k];
219 if (asbd->mFormatFlags & ~(kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsFloat))
220 fprintf(outfile, "(%08lx/%ld) ", asbd->mFormatFlags, asbd->mBitsPerChannel);
221 else {
222 fprintf(outfile, "%s",
223 (asbd->mFormatFlags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE");
224 if (asbd->mFormatFlags & kAudioFormatFlagIsFloat)
225 fprintf(outfile, "F");
226 else
227 fprintf(outfile, "%sI",
228 (asbd->mFormatFlags & kAudioFormatFlagIsSignedInteger) ? "" : "U");
229 fprintf(outfile, "%ld ", asbd->mBitsPerChannel);
230 }
231 }
232 } else {
233 if (++count == 6) {
234 fprintf(outfile, "\n ");
235 count = 0;
236 }
237 fprintf(outfile, "'%s' ", OSTypeToStr(buf, sizeof(buf), dfi->mFormatID));
238 }
239 }
240 fprintf(outfile, "\n");
241 }
242}
243
244#if 0
245char *OSTypeToStr(char *buf, OSType t)
246{
247 char *p = buf;
248 char str[4], *q = str;
249 *(UInt32 *)str = EndianU32_NtoB(t);
250 for (int i = 0; i < 4; ++i) {
251 if (isprint(*q) && *q != '\\')
252 *p++ = *q++;
253 else {
254 sprintf(p, "\\x%02x", *q++);
255 p += 4;
256 }
257 }
258 *p = '\0';
259 return buf;
260}
261
262int StrToOSType(const char *str, OSType &t)
263{
264 char buf[4];
265 const char *p = str;
266 int x;
267 for (int i = 0; i < 4; ++i) {
268 if (*p != '\\') {
269 if ((buf[i] = *p++) == '\0')
270 goto fail;
271 } else {
272 if (*++p != 'x') goto fail;
273 if (sscanf(++p, "%02X", &x) != 1) goto fail;
274 buf[i] = x;
275 p += 2;
276 }
277 }
278 t = EndianU32_BtoN(*(UInt32 *)buf);
279 return p - str;
280fail:
281 return 0;
282}
283#endif