svn merge ^/trunk/blender -r49410:49412
[blender.git] / source / blender / quicktime / apple / qtkit_export.m
1 /*
2  * Code to create QuickTime Movies with Blender
3  *
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.
9  *
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.
14  *
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.
18  *
19  *
20  * The Original Code is written by Rob Haarsma (phase)
21  *
22  * Contributor(s): Stefan Gartner (sgefant)
23  *                                 Damien Plisson 11/2009
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #ifdef WITH_QUICKTIME
29 #if defined(_WIN32) || defined(__APPLE__)
30
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "DNA_scene_types.h"
35 #include "DNA_userdef_types.h"
36
37 #ifdef WITH_AUDASPACE
38 #  include "AUD_C-API.h"
39 #endif
40
41 #include "BKE_global.h"
42 #include "BKE_main.h"
43 #include "BKE_scene.h"
44 #include "BKE_report.h"
45
46 #include "BLI_blenlib.h"
47
48 #include "BLO_sys_types.h"
49
50 #include "IMB_imbuf.h"
51 #include "IMB_imbuf_types.h"
52
53 #include "MEM_guardedalloc.h"
54
55 #ifdef __APPLE__
56 /* evil */
57 #ifndef __AIFF__
58 #define __AIFF__
59 #endif
60 #import <Cocoa/Cocoa.h>
61 #import <QTKit/QTKit.h>
62 #include <AudioToolbox/AudioToolbox.h>
63
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
66 #endif
67
68 #include "quicktime_import.h"
69 #include "quicktime_export.h"
70
71 #endif /* __APPLE__ */
72
73 typedef struct QuicktimeExport {
74         QTMovie *movie;
75         
76         NSString *filename;
77
78         QTTime frameDuration;
79         NSDictionary *frameAttributes;
80         
81         NSString *videoTempFileName;
82         /* Audio section */
83         AUD_Device *audioInputDevice;
84         AudioFileID audioFile;
85         NSString *audioFileName;
86         AudioConverterRef audioConverter;
87         AudioBufferList audioBufferList;
88         AudioStreamBasicDescription audioInputFormat, audioOutputFormat;
89         AudioStreamPacketDescription *audioOutputPktDesc;
90         SInt64 audioFilePos;
91         char* audioInputBuffer;
92         char* audioOutputBuffer;
93         UInt32 audioCodecMaxOutputPacketSize;
94         UInt64 audioTotalExportedFrames, audioTotalSavedFrames;
95         UInt64 audioLastFrame;
96         SInt64 audioOutputPktPos;
97         
98 } QuicktimeExport;
99
100 static struct QuicktimeExport *qtexport;
101
102 #define AUDIOOUTPUTBUFFERSIZE 65536
103
104 #pragma mark rna helper functions
105
106 /* Video codec */
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"},
126         {0,0,NULL}};
127
128 static int qtVideoCodecCount = 18;
129
130 int quicktime_get_num_videocodecs()
131 {
132         return qtVideoCodecCount;
133 }
134
135 QuicktimeCodecTypeDesc* quicktime_get_videocodecType_desc(int indexValue)
136 {
137         if ((indexValue>=0) && (indexValue < qtVideoCodecCount))
138                 return &qtVideoCodecList[indexValue];
139         else
140                 return NULL;
141 }
142
143 int quicktime_rnatmpvalue_from_videocodectype(int codecType)
144 {
145         int i;
146         for (i=0;i<qtVideoCodecCount;i++) {
147                 if (qtVideoCodecList[i].codecType == codecType)
148                         return qtVideoCodecList[i].rnatmpvalue;
149         }
150
151         return 0;
152 }
153
154 int quicktime_videocodecType_from_rnatmpvalue(int rnatmpvalue)
155 {
156         int i;
157         for (i=0;i<qtVideoCodecCount;i++) {
158                 if (qtVideoCodecList[i].rnatmpvalue == rnatmpvalue)
159                         return qtVideoCodecList[i].codecType;
160         }
161         
162         return 0;       
163 }
164
165 /* Audio codec */
166 static QuicktimeCodecTypeDesc qtAudioCodecList[] = {
167         {0, 0, "No audio"},
168         {kAudioFormatLinearPCM, 1, "LPCM"},
169         {kAudioFormatAppleLossless, 2, "Apple Lossless"},
170         {kAudioFormatMPEG4AAC, 3, "AAC"},
171         {0,0,NULL}};
172
173 static int qtAudioCodecCount = 4;
174
175 int quicktime_get_num_audiocodecs()
176 {
177         return qtAudioCodecCount;
178 }
179
180 QuicktimeCodecTypeDesc* quicktime_get_audiocodecType_desc(int indexValue)
181 {
182         if ((indexValue>=0) && (indexValue < qtAudioCodecCount))
183                 return &qtAudioCodecList[indexValue];
184         else
185                 return NULL;
186 }
187
188 int quicktime_rnatmpvalue_from_audiocodectype(int codecType)
189 {
190         int i;
191         for (i=0;i<qtAudioCodecCount;i++) {
192                 if (qtAudioCodecList[i].codecType == codecType)
193                         return qtAudioCodecList[i].rnatmpvalue;
194         }
195         
196         return 0;
197 }
198
199 int quicktime_audiocodecType_from_rnatmpvalue(int rnatmpvalue)
200 {
201         int i;
202         for (i=0;i<qtAudioCodecCount;i++) {
203                 if (qtAudioCodecList[i].rnatmpvalue == rnatmpvalue)
204                         return qtAudioCodecList[i].codecType;
205         }
206         
207         return 0;       
208 }
209
210
211 static NSString *stringWithCodecType(int codecType)
212 {
213         char str[5];
214         
215         *((int*)str) = EndianU32_NtoB(codecType);
216         str[4] = 0;
217         
218         return [NSString stringWithCString:str encoding:NSASCIIStringEncoding];
219 }
220
221 void makeqtstring (RenderData *rd, char *string)
222 {
223         char txt[64];
224
225         strcpy(string, rd->pic);
226         BLI_path_abs(string, G.main->name);
227
228         BLI_make_existing_file(string);
229
230         if (BLI_strcasecmp(string + strlen(string) - 4, ".mov")) {
231                 sprintf(txt, "%04d-%04d.mov", (rd->sfra) , (rd->efra) );
232                 strcat(string, txt);
233         }
234 }
235
236 void filepath_qt(char *string, RenderData *rd)
237 {
238         if (string==NULL) return;
239         
240         strcpy(string, rd->pic);
241         BLI_path_abs(string, G.main->name);
242         
243         BLI_make_existing_file(string);
244         
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 */
249
250                 BLI_path_frame_range(string, rd->sfra, rd->efra, 4);
251                 strcat(string, ".mov");
252         }
253 }
254
255
256 #pragma mark audio export functions
257
258 static OSStatus write_cookie(AudioConverterRef converter, AudioFileID outfile)
259 {
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);
266                 
267                 err = AudioConverterGetProperty(converter, kAudioConverterCompressionMagicCookie, &cookieSize, cookie);
268                 
269                 if (!err)
270                         err = AudioFileSetProperty (outfile, kAudioFilePropertyMagicCookieData, cookieSize, cookie);
271                         // even though some formats have cookies, some files don't take them
272                 
273                 free(cookie);
274         }
275         return err;
276 }
277
278 /* AudioConverter input stream callback */
279 static OSStatus AudioConverterInputCallback(AudioConverterRef inAudioConverter, 
280                                                  UInt32* ioNumberDataPackets,
281                                                  AudioBufferList* ioData,
282                                                  AudioStreamPacketDescription** outDataPacketDescription,
283                                                  void* inUserData)
284 {       
285         if (qtexport->audioTotalExportedFrames >= qtexport->audioLastFrame) { /* EOF */
286                 *ioNumberDataPackets = 0;
287                 return noErr;
288         }
289
290         if (qtexport->audioInputFormat.mBytesPerPacket * *ioNumberDataPackets > AUDIOOUTPUTBUFFERSIZE)
291                 *ioNumberDataPackets = AUDIOOUTPUTBUFFERSIZE / qtexport->audioInputFormat.mBytesPerPacket;
292         
293         if ((qtexport->audioTotalExportedFrames + *ioNumberDataPackets) > qtexport->audioLastFrame)
294                 *ioNumberDataPackets = (qtexport->audioLastFrame - qtexport->audioTotalExportedFrames) / qtexport->audioInputFormat.mFramesPerPacket;
295         
296         qtexport->audioTotalExportedFrames += *ioNumberDataPackets;
297         
298         AUD_readDevice(qtexport->audioInputDevice, (UInt8*)qtexport->audioInputBuffer, 
299                                    qtexport->audioInputFormat.mFramesPerPacket * *ioNumberDataPackets);
300         
301         ioData->mBuffers[0].mDataByteSize = qtexport->audioInputFormat.mBytesPerPacket * *ioNumberDataPackets;
302         ioData->mBuffers[0].mData = qtexport->audioInputBuffer;
303         ioData->mBuffers[0].mNumberChannels = qtexport->audioInputFormat.mChannelsPerFrame;
304         
305         return noErr;
306 }       
307
308
309 #pragma mark export functions
310
311 int start_qt(struct Scene *scene, struct RenderData *rd, int rectx, int recty, ReportList *reports)
312 {
313         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
314         NSError *error;
315         char name[1024];
316         int success= 1;
317         OSStatus err=noErr;
318
319         if(qtexport == NULL) qtexport = MEM_callocN(sizeof(QuicktimeExport), "QuicktimeExport");
320         
321         [QTMovie enterQTKitOnThread];           
322         
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");
326                 success= 0;
327         }
328         else {
329                 makeqtstring(rd, name);
330                 qtexport->filename = [[NSString alloc] initWithCString:name
331                                                                   encoding:[NSString defaultCStringEncoding]];
332                 qtexport->movie = nil;
333                 qtexport->audioFile = NULL;
334
335                 if (rd->qtcodecsettings.audiocodecType) {
336                         // generate a name for our video & audio files
337                         /* Init audio file */
338                         CFURLRef outputFileURL;
339                         char extension[32];
340                         AudioFileTypeID audioFileType;
341                         
342                         switch (rd->qtcodecsettings.audiocodecType) {
343                                 case kAudioFormatLinearPCM:
344                                         audioFileType = kAudioFileWAVEType;
345                                         strcpy(extension,".wav");
346                                         break;
347                                 case kAudioFormatMPEG4AAC:
348                                 case kAudioFormatAppleLossless:
349                                         audioFileType = kAudioFileM4AType;
350                                         strcpy(extension, ".m4a");
351                                         break;
352                                 default:
353                                         audioFileType = kAudioFileAIFFType;
354                                         strcpy(extension,".aiff");
355                                         break;
356                         }
357                                         
358                         tmpnam(name);
359                         strcat(name, extension);
360                         outputFileURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,(UInt8*) name, strlen(name), false);
361                         
362                         if (outputFileURL) {
363                                 
364                                 qtexport->audioFileName = [[NSString alloc] initWithCString:name
365                                                                                                                          encoding:[NSString defaultCStringEncoding]];
366                                 
367                                 qtexport->audioInputFormat.mSampleRate = U.audiorate;
368                                 qtexport->audioInputFormat.mFormatID = kAudioFormatLinearPCM;
369                                 qtexport->audioInputFormat.mChannelsPerFrame = U.audiochannels;
370                                 switch (U.audioformat) {
371                                         case AUD_FORMAT_U8:
372                                                 qtexport->audioInputFormat.mBitsPerChannel = 8;
373                                                 qtexport->audioInputFormat.mFormatFlags = 0;
374                                                 break;
375                                         case AUD_FORMAT_S24:
376                                                 qtexport->audioInputFormat.mBitsPerChannel = 24;
377                                                 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
378                                                 break;
379                                         case AUD_FORMAT_S32:
380                                                 qtexport->audioInputFormat.mBitsPerChannel = 32;
381                                                 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
382                                                 break;
383                                         case AUD_FORMAT_FLOAT32:
384                                                 qtexport->audioInputFormat.mBitsPerChannel = 32;
385                                                 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
386                                                 break;
387                                         case AUD_FORMAT_FLOAT64:
388                                                 qtexport->audioInputFormat.mBitsPerChannel = 64;
389                                                 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
390                                                 break;
391                                         case AUD_FORMAT_S16:
392                                         default:
393                                                 qtexport->audioInputFormat.mBitsPerChannel = 16;
394                                                 qtexport->audioInputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
395                                                 break;
396                                 }
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;
401                                 
402                                 
403                                 /*Output format*/
404                                 qtexport->audioOutputFormat.mFormatID = rd->qtcodecsettings.audiocodecType;
405                                 //TODO: set audio channels
406                                 qtexport->audioOutputFormat.mChannelsPerFrame = 2;
407                                 qtexport->audioOutputFormat.mSampleRate = rd->qtcodecsettings.audioSampleRate;
408                                 
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;
414
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;
420                                                 break;
421                                         case kAudioFormatAppleLossless:
422                                                 switch (U.audioformat) {
423                                                         case AUD_FORMAT_S16:
424                                                                 qtexport->audioOutputFormat.mFormatFlags = kAppleLosslessFormatFlag_16BitSourceData;
425                                                                 break;
426                                                         case AUD_FORMAT_S24:
427                                                                 qtexport->audioOutputFormat.mFormatFlags = kAppleLosslessFormatFlag_24BitSourceData;
428                                                                 break;
429                                                         case AUD_FORMAT_S32:
430                                                                 qtexport->audioOutputFormat.mFormatFlags = kAppleLosslessFormatFlag_32BitSourceData;
431                                                                 break;
432                                                         case AUD_FORMAT_U8:
433                                                         case AUD_FORMAT_FLOAT32:
434                                                         case AUD_FORMAT_FLOAT64:
435                                                         default:
436                                                                 break;
437                                                 }
438                                                 break;
439                                         case kAudioFormatLinearPCM:
440                                         default:
441                                                 switch (rd->qtcodecsettings.audioBitDepth) {
442                                                         case AUD_FORMAT_U8:
443                                                                 qtexport->audioOutputFormat.mBitsPerChannel = 8;
444                                                                 qtexport->audioOutputFormat.mFormatFlags = 0;
445                                                                 break;
446                                                         case AUD_FORMAT_S24:
447                                                                 qtexport->audioOutputFormat.mBitsPerChannel = 24;
448                                                                 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
449                                                                 break;
450                                                         case AUD_FORMAT_S32:
451                                                                 qtexport->audioOutputFormat.mBitsPerChannel = 32;
452                                                                 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
453                                                                 break;
454                                                         case AUD_FORMAT_FLOAT32:
455                                                                 qtexport->audioOutputFormat.mBitsPerChannel = 32;
456                                                                 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
457                                                                 break;
458                                                         case AUD_FORMAT_FLOAT64:
459                                                                 qtexport->audioOutputFormat.mBitsPerChannel = 64;
460                                                                 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
461                                                                 break;
462                                                         case AUD_FORMAT_S16:
463                                                         default:
464                                                                 qtexport->audioOutputFormat.mBitsPerChannel = 16;
465                                                                 qtexport->audioOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
466                                                                 break;
467                                                 }
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;
472                                                 break;
473                                 }
474                                                                                                 
475                                 err = AudioFileCreateWithURL(outputFileURL, audioFileType, &qtexport->audioOutputFormat, kAudioFileFlags_EraseFile, &qtexport->audioFile);
476                                 CFRelease(outputFileURL);
477                                 
478                                 if(err)
479                                         BKE_report(reports, RPT_ERROR, "\nQuicktime: unable to create temporary audio file. Format error ?");
480                                 else {
481                                         err = AudioConverterNew(&qtexport->audioInputFormat, &qtexport->audioOutputFormat, &qtexport->audioConverter);
482                                         if (err) {
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;
488                                         }
489                                         else {
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);
496                                                         
497                                                         if (rd->qtcodecsettings.audioCodecFlags & QTAUDIO_FLAG_CODEC_ISCBR)
498                                                                 prop = kAudioCodecBitRateControlMode_Constant;
499                                                         else
500                                                                 prop = kAudioCodecBitRateControlMode_LongTermAverage;
501                                                         AudioConverterSetProperty(qtexport->audioConverter, kAudioCodecPropertyBitRateControlMode,
502                                                                                                                         sizeof(prop), &prop);
503                                                 }
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);
509                                                         
510                                                         prop = kAudioConverterQuality_Max;
511                                                         AudioConverterSetProperty(qtexport->audioConverter, kAudioConverterSampleRateConverterQuality,
512                                                                                                           sizeof(prop), &prop);
513                                                 }
514                                                 
515                                                 write_cookie(qtexport->audioConverter, qtexport->audioFile);
516                                                 
517                                                 /* Allocate output buffer */
518                                                 if (qtexport->audioOutputFormat.mBytesPerPacket ==0) /* VBR */
519                                                         AudioConverterGetProperty(qtexport->audioConverter, kAudioConverterPropertyMaximumOutputPacketSize,
520                                                                                                   &propSize, &qtexport->audioCodecMaxOutputPacketSize);
521                                                 else
522                                                         qtexport->audioCodecMaxOutputPacketSize = qtexport->audioOutputFormat.mBytesPerPacket;
523                                                 
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,
527                                                                                                                                    "qt_audio_pktdesc");
528                                         }
529                                 }
530                         }
531                         
532                         if (err == noErr) {
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];
537
538                         }
539                 }
540                 else
541                         qtexport->movie = [[QTMovie alloc] initToWritableFile:qtexport->filename error:&error];
542                         
543                 if(qtexport->movie == nil) {
544                         BKE_report(reports, RPT_ERROR, "Unable to create quicktime movie.");
545                         success= 0;
546                         if (qtexport->filename) [qtexport->filename release];
547                         qtexport->filename = nil;
548                         if (qtexport->audioFileName) [qtexport->audioFileName release];
549                         qtexport->audioFileName = nil;
550                         if (qtexport->videoTempFileName) [qtexport->videoTempFileName release];
551                         qtexport->videoTempFileName = nil;
552                         [QTMovie exitQTKitOnThread];
553                 }
554                 else {
555                         [qtexport->movie retain];
556                         [qtexport->movie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
557                         [qtexport->movie setAttribute:@"Made with Blender" forKey:QTMovieCopyrightAttribute];
558                         
559                         qtexport->frameDuration = QTMakeTime(rd->frs_sec_base*1000, rd->frs_sec*1000);
560                         
561                         /* specifying the codec attributes : try to retrieve them from render data first*/
562                         if (rd->qtcodecsettings.codecType) {
563                                 qtexport->frameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
564                                                                                          stringWithCodecType(rd->qtcodecsettings.codecType),
565                                                                                          QTAddImageCodecType,
566                                                                                          [NSNumber numberWithLong:((rd->qtcodecsettings.codecSpatialQuality)*codecLosslessQuality)/100],
567                                                                                          QTAddImageCodecQuality,
568                                                                                          nil];
569                         }
570                         else {
571                                 qtexport->frameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:@"jpeg",
572                                                                                          QTAddImageCodecType,
573                                                                                          [NSNumber numberWithLong:codecHighQuality],
574                                                                                          QTAddImageCodecQuality,
575                                                                                          nil];
576                         }
577                         [qtexport->frameAttributes retain];
578                         
579                         if (qtexport->audioFile) {
580                                 /* Init audio input stream */
581                                 AUD_DeviceSpecs specs;
582
583                                 specs.channels = U.audiochannels;
584                                 specs.format = U.audioformat;
585                                 specs.rate = U.audiorate;
586                                 qtexport->audioInputDevice = AUD_openReadDevice(specs);
587                                 AUD_playDevice(qtexport->audioInputDevice, scene->sound_scene, rd->sfra * rd->frs_sec_base / rd->frs_sec);
588                                                                 
589                                 qtexport->audioOutputPktPos = 0;
590                                 qtexport->audioTotalExportedFrames = 0;
591                                 qtexport->audioTotalSavedFrames = 0;
592                                 
593                                 qtexport->audioLastFrame = (rd->efra - rd->sfra) * qtexport->audioInputFormat.mSampleRate * rd->frs_sec_base / rd->frs_sec;
594                         }
595                 }
596         }
597         
598         [pool drain];
599
600         return success;
601 }
602
603 int append_qt(struct RenderData *rd, int start_frame, int frame, int *pixels, int rectx, int recty, ReportList *reports)
604 {
605         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
606         NSBitmapImageRep *blBitmapFormatImage;
607         NSImage *frameImage;
608         OSStatus err = noErr;
609         unsigned char *from_Ptr,*to_Ptr;
610         int y,from_i,to_i;
611         
612         /* Create bitmap image rep in blender format (32bit RGBA) */
613         blBitmapFormatImage = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
614                                                                                                                                   pixelsWide:rectx 
615                                                                                                                                   pixelsHigh:recty
616                                                                                                                            bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
617                                                                                                                           colorSpaceName:NSCalibratedRGBColorSpace 
618                                                                                                                                 bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
619                                                                                                                                  bytesPerRow:rectx*4
620                                                                                                                                 bitsPerPixel:32];
621         if (!blBitmapFormatImage) {
622                 [pool drain];
623                 return 0;
624         }
625         
626         from_Ptr = (unsigned char*)pixels;
627         to_Ptr = (unsigned char*)[blBitmapFormatImage bitmapData];
628         for (y = 0; y < recty; y++) {
629                 to_i = (recty-y-1)*rectx;
630                 from_i = y*rectx;
631                 memcpy(to_Ptr+4*to_i, from_Ptr+4*from_i, 4*rectx);
632         }
633         
634         frameImage = [[NSImage alloc] initWithSize:NSMakeSize(rectx, recty)];
635         [frameImage addRepresentation:blBitmapFormatImage];
636         
637         /* Add the image to the movie clip */
638         [qtexport->movie addImage:frameImage
639                                   forDuration:qtexport->frameDuration
640                            withAttributes:qtexport->frameAttributes];
641
642         [blBitmapFormatImage release];
643         [frameImage release];
644         
645         
646         if (qtexport->audioFile) {
647                 UInt32 audioPacketsConverted;
648                 /* Append audio */
649                 while (qtexport->audioTotalExportedFrames < qtexport->audioLastFrame) { 
650
651                         qtexport->audioBufferList.mNumberBuffers = 1;
652                         qtexport->audioBufferList.mBuffers[0].mNumberChannels = qtexport->audioOutputFormat.mChannelsPerFrame;
653                         qtexport->audioBufferList.mBuffers[0].mDataByteSize = AUDIOOUTPUTBUFFERSIZE;
654                         qtexport->audioBufferList.mBuffers[0].mData = qtexport->audioOutputBuffer;
655                         audioPacketsConverted = AUDIOOUTPUTBUFFERSIZE / qtexport->audioCodecMaxOutputPacketSize;
656                         
657                         err = AudioConverterFillComplexBuffer(qtexport->audioConverter, AudioConverterInputCallback,
658                                                                                         NULL, &audioPacketsConverted, &qtexport->audioBufferList, qtexport->audioOutputPktDesc);
659                         if (audioPacketsConverted) {
660                                 AudioFileWritePackets(qtexport->audioFile, false, qtexport->audioBufferList.mBuffers[0].mDataByteSize,
661                                                                           qtexport->audioOutputPktDesc, qtexport->audioOutputPktPos, &audioPacketsConverted, qtexport->audioOutputBuffer);
662                                 qtexport->audioOutputPktPos += audioPacketsConverted;
663                                 
664                                 if (qtexport->audioOutputFormat.mFramesPerPacket) { 
665                                         // this is the common case: format has constant frames per packet
666                                         qtexport->audioTotalSavedFrames += (audioPacketsConverted * qtexport->audioOutputFormat.mFramesPerPacket);
667                                 }
668                                 else {
669                                         unsigned int i;
670                                         // if there are variable frames per packet, then we have to do this for each packeet
671                                         for (i = 0; i < audioPacketsConverted; ++i)
672                                                 qtexport->audioTotalSavedFrames += qtexport->audioOutputPktDesc[i].mVariableFramesInPacket;
673                                 }
674                                 
675                                 
676                         }
677                         else {
678                                 //Error getting audio packets
679                                 BKE_reportf(reports, RPT_ERROR, "Unable to get further audio packets from frame %i, error = 0x%x",(int)qtexport->audioTotalExportedFrames,err);
680                                 break;
681                         }
682
683                 }
684         }
685         [pool drain];   
686
687         return 1;
688 }
689
690
691 void end_qt(void)
692 {
693         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
694         if (qtexport->movie) {
695                 
696                 if (qtexport->audioFile)
697                 {
698                         NSDictionary *dict = nil;
699                         QTMovie *audioTmpMovie = nil;
700                         NSError *error;
701                         NSFileManager *fileManager;
702                         
703                         /* Mux video and audio then save file */
704                         
705                         /* Write last frames for VBR files */
706                         if (qtexport->audioOutputFormat.mBitsPerChannel == 0) {
707                                 OSStatus err = noErr;
708                                 AudioConverterPrimeInfo primeInfo;
709                                 UInt32 primeSize = sizeof(primeInfo);
710                                 
711                                 err = AudioConverterGetProperty(qtexport->audioConverter, kAudioConverterPrimeInfo, &primeSize, &primeInfo);
712                                 if (err == noErr) {
713                                         // there's priming to write out to the file
714                                         AudioFilePacketTableInfo pti;
715                                         pti.mPrimingFrames = primeInfo.leadingFrames;
716                                         pti.mRemainderFrames = primeInfo.trailingFrames;
717                                         pti.mNumberValidFrames = qtexport->audioTotalSavedFrames - pti.mPrimingFrames - pti.mRemainderFrames;
718                                         AudioFileSetProperty(qtexport->audioFile, kAudioFilePropertyPacketTableInfo, sizeof(pti), &pti);
719                                 }
720                                 
721                         }
722                         
723                         write_cookie(qtexport->audioConverter, qtexport->audioFile);
724                         AudioConverterDispose(qtexport->audioConverter);
725                         AudioFileClose(qtexport->audioFile);
726                         AUD_closeReadDevice(qtexport->audioInputDevice);
727                         qtexport->audioFile = NULL;
728                         qtexport->audioInputDevice = NULL;
729                         MEM_freeN(qtexport->audioInputBuffer);
730                         MEM_freeN(qtexport->audioOutputBuffer);
731                         MEM_freeN(qtexport->audioOutputPktDesc);
732                         
733                         /* Reopen audio file and merge it */
734                         audioTmpMovie = [QTMovie movieWithFile:qtexport->audioFileName error:&error];
735                         if (audioTmpMovie) {
736                                 NSArray *audioTracks = [audioTmpMovie tracksOfMediaType:QTMediaTypeSound];
737                                 QTTrack *audioTrack = nil;
738                                 if( [audioTracks count] > 0 )
739                                 {
740                                         audioTrack = [audioTracks objectAtIndex:0];
741                                 }
742                         
743                                 if( audioTrack )
744                                 {
745                                         QTTimeRange totalRange;
746                                         totalRange.time = QTZeroTime;
747                                         totalRange.duration = [[audioTmpMovie attributeForKey:QTMovieDurationAttribute] QTTimeValue];
748                                         
749                                         [qtexport->movie insertSegmentOfTrack:audioTrack timeRange:totalRange atTime:QTZeroTime];
750                                 }
751                         }
752                         
753                         /* Save file */
754                         dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] 
755                                                                                            forKey:QTMovieFlatten];
756
757                         if (dict) {
758                                 [qtexport->movie writeToFile:qtexport->filename withAttributes:dict];
759                         }
760                         
761                         /* Delete temp files */
762                         fileManager = [[NSFileManager alloc] init];
763                         [fileManager removeItemAtPath:qtexport->audioFileName error:&error];
764                         [fileManager removeItemAtPath:qtexport->videoTempFileName error:&error];
765                         [fileManager release];
766                 }
767                 else {
768                         /* Flush update of the movie file */
769                         [qtexport->movie updateMovieFile];
770                         
771                         [qtexport->movie invalidate];
772                 }
773                 
774                 /* Clean up movie structure */
775                 if (qtexport->filename) [qtexport->filename release];
776                 qtexport->filename = nil;
777                 if (qtexport->audioFileName) [qtexport->audioFileName release];
778                 qtexport->audioFileName = nil;
779                 if (qtexport->videoTempFileName) [qtexport->videoTempFileName release];
780                 qtexport->videoTempFileName = nil;
781                 [qtexport->frameAttributes release];
782                 [qtexport->movie release];
783         }
784         
785         [QTMovie exitQTKitOnThread];
786
787         if(qtexport) {
788                 MEM_freeN(qtexport);
789                 qtexport = NULL;
790         }
791         [pool drain];
792 }
793
794
795 void free_qtcomponentdata(void)
796 {
797 }
798
799 void quicktime_verify_image_type(RenderData *rd, ImageFormatData *imf)
800 {
801         if (imf->imtype == R_IMF_IMTYPE_QUICKTIME) {
802                 if ((rd->qtcodecsettings.codecType<= 0) ||
803                         (rd->qtcodecsettings.codecSpatialQuality <0) ||
804                         (rd->qtcodecsettings.codecSpatialQuality > 100)) {
805                         
806                         rd->qtcodecsettings.codecType = kJPEGCodecType;
807                         rd->qtcodecsettings.codecSpatialQuality = (codecHighQuality*100)/codecLosslessQuality;
808                 }
809                 if ((rd->qtcodecsettings.audioSampleRate < 21000) ||
810                         (rd->qtcodecsettings.audioSampleRate > 193000)) 
811                         rd->qtcodecsettings.audioSampleRate = 48000;
812                 
813                 if (rd->qtcodecsettings.audioBitDepth == 0)
814                         rd->qtcodecsettings.audioBitDepth = AUD_FORMAT_S16;
815                 
816                 if (rd->qtcodecsettings.audioBitRate == 0)
817                         rd->qtcodecsettings.audioBitRate = 256000;
818         }
819 }
820
821 #endif /* _WIN32 || __APPLE__ */
822 #endif /* WITH_QUICKTIME */
823