Welcome to mirror list, hosted at ThFree Co, Russian Federation.

qtkit_export.m « apple « quicktime « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 460b35a05924fe95d2a656a6b5cc751c6e845267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
 * $Id$
 *
 * qtkit_export.m
 *
 * Code to create QuickTime Movies with Blender
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 * The Original Code is written by Rob Haarsma (phase)
 *
 * Contributor(s): Stefan Gartner (sgefant)
 *				   Damien Plisson 11/2009
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#ifdef WITH_QUICKTIME
#if defined(_WIN32) || defined(__APPLE__)

#include <stdio.h>
#include <string.h>

#include "DNA_scene_types.h"

#include "BKE_global.h"
#include "BKE_scene.h"

#include "BLI_blenlib.h"

#include "BLO_sys_types.h"

#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

#include "MEM_guardedalloc.h"

#include "quicktime_import.h"
#include "quicktime_export.h"


#ifdef __APPLE__
/* evil */
#ifndef __AIFF__
#define __AIFF__
#endif
#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>
#endif /* __APPLE__ */

typedef struct QuicktimeExport {
	QTMovie *movie;
	
	NSString *filename;

	QTTime frameDuration;
	NSDictionary *frameAttributes;
} QuicktimeExport;

static struct QuicktimeExport *qtexport;


void makeqtstring (RenderData *rd, char *string) {
	char txt[64];

	strcpy(string, rd->pic);
	BLI_convertstringcode(string, G.sce);

	BLI_make_existing_file(string);

	if (BLI_strcasecmp(string + strlen(string) - 4, ".mov")) {
		sprintf(txt, "%04d_%04d.mov", (rd->sfra) , (rd->efra) );
		strcat(string, txt);
	}
}


void start_qt(struct Scene *scene, struct RenderData *rd, int rectx, int recty)
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSError *error;
	char name[2048];

	
	if(qtexport == NULL) qtexport = MEM_callocN(sizeof(QuicktimeExport), "QuicktimeExport");

	if (G.afbreek != 1) {
		/* Check first if the QuickTime 7.2.1 initToWritableFile: method is available */
		if ([[[[QTMovie alloc] init] autorelease] respondsToSelector:@selector(initToWritableFile:error:)] != YES) {
			G.afbreek = 1;
			fprintf(stderr, "\nUnable to create quicktime movie, need Quicktime rev 7.2.1 or later");
		}
		else {
			makeqtstring(rd, name);
			qtexport->filename = [NSString stringWithCString:name
									  encoding:[NSString defaultCStringEncoding]];
			qtexport->movie = [[QTMovie alloc] initToWritableFile:qtexport->filename error:&error];
				
			if(qtexport->movie == nil) {
				G.afbreek = 1;
				NSLog(@"Unable to create quicktime movie : %@",[error localizedDescription]);
			} else {
				[qtexport->movie retain];
				[qtexport->filename retain];
				[qtexport->movie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
				[qtexport->movie setAttribute:@"Made with Blender" forKey:QTMovieCopyrightAttribute];
				
				qtexport->frameDuration = QTMakeTime(rd->frs_sec_base*1000, rd->frs_sec*1000);
				
				/* specifying the codec attributes
				TODO: get these values from RenderData/scene*/
				qtexport->frameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:@"jpeg",
											 QTAddImageCodecType,
											 [NSNumber numberWithLong:codecHighQuality],
											 QTAddImageCodecQuality,
											 nil];
				[qtexport->frameAttributes retain];
			}
		}
	}
	
	[pool drain];
}


void append_qt(struct RenderData *rd, int frame, int *pixels, int rectx, int recty)
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSBitmapImageRep *blBitmapFormatImage;
	NSImage *frameImage;
	unsigned char *from_Ptr,*to_Ptr;
	int y,from_i,to_i;
	
	
	/* Create bitmap image rep in blender format (32bit RGBA) */
	blBitmapFormatImage = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
																  pixelsWide:rectx 
																  pixelsHigh:recty
															   bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
															  colorSpaceName:NSCalibratedRGBColorSpace 
																bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
																 bytesPerRow:rectx*4
																bitsPerPixel:32];
	if (!blBitmapFormatImage) {
		[pool drain];
		return;
	}
	
	from_Ptr = (unsigned char*)pixels;
	to_Ptr = (unsigned char*)[blBitmapFormatImage bitmapData];
	for (y = 0; y < recty; y++) {
		to_i = (recty-y-1)*rectx;
		from_i = y*rectx;
		memcpy(to_Ptr+4*to_i, from_Ptr+4*from_i, 4*rectx);
	}
	
	frameImage = [[NSImage alloc] initWithSize:NSMakeSize(rectx, recty)];
	[frameImage addRepresentation:blBitmapFormatImage];
	
	/* Add the image to the movie clip */
	[qtexport->movie addImage:frameImage
				  forDuration:qtexport->frameDuration
			   withAttributes:qtexport->frameAttributes];

	[blBitmapFormatImage release];
	[frameImage release];
	[pool drain];	
}


