2 * Code to create QuickTime Movies with Blender
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is written by Rob Haarsma (phase)
22 * Contributor(s): Stefan Gartner (sgefant)
23 * Damien Plisson 11/2009
25 * ***** END GPL LICENSE BLOCK *****
29 #if defined(_WIN32) || defined(__APPLE__)
34 #include "DNA_scene_types.h"
35 #include "DNA_userdef_types.h"
38 # include "AUD_C-API.h"
41 #include "BKE_global.h"
43 #include "BKE_scene.h"
44 #include "BKE_report.h"
46 #include "BLI_blenlib.h"
48 #include "BLO_sys_types.h"
50 #include "IMB_imbuf.h"
51 #include "IMB_imbuf_types.h"
53 #include "MEM_guardedalloc.h"
60 #import <Cocoa/Cocoa.h>
61 #import <QTKit/QTKit.h>
62 #include <AudioToolbox/AudioToolbox.h>
64 #if (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4) || !__LP64__
65 #error 64 bit build & OSX 10.5 minimum are needed for QTKit
68 #include "quicktime_import.h"
69 #include "quicktime_export.h"
71 #endif /* __APPLE__ */
73 typedef struct QuicktimeExport {
79 NSDictionary *frameAttributes;
81 NSString *videoTempFileName;
83 AUD_Device *audioInputDevice;
84 AudioFileID audioFile;
85 NSString *audioFileName;
86 AudioConverterRef audioConverter;
87 AudioBufferList audioBufferList;
88 AudioStreamBasicDescription audioInputFormat, audioOutputFormat;
89 AudioStreamPacketDescription *audioOutputPktDesc;
91 char *audioInputBuffer;
92 char *audioOutputBuffer;
93 UInt32 audioCodecMaxOutputPacketSize;
94 UInt64 audioTotalExportedFrames, audioTotalSavedFrames;
95 UInt64 audioLastFrame;
96 SInt64 audioOutputPktPos;
100 static struct QuicktimeExport *qtexport;
102 #define AUDIOOUTPUTBUFFERSIZE 65536
104 #pragma mark rna helper functions
107 static QuicktimeCodecTypeDesc qtVideoCodecList[] = {
108 {kRawCodecType, 1, "Uncompressed"},
109 {k422YpCbCr8CodecType, 2, "Uncompressed 8-bit 4:2:2"},
110 {k422YpCbCr10CodecType, 3, "Uncompressed 10-bit 4:2:2"},
111 {kComponentVideoCodecType, 4, "Component Video"},
112 {kPixletCodecType, 5, "Pixlet"},
113 {kPNGCodecType, 6, "PNG"},
114 {kJPEGCodecType, 7, "JPEG"},
115 {kMotionJPEGACodecType, 8, "M-JPEG A"},
116 {kMotionJPEGBCodecType, 9, "M-JPEG B"},
117 {kDVCPALCodecType, 10, "DV PAL"},
118 {kDVCNTSCCodecType, 11, "DV/DVCPRO NTSC"},
119 {kDVCPROHD720pCodecType, 12, "DVCPRO HD 720p"},
120 {kDVCPROHD1080i50CodecType, 13, "DVCPRO HD 1080i50"},
121 {kDVCPROHD1080i60CodecType, 14, "DVCPRO HD 1080i60"},
122 {kMPEG4VisualCodecType, 15, "MPEG4"},
123 {kH263CodecType, 16, "H.263"},
124 {kH264CodecType, 17, "H.264"},
125 {kAnimationCodecType, 18, "Animation"},
128 static int qtVideoCodecCount = 18;
130 int quicktime_get_num_videocodecs()
132 return qtVideoCodecCount;
135 QuicktimeCodecTypeDesc* quicktime_get_videocodecType_desc(int indexValue)
137 if ((indexValue>=0) && (indexValue < qtVideoCodecCount))
138 return &qtVideoCodecList[indexValue];
143 int quicktime_rnatmpvalue_from_videocodectype(int codecType)
146 for (i = 0; i < qtVideoCodecCount; i++) {
147 if (qtVideoCodecList[i].codecType == codecType)
148 return qtVideoCodecList[i].rnatmpvalue;
154 int quicktime_videocodecType_from_rnatmpvalue(int rnatmpvalue)
157 for (i = 0; i < qtVideoCodecCount; i++) {
158 if (qtVideoCodecList[i].rnatmpvalue == rnatmpvalue)
159 return qtVideoCodecList[i].codecType;
166 static QuicktimeCodecTypeDesc qtAudioCodecList[] = {
168 {kAudioFormatLinearPCM, 1, "LPCM"},
169 {kAudioFormatAppleLossless, 2, "Apple Lossless"},
170 {kAudioFormatMPEG4AAC, 3, "AAC"},
173 static int qtAudioCodecCount = 4;
175 int quicktime_get_num_audiocodecs()
177 return qtAudioCodecCount;
180 QuicktimeCodecTypeDesc* quicktime_get_audiocodecType_desc(int indexValue)
182 if ((indexValue>=0) && (indexValue < qtAudioCodecCount))
183 return &qtAudioCodecList[indexValue];
188 int quicktime_rnatmpvalue_from_audiocodectype(int codecType)
191 for (i = 0; i < qtAudioCodecCount; i++) {
192 if (qtAudioCodecList[i].codecType == codecType)
193 return qtAudioCodecList[i].rnatmpvalue;
199 int quicktime_audiocodecType_from_rnatmpvalue(int rnatmpvalue)
202 for (i = 0; i < qtAudioCodecCount; i++) {
203 if (qtAudioCodecList[i].rnatmpvalue == rnatmpvalue)
204 return qtAudioCodecList[i].codecType;
211 static NSString *stringWithCodecType(int codecType)
215 *((int *)str) = EndianU32_NtoB(codecType);
218 return [NSString stringWithCString:str encoding:NSASCIIStringEncoding];
221 void makeqtstring (RenderData *rd, char *string)
225 strcpy(string, rd->pic);
226 BLI_path_abs(string, G.main->name);
228 BLI_make_existing_file(string);
230 if (BLI_strcasecmp(string + strlen(string) - 4, ".mov")) {
231 sprintf(txt, "%04d-%04d.mov", (rd->sfra) , (rd->efra) );
236 void filepath_qt(char *string, RenderData *rd)
238 if (string == NULL) return;
240 strcpy(string, rd->pic);
241 BLI_path_abs(string, G.main->name);
243 BLI_make_existing_file(string);
245 if (!BLI_testextensie(string, ".mov")) {
246 /* if we don't have any #'s to insert numbers into, use 4 numbers by default */
247 if (strchr(string, '#')==NULL)
248 strcat(string, "####"); /* 4 numbers */
250 BLI_path_frame_range(string, rd->sfra, rd->efra, 4);
251 strcat(string, ".mov");
256 #pragma mark audio export functions
258 static OSStatus write_cookie(AudioConverterRef converter, AudioFileID outfile)
260 // grab the cookie from the converter and write it to the file
261 UInt32 cookieSize = 0;
262 OSStatus err = AudioConverterGetPropertyInfo(converter, kAudioConverterCompressionMagicCookie, &cookieSize, NULL);
263 // if there is an error here, then the format doesn't have a cookie, so on we go
264 if (!err && cookieSize) {
265 char* cookie = malloc(cookieSize);
267 err = AudioConverterGetProperty(converter, kAudioConverterCompressionMagicCookie, &cookieSize, cookie);
270 err = AudioFileSetProperty (outfile, kAudioFilePropertyMagicCookieData, cookieSize, cookie);
271 // even though some formats have cookies, some files don't take them
278 /* AudioConverter input stream callback */
279 static OSStatus AudioConverterInputCallback(AudioConverterRef inAudioConverter,
280 UInt32* ioNumberDataPackets,
281 AudioBufferList* ioData,
282 AudioStreamPacketDescription** outDataPacketDescription,
285 if (qtexport->audioTotalExportedFrames >= qtexport->audioLastFrame) { /* EOF */
286 *ioNumberDataPackets = 0;
290 if (qtexport->audioInputFormat.mBytesPerPacket * *ioNumberDataPackets > AUDIOOUTPUTBUFFERSIZE)
291 *ioNumberDataPackets = AUDIOOUTPUTBUFFERSIZE / qtexport->audioInputFormat.mBytesPerPacket;
293 if ((qtexport->audioTotalExportedFrames + *ioNumberDataPackets) > qtexport->audioLastFrame)
294 *ioNumberDataPackets = (qtexport->audioLastFrame - qtexport->audioTotalExportedFrames) / qtexport->audioInputFormat.mFramesPerPacket;
296 qtexport->audioTotalExportedFrames += *ioNumberDataPackets;
298 AUD_readDevice(qtexport->audioInputDevice, (UInt8*)qtexport->audioInputBuffer,
299 qtexport->audioInputFormat.mFramesPerPacket * *ioNumberDataPackets);
301 ioData->mBuffers[0].mDataByteSize = qtexport->audioInputFormat.mBytesPerPacket * *ioNumberDataPackets;
302 ioData->mBuffers[0].mData = qtexport->audioInputBuffer;
303 ioData->mBuffers[0].mNumberChannels = qtexport->audioInputFormat.mChannelsPerFrame;
309 #pragma mark export functions
311 int start_qt(struct Scene *scene, struct RenderData *rd, int rectx, int recty, ReportList *reports)
313 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
317 OSStatus err = noErr;
319 if(qtexport == NULL) qtexport = MEM_callocN(sizeof(QuicktimeExport), "QuicktimeExport");
321 [QTMovie enterQTKitOnThread];
323 /* Check first if the QuickTime 7.2.1 initToWritableFile: method is available */
324 if ([[[[QTMovie alloc] init] autorelease] respondsToSelector:@selector(initToWritableFile:error:)] != YES) {
325 BKE_report(reports, RPT_ERROR, "\nUnable to create quicktime movie, need Quicktime rev 7.2.1 or later");
329 makeqtstring(rd, name);
330 qtexport->filename = [[NSString alloc] initWithCString:name
331 encoding:[NSString defaultCStringEncoding]];
332 qtexport->movie = nil;
333 qtexport->audioFile = NULL;
335 if (rd->qtcodecsettings.audiocodecType) {
336 // generate a name for our video & audio files
337 /* Init audio file */
338 CFURLRef outputFileURL;
340 AudioFileTypeID audioFileType;
342 switch (rd->qtcodecsettings.audiocodecType) {
343 case kAudioFormatLinearPCM:
344 audioFileType = kAudioFileWAVEType;
345 strcpy(extension,".wav");
347 case kAudioFormatMPEG4AAC:
348 case kAudioFormatAppleLossless:
349 audioFileType = kAudioFileM4AType;
350 strcpy(extension, ".m4a");
353 audioFileType = kAudioFileAIFFType;
354 strcpy(extension,".aiff");
359 strcat(name, extension);
360 outputFileURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,(UInt8*) name, strlen(name), false);
364 qtexport->audioFileName = [[NSString alloc] initWithCString:name
365 encoding:[NSString defaultCStringEncoding]];
367 qtexport->audioInputFormat.mSampleRate = U.audiorate;
368 qtexport->audioInputFormat.mFormatID = kAudioFormatLinearPCM;
369 qtexport->audioInputFormat.mChannelsPerFrame = U.audiochannels;
370 switch (U.audioformat) {
372 qtexport->audioInputFormat.mBitsPerChannel = 8;
373 qtexport->audioInputFormat.mFormatFlags = 0;
376 qtexport->audioInputFormat.mBitsPerChannel = 24;
377 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
380 qtexport->audioInputFormat.mBitsPerChannel = 32;
381 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
383 case AUD_FORMAT_FLOAT32:
384 qtexport->audioInputFormat.mBitsPerChannel = 32;
385 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
387 case AUD_FORMAT_FLOAT64:
388 qtexport->audioInputFormat.mBitsPerChannel = 64;
389 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
393 qtexport->audioInputFormat.mBitsPerChannel = 16;
394 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
397 qtexport->audioInputFormat.mBytesPerFrame = qtexport->audioInputFormat.mChannelsPerFrame * qtexport->audioInputFormat.mBitsPerChannel / 8;
398 qtexport->audioInputFormat.mFramesPerPacket = 1; /*If not ==1, then need to check input callback for "rounding" issues"*/
399 qtexport->audioInputFormat.mBytesPerPacket = qtexport->audioInputFormat.mBytesPerFrame;
400 qtexport->audioInputFormat.mFormatFlags |= kLinearPCMFormatFlagIsPacked;
404 qtexport->audioOutputFormat.mFormatID = rd->qtcodecsettings.audiocodecType;
405 //TODO: set audio channels
406 qtexport->audioOutputFormat.mChannelsPerFrame = 2;
407 qtexport->audioOutputFormat.mSampleRate = rd->qtcodecsettings.audioSampleRate;
409 /* Default value for compressed formats, overridden after if not the case */
410 qtexport->audioOutputFormat.mFramesPerPacket = 0;
411 qtexport->audioOutputFormat.mBytesPerFrame = 0;
412 qtexport->audioOutputFormat.mBytesPerPacket = 0;
413 qtexport->audioOutputFormat.mBitsPerChannel = 0;
415 switch (rd->qtcodecsettings.audiocodecType) {
416 case kAudioFormatMPEG4AAC:
417 qtexport->audioOutputFormat.mFormatFlags = kMPEG4Object_AAC_Main;
418 /* AAC codec does not handle sample rates above 48kHz, force this limit instead of getting an error afterwards */
419 if (qtexport->audioOutputFormat.mSampleRate > 48000) qtexport->audioOutputFormat.mSampleRate = 48000;
421 case kAudioFormatAppleLossless:
422 switch (U.audioformat) {
424 qtexport->audioOutputFormat.mFormatFlags = kAppleLosslessFormatFlag_16BitSourceData;
427 qtexport->audioOutputFormat.mFormatFlags = kAppleLosslessFormatFlag_24BitSourceData;
430 qtexport->audioOutputFormat.mFormatFlags = kAppleLosslessFormatFlag_32BitSourceData;
433 case AUD_FORMAT_FLOAT32:
434 case AUD_FORMAT_FLOAT64:
439 case kAudioFormatLinearPCM:
441 switch (rd->qtcodecsettings.audioBitDepth) {
443 qtexport->audioOutputFormat.mBitsPerChannel = 8;
444 qtexport->audioOutputFormat.mFormatFlags = 0;
447 qtexport->audioOutputFormat.mBitsPerChannel = 24;
448 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
451 qtexport->audioOutputFormat.mBitsPerChannel = 32;
452 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
454 case AUD_FORMAT_FLOAT32:
455 qtexport->audioOutputFormat.mBitsPerChannel = 32;
456 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
458 case AUD_FORMAT_FLOAT64:
459 qtexport->audioOutputFormat.mBitsPerChannel = 64;
460 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
464 qtexport->audioOutputFormat.mBitsPerChannel = 16;
465 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
468 qtexport->audioOutputFormat.mFormatFlags |= kLinearPCMFormatFlagIsPacked;
469 qtexport->audioOutputFormat.mBytesPerPacket = qtexport->audioOutputFormat.mChannelsPerFrame * (qtexport->audioOutputFormat.mBitsPerChannel / 8);
470 qtexport->audioOutputFormat.mFramesPerPacket = 1;
471 qtexport->audioOutputFormat.mBytesPerFrame = qtexport->audioOutputFormat.mBytesPerPacket;
475 err = AudioFileCreateWithURL(outputFileURL, audioFileType, &qtexport->audioOutputFormat, kAudioFileFlags_EraseFile, &qtexport->audioFile);
476 CFRelease(outputFileURL);
479 BKE_report(reports, RPT_ERROR, "\nQuicktime: unable to create temporary audio file. Format error ?");
481 err = AudioConverterNew(&qtexport->audioInputFormat, &qtexport->audioOutputFormat, &qtexport->audioConverter);
483 BKE_report(reports, RPT_ERROR, "\nQuicktime: unable to initialize audio codec converter. Format error ?");
484 AudioFileClose(qtexport->audioFile);
485 qtexport->audioFile = NULL;
486 [qtexport->audioFileName release];
487 qtexport->audioFileName = nil;
490 UInt32 prop,propSize;
491 /* Set up codec properties */
492 if (rd->qtcodecsettings.audiocodecType == kAudioFormatMPEG4AAC) { /* Lossy compressed format */
493 prop = rd->qtcodecsettings.audioBitRate;
494 AudioConverterSetProperty(qtexport->audioConverter, kAudioConverterEncodeBitRate,
495 sizeof(prop), &prop);
497 if (rd->qtcodecsettings.audioCodecFlags & QTAUDIO_FLAG_CODEC_ISCBR)
498 prop = kAudioCodecBitRateControlMode_Constant;
500 prop = kAudioCodecBitRateControlMode_LongTermAverage;
501 AudioConverterSetProperty(qtexport->audioConverter, kAudioCodecPropertyBitRateControlMode,
502 sizeof(prop), &prop);
504 /* Conversion quality : if performance impact then offer degraded option */
505 if ((rd->qtcodecsettings.audioCodecFlags & QTAUDIO_FLAG_RESAMPLE_NOHQ) == 0) {
506 prop = kAudioConverterSampleRateConverterComplexity_Mastering;
507 AudioConverterSetProperty(qtexport->audioConverter, kAudioConverterSampleRateConverterComplexity,
508 sizeof(prop), &prop);
510 prop = kAudioConverterQuality_Max;
511 AudioConverterSetProperty(qtexport->audioConverter, kAudioConverterSampleRateConverterQuality,
512 sizeof(prop), &prop);
515 write_cookie(qtexport->audioConverter, qtexport->audioFile);
517 /* Allocate output buffer */
518 if (qtexport->audioOutputFormat.mBytesPerPacket ==0) /* VBR */
519 AudioConverterGetProperty(qtexport->audioConverter, kAudioConverterPropertyMaximumOutputPacketSize,
520 &propSize, &qtexport->audioCodecMaxOutputPacketSize);
522 qtexport->audioCodecMaxOutputPacketSize = qtexport->audioOutputFormat.mBytesPerPacket;
524 qtexport->audioInputBuffer = MEM_mallocN(AUDIOOUTPUTBUFFERSIZE, "qt_audio_inputPacket");
525 qtexport->audioOutputBuffer = MEM_mallocN(AUDIOOUTPUTBUFFERSIZE, "qt_audio_outputPacket");
526 qtexport->audioOutputPktDesc = MEM_mallocN(sizeof(AudioStreamPacketDescription) * AUDIOOUTPUTBUFFERSIZE / qtexport->audioCodecMaxOutputPacketSize,
533 qtexport->videoTempFileName = [[NSString alloc] initWithCString:tmpnam(nil)
534 encoding:[NSString defaultCStringEncoding]];
535 if (qtexport->videoTempFileName) {
536 qtexport->movie = [[QTMovie alloc] initToWritableFile:qtexport->videoTempFileName error:&error];
542 qtexport->movie = [[QTMovie alloc] initToWritableFile:qtexport->filename error:&error];
544 if(qtexport->movie == nil) {
545 BKE_report(reports, RPT_ERROR, "Unable to create quicktime movie.");
547 if (qtexport->filename) [qtexport->filename release];
548 qtexport->filename = nil;
549 if (qtexport->audioFileName) [qtexport->audioFileName release];
550 qtexport->audioFileName = nil;
551 if (qtexport->videoTempFileName) [qtexport->videoTempFileName release];
552 qtexport->videoTempFileName = nil;
553 [QTMovie exitQTKitOnThread];
556 [qtexport->movie retain];
557 [qtexport->movie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
558 [qtexport->movie setAttribute:@"Made with Blender" forKey:QTMovieCopyrightAttribute];
560 qtexport->frameDuration = QTMakeTime(rd->frs_sec_base*1000, rd->frs_sec*1000);
562 /* specifying the codec attributes : try to retrieve them from render data first*/
563 if (rd->qtcodecsettings.codecType) {
564 qtexport->frameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
565 stringWithCodecType(rd->qtcodecsettings.codecType),
567 [NSNumber numberWithLong:((rd->qtcodecsettings.codecSpatialQuality)*codecLosslessQuality)/100],
568 QTAddImageCodecQuality,
572 qtexport->frameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:@"jpeg",
574 [NSNumber numberWithLong:codecHighQuality],
575 QTAddImageCodecQuality,
578 [qtexport->frameAttributes retain];
580 if (qtexport->audioFile) {
581 /* Init audio input stream */
582 AUD_DeviceSpecs specs;
584 specs.channels = U.audiochannels;
585 specs.format = U.audioformat;
586 specs.rate = U.audiorate;
587 qtexport->audioInputDevice = AUD_openReadDevice(specs);
588 AUD_playDevice(qtexport->audioInputDevice, scene->sound_scene, rd->sfra * rd->frs_sec_base / rd->frs_sec);
590 qtexport->audioOutputPktPos = 0;
591 qtexport->audioTotalExportedFrames = 0;
592 qtexport->audioTotalSavedFrames = 0;
594 qtexport->audioLastFrame = (rd->efra - rd->sfra) * qtexport->audioInputFormat.mSampleRate * rd->frs_sec_base / rd->frs_sec;
604 int append_qt(struct RenderData *rd, int start_frame, int frame, int *pixels, int rectx, int recty, ReportList *reports)
606 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
607 NSBitmapImageRep *blBitmapFormatImage;
609 OSStatus err = noErr;
610 unsigned char *from_Ptr,*to_Ptr;
613 /* Create bitmap image rep in blender format (32bit RGBA) */
614 blBitmapFormatImage = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
617 bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
618 colorSpaceName:NSCalibratedRGBColorSpace
619 bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
622 if (!blBitmapFormatImage) {
627 from_Ptr = (unsigned char *)pixels;
628 to_Ptr = (unsigned char *)[blBitmapFormatImage bitmapData];
629 for (y = 0; y < recty; y++) {
630 to_i = (recty-y-1)*rectx;
632 memcpy(to_Ptr+4*to_i, from_Ptr+4*from_i, 4*rectx);
635 frameImage = [[NSImage alloc] initWithSize:NSMakeSize(rectx, recty)];
636 [frameImage addRepresentation:blBitmapFormatImage];
638 /* Add the image to the movie clip */
639 [qtexport->movie addImage:frameImage
640 forDuration:qtexport->frameDuration
641 withAttributes:qtexport->frameAttributes];
643 [blBitmapFormatImage release];
644 [frameImage release];
647 if (qtexport->audioFile) {
648 UInt32 audioPacketsConverted;
650 while (qtexport->audioTotalExportedFrames < qtexport->audioLastFrame) {
652 qtexport->audioBufferList.mNumberBuffers = 1;
653 qtexport->audioBufferList.mBuffers[0].mNumberChannels = qtexport->audioOutputFormat.mChannelsPerFrame;
654 qtexport->audioBufferList.mBuffers[0].mDataByteSize = AUDIOOUTPUTBUFFERSIZE;
655 qtexport->audioBufferList.mBuffers[0].mData = qtexport->audioOutputBuffer;
656 audioPacketsConverted = AUDIOOUTPUTBUFFERSIZE / qtexport->audioCodecMaxOutputPacketSize;
658 err = AudioConverterFillComplexBuffer(qtexport->audioConverter, AudioConverterInputCallback,
659 NULL, &audioPacketsConverted, &qtexport->audioBufferList, qtexport->audioOutputPktDesc);
660 if (audioPacketsConverted) {
661 AudioFileWritePackets(qtexport->audioFile, false, qtexport->audioBufferList.mBuffers[0].mDataByteSize,
662 qtexport->audioOutputPktDesc, qtexport->audioOutputPktPos, &audioPacketsConverted, qtexport->audioOutputBuffer);
663 qtexport->audioOutputPktPos += audioPacketsConverted;
665 if (qtexport->audioOutputFormat.mFramesPerPacket) {
666 // this is the common case: format has constant frames per packet
667 qtexport->audioTotalSavedFrames += (audioPacketsConverted * qtexport->audioOutputFormat.mFramesPerPacket);
671 // if there are variable frames per packet, then we have to do this for each packeet
672 for (i = 0; i < audioPacketsConverted; ++i)
673 qtexport->audioTotalSavedFrames += qtexport->audioOutputPktDesc[i].mVariableFramesInPacket;
679 //Error getting audio packets
680 BKE_reportf(reports, RPT_ERROR, "Unable to get further audio packets from frame %i, error = 0x%x",(int)qtexport->audioTotalExportedFrames,err);
694 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
695 if (qtexport->movie) {
697 if (qtexport->audioFile)
699 NSDictionary *dict = nil;
700 QTMovie *audioTmpMovie = nil;
702 NSFileManager *fileManager;
704 /* Mux video and audio then save file */
706 /* Write last frames for VBR files */
707 if (qtexport->audioOutputFormat.mBitsPerChannel == 0) {
708 OSStatus err = noErr;
709 AudioConverterPrimeInfo primeInfo;
710 UInt32 primeSize = sizeof(primeInfo);
712 err = AudioConverterGetProperty(qtexport->audioConverter, kAudioConverterPrimeInfo, &primeSize, &primeInfo);
714 // there's priming to write out to the file
715 AudioFilePacketTableInfo pti;
716 pti.mPrimingFrames = primeInfo.leadingFrames;
717 pti.mRemainderFrames = primeInfo.trailingFrames;
718 pti.mNumberValidFrames = qtexport->audioTotalSavedFrames - pti.mPrimingFrames - pti.mRemainderFrames;
719 AudioFileSetProperty(qtexport->audioFile, kAudioFilePropertyPacketTableInfo, sizeof(pti), &pti);
724 write_cookie(qtexport->audioConverter, qtexport->audioFile);
725 AudioConverterDispose(qtexport->audioConverter);
726 AudioFileClose(qtexport->audioFile);
727 AUD_closeReadDevice(qtexport->audioInputDevice);
728 qtexport->audioFile = NULL;
729 qtexport->audioInputDevice = NULL;
730 MEM_freeN(qtexport->audioInputBuffer);
731 MEM_freeN(qtexport->audioOutputBuffer);
732 MEM_freeN(qtexport->audioOutputPktDesc);
734 /* Reopen audio file and merge it */
735 audioTmpMovie = [QTMovie movieWithFile:qtexport->audioFileName error:&error];
737 NSArray *audioTracks = [audioTmpMovie tracksOfMediaType:QTMediaTypeSound];
738 QTTrack *audioTrack = nil;
739 if( [audioTracks count] > 0 )
741 audioTrack = [audioTracks objectAtIndex:0];
746 QTTimeRange totalRange;
747 totalRange.time = QTZeroTime;
748 totalRange.duration = [[audioTmpMovie attributeForKey:QTMovieDurationAttribute] QTTimeValue];
750 [qtexport->movie insertSegmentOfTrack:audioTrack timeRange:totalRange atTime:QTZeroTime];
755 dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
756 forKey:QTMovieFlatten];
759 [qtexport->movie writeToFile:qtexport->filename withAttributes:dict];
762 /* Delete temp files */
763 fileManager = [[NSFileManager alloc] init];
764 [fileManager removeItemAtPath:qtexport->audioFileName error:&error];
765 [fileManager removeItemAtPath:qtexport->videoTempFileName error:&error];
766 [fileManager release];
769 /* Flush update of the movie file */
770 [qtexport->movie updateMovieFile];
772 [qtexport->movie invalidate];
775 /* Clean up movie structure */
776 if (qtexport->filename) [qtexport->filename release];
777 qtexport->filename = nil;
778 if (qtexport->audioFileName) [qtexport->audioFileName release];
779 qtexport->audioFileName = nil;
780 if (qtexport->videoTempFileName) [qtexport->videoTempFileName release];
781 qtexport->videoTempFileName = nil;
782 [qtexport->frameAttributes release];
783 [qtexport->movie release];
786 [QTMovie exitQTKitOnThread];
796 void free_qtcomponentdata(void)
800 void quicktime_verify_image_type(RenderData *rd, ImageFormatData *imf)
802 if (imf->imtype == R_IMF_IMTYPE_QUICKTIME) {
803 if ((rd->qtcodecsettings.codecType<= 0) ||
804 (rd->qtcodecsettings.codecSpatialQuality <0) ||
805 (rd->qtcodecsettings.codecSpatialQuality > 100)) {
807 rd->qtcodecsettings.codecType = kJPEGCodecType;
808 rd->qtcodecsettings.codecSpatialQuality = (codecHighQuality*100)/codecLosslessQuality;
810 if ((rd->qtcodecsettings.audioSampleRate < 21000) ||
811 (rd->qtcodecsettings.audioSampleRate > 193000))
812 rd->qtcodecsettings.audioSampleRate = 48000;
814 if (rd->qtcodecsettings.audioBitDepth == 0)
815 rd->qtcodecsettings.audioBitDepth = AUD_FORMAT_S16;
817 if (rd->qtcodecsettings.audioBitRate == 0)
818 rd->qtcodecsettings.audioBitRate = 256000;
822 #endif /* _WIN32 || __APPLE__ */
823 #endif /* WITH_QUICKTIME */