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 QTAACFile.cpp
40
41=============================================================================*/
42
43#include "QTAACFile.h"
44#include "CAXException.h"
45#include "CAStreamBasicDescription.h"
46
47
48// _______________________________________________________________________________________
49//
50QTAACFile::QTAACFile() :
51 mMovieResFile(-1),
52 mMovie(NULL),
53 mPacketHandle(NULL),
54 mMagicCookie(NULL),
55 mConverter(NULL),
56 mFileMaxPacketSize(0)
57{
58}
59
60// _______________________________________________________________________________________
61//
62QTAACFile::~QTAACFile()
63{
64 Close();
65 CloseConverter();
66 if (mPacketHandle)
67 DisposeHandle(mPacketHandle);
68}
69
70// _______________________________________________________________________________________
71//
72void QTAACFile::Close()
73{
74 if (mMovieResFile != -1) {
75 CloseMovieFile(mMovieResFile);
76 mMovieResFile = -1;
77 }
78 if (mMovie) {
79 DisposeMovie(mMovie);
80 mMovie = NULL;
81 }
82 if (mMagicCookie) {
83 free(mMagicCookie);
84 mMagicCookie = NULL;
85 }
86}
87
88// _______________________________________________________________________________________
89//
90void QTAACFile::CloseConverter()
91{
92 if (mConverter) {
93 AudioConverterDispose(mConverter);
94 mConverter = NULL;
95 }
96}
97
98// _______________________________________________________________________________________
99/*
100 GetMP4DecoderConfig
101
102 NOTE: This is designed to work only for a very specific case -- an MPEG-4/QuickTime movie
103 containing AAC-encoded audio. It is adapted from the "SoundPlayer"QuickTime sample code,
104 but with much generality removed.
105
106 If the audio is not MPEG-4, then we'll fail immediately with an exception.
107
108 If the audio is MPEG-4, but not AAC-encoded, then we will fail somewhere beyond this function, when passing the magic cookie (ESDS) to the AudioConverter.
109*/
110static void GetMP4DecoderConfig(Media inMedia, AudioStreamBasicDescription &outStreamFormat, char **outEsds, UInt32 *outEsdsSize)
111{
112 OSStatus err = noErr;
113 Size size;
114 Handle extension = NULL;
115 SoundDescriptionHandle hSoundDescription = (SoundDescriptionHandle)NewHandle(0);
116 struct MyMP4AudioAtom {
117 UInt32 size;
118 OSType atomType;
119 UInt32 version;
120 };
121
122 try {
123 // get the description of the sample data
124 GetMediaSampleDescription(inMedia, 1, (SampleDescriptionHandle)hSoundDescription);
125 XThrowIfError(GetMoviesError(), "couldn't get media sample description");
126
127 // other formats: exercise for the reader!
128 OSType fmt = (*hSoundDescription)->dataFormat;
129 XThrowIf(fmt != 'mp4a', -1, "can only decode MPEG-4/AAC");
130
131 extension = NewHandle(0);
132
133 // get the "magic" decompression atom
134 // This extension to the SoundDescription information stores data specific to a given audio decompressor.
135 // Some audio decompression algorithms require a set of out-of-stream values to configure the decompressor
136 // which are stored in a siDecompressionParams atom. The contents of the siDecompressionParams atom are dependent
137 // on the audio decompressor.
138
139 err = GetSoundDescriptionExtension(hSoundDescription, &extension, siDecompressionParams);
140
141 // if it doesn't have an atom, that's ok for some formats, but not for AAC
142 XThrowIfError(err, "couldn't get sound description extension");
143
144 size = GetHandleSize(extension);
145 HLock(extension);
146 UserDataAtom *atom = (UserDataAtom *)*extension;
147 bool moreAtoms = true;
148 do {
149 long atomSize = EndianS32_BtoN(atom->size);
150
151 XThrowIf(atomSize < 8, -1, "invalid sound description atom");
152 switch (EndianU32_BtoN(atom->atomType)) {
153 case 'esds':
154 *outEsdsSize = atomSize - sizeof(MyMP4AudioAtom);
155 *outEsds = (char *)malloc(*outEsdsSize);
156 memcpy(*outEsds, (char *)atom + sizeof(MyMP4AudioAtom), *outEsdsSize);
157 break;
158 case kAudioTerminatorAtomType:
159 moreAtoms = false;
160 break;
161 }
162 atom = (UserDataAtom *)((char *)atom + atomSize);
163 } while (moreAtoms);
164
165 HUnlock(extension);
166
167 // set up our stream description
168 memset(&outStreamFormat, 0, sizeof(AudioStreamBasicDescription));
169 outStreamFormat.mFormatID = kAudioFormatMPEG4AAC;
170 }
171 catch (...) {
172 if (extension) DisposeHandle(extension);
173 if (hSoundDescription) DisposeHandle((Handle)hSoundDescription);
174 throw;
175 }
176 if (extension) DisposeHandle(extension);
177 if (hSoundDescription) DisposeHandle((Handle)hSoundDescription);
178}
179
180// _______________________________________________________________________________________
181//
182void QTAACFile::Open(const char *filename)
183{
184 FSRef theFSRef;
185 Track theAudioTrack;
186 FSSpec theFSSpec;
187
188 // convert from POSIX full path to FSSpec
189 XThrowIfError(FSPathMakeRef((UInt8 *)filename, &theFSRef, NULL), "locate file");
190 XThrowIfError(FSGetCatalogInfo(&theFSRef, 0, NULL, NULL, &theFSSpec, NULL), "get FSSpec");
191
192 // make a QuickTime movie (will open MP4 transparently)
193 XThrowIfError(OpenMovieFile(&theFSSpec, &mMovieResFile, fsRdPerm), "OpenMovieFile failed");
194 SInt16 nResID = 0;
195 Str255 strName;
196 Boolean bWasChanged;
197 XThrowIfError(NewMovieFromFile(&mMovie, mMovieResFile, &nResID, strName, newMovieActive, &bWasChanged), "NewMovieFromFile failed");
198
199 // NOTE: we're assuming the data we want is in the movie's first enabled audio track
200 theAudioTrack = GetMovieIndTrackType(mMovie, 1, SoundMediaType, movieTrackMediaType | movieTrackEnabledOnly);
201 XThrowIf(theAudioTrack == NULL, -1, "movie contains no audio track");
202
203 // find the track's media, and interrogate it for the audio format and magic cookie (decoder config)
204 mMedia = GetTrackMedia(theAudioTrack);
205 XThrowIf(mMedia == NULL, -1, "track contains no media");
206 GetMP4DecoderConfig(mMedia, mFileDataFormat, &mMagicCookie, &mMagicCookieSize);
207
208 // Fill out the remaining fields of the ASBD by evaluating the magic cookie
209 UInt32 size = sizeof(mFileDataFormat);
210 XThrowIfError(AudioFormatGetProperty(kAudioFormatProperty_ASBDFromESDS, mMagicCookieSize, mMagicCookie, &size, &mFileDataFormat),
211 "couldn't get ASBD from the Magic Cookie");
212
213 // build a vector of times at which packets begin
214 // so later we can randomly access the file by packet number
215 TimeValue currentTime = 0;
216 static const Fixed oneFixed = 0x10000;
217 while (true) {
218 TimeValue interestingTime, interestingDuration;
219 GetMediaNextInterestingTime(mMedia, nextTimeMediaSample | nextTimeEdgeOK, currentTime, oneFixed, &interestingTime, &interestingDuration);
220 if (interestingTime < 0)
221 break;
222 mPacketTimes.push_back(interestingTime);
223 currentTime = interestingTime + 1;
224 }
225 mNumberPackets = mPacketTimes.size();
226 mPacketMark = 0;
227 mPacketHandle = NewHandle(1024); // memory into which packets will be read
228}
229
230// _______________________________________________________________________________________
231//
232void QTAACFile::SetClientFormat(const CAStreamBasicDescription &dataFormat)
233{
234 CloseConverter();
235
236 XThrowIf(!dataFormat.IsPCM(), -1, "non-PCM client format on audio file");
237 mClientDataFormat = dataFormat;
238
239 if (mClientDataFormat != mFileDataFormat) {
240 // We need an AudioConverter.
241 // file -> client
242 XThrowIfError(AudioConverterNew(&mFileDataFormat, &mClientDataFormat, &mConverter),
243 "create audio converter");
244
245 // set the magic cookie, if any (for decode)
246 if (mMagicCookie)
247 SetConverterProperty(kAudioConverterDecompressionMagicCookie,
248 mMagicCookieSize, mMagicCookie, true);
249 }
250 UpdateClientMaxPacketSize();
251}
252
253// _______________________________________________________________________________________
254//
255OSStatus QTAACFile::SetConverterProperty(AudioConverterPropertyID inPropertyID,
256 UInt32 inPropertyDataSize,
257 const void* inPropertyData,
258 bool inCanFail)
259{
260 OSStatus err = AudioConverterSetProperty(mConverter, inPropertyID, inPropertyDataSize, inPropertyData);
261 if (!inCanFail) {
262 XThrowIfError(err, "set audio converter property");
263 }
264 UpdateClientMaxPacketSize();
265 return err;
266}
267
268// _______________________________________________________________________________________
269//
270void QTAACFile::UpdateClientMaxPacketSize()
271{
272 if (mConverter != NULL) {
273 AudioConverterPropertyID property =
274 kAudioConverterPropertyMaximumOutputPacketSize;
275
276 UInt32 propertySize = sizeof(UInt32);
277 XThrowIfError(AudioConverterGetProperty(mConverter, property, &propertySize, &mClientMaxPacketSize),
278 "get audio converter's maximum packet size");
279 } else
280 mClientMaxPacketSize = mFileMaxPacketSize;
281 XThrowIf(mClientMaxPacketSize == 0, -1, "client maximum packet size is 0");
282}
283
284// _______________________________________________________________________________________
285//
286// This function reads the next packet from the movie. It always reads only 1 packet,
287// regardless of how many were asked for -- whether it's called either as a callback from
288// AudioConverterFillComplexBuffer, or directly from ReadPackets (below), the caller
289// will just call again if it wants more.
290OSStatus QTAACFile::ReadInputProc( AudioConverterRef inAudioConverter,
291 UInt32* ioNumberDataPackets,
292 AudioBufferList* ioData,
293 AudioStreamPacketDescription** outDataPacketDescription,
294 void* inUserData)
295{
296 QTAACFile *This = static_cast<QTAACFile *>(inUserData);
297
298 UInt32 remainingPacketsInFile = This->mNumberPackets - This->mPacketMark;
299 if (remainingPacketsInFile == 0) {
300 // end of file
301 *ioNumberDataPackets = 0;
302 ioData->mBuffers[0].mDataByteSize = 0;
303 return noErr; // EOF is signified by 0 packets/0 bytes/noErr
304 }
305
306 UInt32 ioPackets = 1;
307
308 // don't try to read past EOF
309 if (ioPackets > remainingPacketsInFile)
310 ioPackets = remainingPacketsInFile;
311
312 long bytesRead;
313 OSStatus err;
314
315 HUnlock(This->mPacketHandle);
316 err = GetMediaSample(This->mMedia, // specifies the media for this operation
317 This->mPacketHandle, // function returns the sample data into this handle
318 0, // maximum number of bytes of sample data to be returned
319 &bytesRead, // the number of bytes of sample data returned
320 This->mPacketTimes[This->mPacketMark],
321 // starting time of the sample to be retrieved (must be in Media's TimeScale)
322 NULL, // indicates the actual time of the returned sample data
323 NULL, // duration of each sample in the media
324 NULL, // sample description corresponding to the returned sample data (NULL to ignore)
325 NULL, // index value to the sample description that corresponds to the returned sample data (NULL to ignore)
326 0, // maximum number of samples to be returned (0 to use a value that is appropriate for the media)
327 NULL, // number of samples it actually returned
328 NULL); // flags that describe the sample (NULL to ignore)
329
330 if (err)
331 return err;
332
333 if (outDataPacketDescription) {
334 This->mPacketDescription.mStartOffset = 0;
335 This->mPacketDescription.mVariableFramesInPacket = 0;
336 This->mPacketDescription.mDataByteSize = bytesRead;
337 *outDataPacketDescription = &This->mPacketDescription;
338 }
339
340 HLock(This->mPacketHandle);
341 ioData->mBuffers[0].mDataByteSize = bytesRead;
342 ioData->mBuffers[0].mData = *This->mPacketHandle;
343 This->mPacketMark += ioPackets;
344 *ioNumberDataPackets = ioPackets;
345 return noErr;
346}
347
348// _______________________________________________________________________________________
349//
350void QTAACFile::ReadPackets(UInt32 &ioNumPackets, AudioBufferList *ioData)
351{
352 // older versions of AudioConverterFillComplexBuffer don't do this, so do our own sanity check
353 UInt32 maxNumPackets = ioData->mBuffers[0].mDataByteSize / mClientMaxPacketSize;
354 if (ioNumPackets > maxNumPackets)
355 ioNumPackets = maxNumPackets;
356
357 if (mConverter == NULL)
358 XThrowIfError(ReadInputProc(NULL, &ioNumPackets, ioData, NULL, this), "read audio file");
359 else {
360 XThrowIfError(AudioConverterFillComplexBuffer(mConverter, ReadInputProc, this, &ioNumPackets, ioData, NULL),
361 "qt convert audio packets");
362 }
363}