9f2a97dee237bd7775750e4a5674522156ee4499
[blender.git] / source / blender / quicktime / apple / qtkit_export.m
1 /**
2  * $Id$
3  *
4  * qtkit_export.m
5  *
6  * Code to create QuickTime Movies with Blender
7  *
8  * ***** BEGIN GPL LICENSE BLOCK *****
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  *
24  * The Original Code is written by Rob Haarsma (phase)
25  *
26  * Contributor(s): Stefan Gartner (sgefant)
27  *                                 Damien Plisson 11/2009
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #ifdef WITH_QUICKTIME
33 #if defined(_WIN32) || defined(__APPLE__)
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "DNA_scene_types.h"
39
40 #include "BKE_global.h"
41 #include "BKE_scene.h"
42
43 #include "BLI_blenlib.h"
44
45 #include "BLO_sys_types.h"
46
47 #include "IMB_imbuf.h"
48 #include "IMB_imbuf_types.h"
49
50 #include "MEM_guardedalloc.h"
51
52 #include "quicktime_import.h"
53 #include "quicktime_export.h"
54
55
56 #ifdef __APPLE__
57 /* evil */
58 #ifndef __AIFF__
59 #define __AIFF__
60 #endif
61 #import <Cocoa/Cocoa.h>
62 #import <QTKit/QTKit.h>
63 #endif /* __APPLE__ */
64
65 typedef struct QuicktimeExport {
66         QTMovie *movie;
67         
68         NSString *filename;
69
70         QTTime frameDuration;
71         NSDictionary *frameAttributes;
72 } QuicktimeExport;
73
74 static struct QuicktimeExport *qtexport;
75
76
77 static NSString *stringWithCodecType(int codecType) {
78         switch (codecType) {
79                 case QT_CODECTYPE_RAW:
80                         return @"raw ";
81                 case QT_CODECTYPE_MJPEGA:
82                         return @"mjpa";
83                 case QT_CODECTYPE_MJPEGB:
84                         return @"mjpb";
85                 case QT_CODECTYPE_DVCPAL:
86                         return @"dvcp";
87                 case QT_CODECTYPE_DVCNTSC:
88                         return @"dvc ";
89                 case QT_CODECTYPE_MPEG4:
90                         return @"mp4v";
91                 case QT_CODECTYPE_H263:
92                         return @"h263";
93                 case QT_CODECTYPE_H264:
94                         return @"avc1";
95                 case QT_CODECTYPE_DVCPROHD720p:
96                         return @"dvhp";
97                 case QT_CODECTYPE_DVCPROHD1080i50:
98                         return @"dvh5";
99                 case QT_CODECTYPE_DVCPROHD1080i60:
100                         return @"dvh6";
101                         
102                 case QT_CODECTYPE_JPEG:
103                 default:
104                         return @"jpeg"; 
105         }
106 }
107
108 void makeqtstring (RenderData *rd, char *string) {
109         char txt[64];
110
111         strcpy(string, rd->pic);
112         BLI_convertstringcode(string, G.sce);
113
114         BLI_make_existing_file(string);
115
116         if (BLI_strcasecmp(string + strlen(string) - 4, ".mov")) {
117                 sprintf(txt, "%04d_%04d.mov", (rd->sfra) , (rd->efra) );
118                 strcat(string, txt);
119         }
120 }
121
122
123 void start_qt(struct Scene *scene, struct RenderData *rd, int rectx, int recty)
124 {
125         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
126         NSError *error;
127         char name[2048];
128
129         
130         if(qtexport == NULL) qtexport = MEM_callocN(sizeof(QuicktimeExport), "QuicktimeExport");
131
132         if (G.afbreek != 1) {
133                 /* Check first if the QuickTime 7.2.1 initToWritableFile: method is available */
134                 if ([[[[QTMovie alloc] init] autorelease] respondsToSelector:@selector(initToWritableFile:error:)] != YES) {
135                         G.afbreek = 1;
136                         fprintf(stderr, "\nUnable to create quicktime movie, need Quicktime rev 7.2.1 or later");
137                 }
138                 else {
139                         makeqtstring(rd, name);
140                         qtexport->filename = [NSString stringWithCString:name
141                                                                           encoding:[NSString defaultCStringEncoding]];
142                         qtexport->movie = [[QTMovie alloc] initToWritableFile:qtexport->filename error:&error];
143                                 
144                         if(qtexport->movie == nil) {
145                                 G.afbreek = 1;
146                                 NSLog(@"Unable to create quicktime movie : %@",[error localizedDescription]);
147                         } else {
148                                 [qtexport->movie retain];
149                                 [qtexport->filename retain];
150                                 [qtexport->movie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
151                                 [qtexport->movie setAttribute:@"Made with Blender" forKey:QTMovieCopyrightAttribute];
152                                 
153                                 qtexport->frameDuration = QTMakeTime(rd->frs_sec_base*1000, rd->frs_sec*1000);
154                                 
155                                 /* specifying the codec attributes : try to retrieve them from render data first*/
156                                 if (rd->qtcodecsettings.codecType) {
157                                         qtexport->frameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
158                                                                                                  stringWithCodecType(rd->qtcodecsettings.codecType),
159                                                                                                  QTAddImageCodecType,
160                                                                                                  [NSNumber numberWithLong:((rd->qtcodecsettings.codecSpatialQuality)*codecLosslessQuality)/100],
161                                                                                                  QTAddImageCodecQuality,
162                                                                                                  nil];
163                                 }
164                                 else {
165                                         qtexport->frameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:@"jpeg",
166                                                                                                  QTAddImageCodecType,
167                                                                                                  [NSNumber numberWithLong:codecHighQuality],
168                                                                                                  QTAddImageCodecQuality,
169                                                                                                  nil];
170                                 }
171                                 [qtexport->frameAttributes retain];
172                         }
173                 }
174         }
175         
176         [pool drain];
177 }
178
179
180 void append_qt(struct RenderData *rd, int frame, int *pixels, int rectx, int recty)
181 {
182         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
183         NSBitmapImageRep *blBitmapFormatImage;
184         NSImage *frameImage;
185         unsigned char *from_Ptr,*to_Ptr;
186         int y,from_i,to_i;
187         
188         
189         /* Create bitmap image rep in blender format (32bit RGBA) */
190         blBitmapFormatImage = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
191                                                                                                                                   pixelsWide:rectx 
192                                                                                                                                   pixelsHigh:recty
193                                                                                                                            bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
194                                                                                                                           colorSpaceName:NSCalibratedRGBColorSpace 
195                                                                                                                                 bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
196                                                                                                                                  bytesPerRow:rectx*4
197                                                                                                                                 bitsPerPixel:32];
198         if (!blBitmapFormatImage) {
199                 [pool drain];
200                 return;
201         }
202         
203         from_Ptr = (unsigned char*)pixels;
204         to_Ptr = (unsigned char*)[blBitmapFormatImage bitmapData];
205         for (y = 0; y < recty; y++) {
206                 to_i = (recty-y-1)*rectx;
207                 from_i = y*rectx;
208                 memcpy(to_Ptr+4*to_i, from_Ptr+4*from_i, 4*rectx);
209         }
210         
211         frameImage = [[NSImage alloc] initWithSize:NSMakeSize(rectx, recty)];
212         [frameImage addRepresentation:blBitmapFormatImage];
213         
214         /* Add the image to the movie clip */
215         [qtexport->movie addImage:frameImage
216                                   forDuration:qtexport->frameDuration
217                            withAttributes:qtexport->frameAttributes];
218
219         [blBitmapFormatImage release];
220         [frameImage release];
221         [pool drain];   
222 }
223
224
225 void end_qt(void)
226 {
227         if (qtexport->movie) {
228                 /* Flush update of the movie file */
229                 [qtexport->movie updateMovieFile];
230                 
231                 [qtexport->movie invalidate];
232                 
233                 /* Clean up movie structure */
234                 [qtexport->filename release];
235                 [qtexport->frameAttributes release];
236                 [qtexport->movie release];
237                 }
238
239         if(qtexport) {
240                 MEM_freeN(qtexport);
241                 qtexport = NULL;
242         }
243 }
244
245
246 void free_qtcomponentdata(void) {
247 }
248
249 void quicktime_verify_image_type(RenderData *rd)
250 {
251         if (rd->imtype == R_QUICKTIME) {
252                 if ((rd->qtcodecsettings.codecType<= 0) ||
253                         (rd->qtcodecsettings.codecSpatialQuality <0) ||
254                         (rd->qtcodecsettings.codecSpatialQuality > 100)) {
255                         
256                         rd->qtcodecsettings.codecType = QT_CODECTYPE_JPEG;
257                         rd->qtcodecsettings.codecSpatialQuality = (codecHighQuality*100)/codecLosslessQuality;
258                 }
259         }
260 }
261
262 #endif /* _WIN32 || __APPLE__ */
263 #endif /* WITH_QUICKTIME */
264