void end_qt(void)
{
	if (qtexport->movie) {
		/* Flush update of the movie file */
		[qtexport->movie updateMovieFile];
		
		[qtexport->movie invalidate];
		
		/* Clean up movie structure */
		[qtexport->filename release];
		[qtexport->frameAttributes release];
		[qtexport->movie release];
		}

	if(qtexport) {
		MEM_freeN(qtexport);
		qtexport = NULL;
	}
}


void free_qtcomponentdata(void) {
}


int get_qtcodec_settings(RenderData *rd) 
{
/*
	// get previous selected codecsetting, if any 
	if(rd->qtcodecdata && rd->qtcodecdata->cdParms) {
		QT_GetCodecSettingsFromScene(rd);
		check_renderbutton_framerate(rd);
	} else {
		// configure the standard image compression dialog box
		// set some default settings
		qtdata->gSpatialSettings.codec = anyCodec;         
		qtdata->gSpatialSettings.spatialQuality = codecMaxQuality;
		qtdata->gTemporalSettings.temporalQuality = codecMaxQuality;
		qtdata->gTemporalSettings.keyFrameRate = 25;   
		qtdata->aDataRateSetting.dataRate = 90 * 1024;          

		err = SCSetInfo(qtdata->theComponent, scTemporalSettingsType,	&qtdata->gTemporalSettings);
		CheckError(err, "SCSetInfo1 error");
		err = SCSetInfo(qtdata->theComponent, scSpatialSettingsType,	&qtdata->gSpatialSettings);
		CheckError(err, "SCSetInfo2 error");
		err = SCSetInfo(qtdata->theComponent, scDataRateSettingsType,	&qtdata->aDataRateSetting);
		CheckError(err, "SCSetInfo3 error");
	}

	check_renderbutton_framerate(rd);

	// put up the dialog box - it needs to be called from the main thread
	err = SCRequestSequenceSettings(qtdata->theComponent);
 
	if (err == scUserCancelled) {
		G.afbreek = 1;
		return 0;
	}

	// get user selected data
	SCGetInfo(qtdata->theComponent, scTemporalSettingsType,	&qtdata->gTemporalSettings);
	SCGetInfo(qtdata->theComponent, scSpatialSettingsType,	&qtdata->gSpatialSettings);
	SCGetInfo(qtdata->theComponent, scDataRateSettingsType,	&qtdata->aDataRateSetting);

	QT_SaveCodecSettingsToScene(rd);

	// framerate jugglin'
	switch (qtexport->frameRate) {
		case 1571553: // 23.98 fps
			qtexport->frameDuration = QTMakeTime(1001, 24000);
			rd->frs_sec = 24;
			rd->frs_sec_base = 1.001;
			break;
		case 1964113: // 29.97 fps
			qtexport->frameDuration = QTMakeTime(1001, 30000);
			rd->frs_sec = 30;
			rd->frs_sec_base = 1.001;
			break;
		case 3928227: // 59.94 fps
			qtexport->frameDuration = QTMakeTime(1001, 60000);
			rd->frs_sec = 60;
			rd->frs_sec_base = 1.001;
			break;
		default:
		{
			double fps = qtexport->frameRate;
			qtexport->frameDuration = QTMakeTime(60000/(qtexport->frameRate / 65536), 60000);
			
			if ((qtexport->frameRate & 0xffff) == 0) {
				rd->frs_sec = fps / 65536;
				rd->frs_sec_base = 1;
			} else {
				// we do our very best... 
				rd->frs_sec = (fps * 10000 / 65536);
				rd->frs_sec_base = 10000;
			}
		}
			break;
	}
*/
	return 1;
}

#endif /* _WIN32 || __APPLE__ */
#endif /* WITH_QUICKTIME */