1 // Copyright (c) 2009 Yanagi Asakura
3 // This software is provided 'as-is', without any express or implied
4 // warranty. In no event will the authors be held liable for any damages
5 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
11 // 1. The origin of this software must not be misrepresented; you must not
12 // claim that you wrote the original software. If you use this software
13 // in a product, an acknowledgment in the product documentation would be
14 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
19 // 3. This notice may not be removed or altered from any source
26 // Created by 柳 on 09/09/12.
27 // Copyright 2009 __MyCompanyName__. All rights reserved.
33 @implementation ElisMedia
35 // QuickTime X を試すためにかなり書き換えた。けど戻した。
36 - (id)initWithMovieFile:(NSString*)path
41 movie = [[QTMovie alloc] initWithFile:path error:nil];
42 [movie gotoBeginning];
44 QTOpenGLTextureContextCreate(kCFAllocatorDefault,
45 [[mainView openGLContext] CGLContextObj],
46 [[mainView pixelFormat] CGLPixelFormatObj],
50 SetMovieVisualContext([movie quickTimeMovie], textureContext);
52 attr = [NSDictionary dictionaryWithObjectsAndKeys:
53 QTMovieFrameImageTypeCIImage, QTMovieFrameImageType,
54 #ifdef __SNOW_LEOPARD__
55 [NSNumber numberWithBool:YES], QTMovieFrameImageSessionMode, // 内部でキャッシュしろ
57 [NSNumber numberWithBool:YES], QTMovieFrameImageHighQuality, // 画質低下を許さない
58 [movie attributeForKey:QTMovieNaturalSizeAttribute], QTMovieFrameImageSize, // 勝手にリサイズするな
59 [NSNumber numberWithBool:NO], QTMovieFrameImageSingleField, // インターレース解除?
66 [movie setVolume:0.0f];
68 if([[NSWorkspace sharedWorkspace] type:[[NSWorkspace sharedWorkspace] typeOfFile:path error:nil]
69 conformsToType:@"com.apple.quartz-composer-composition"])
77 - (id)initWithImageFile:(NSString*)path
84 image = [[CIImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path]];
91 - (id)initWithSoundFile:(NSString*)path
96 sound = [[QTMovie alloc] initWithFile:path error:nil];
97 [sound setVolume:0.2]; // あまりにうるさい。
103 - (id)initWithText:(NSAttributedString*)t
110 // [attr setValue:[NSFont fontWithName:@"HiraKakuPro-W3" size:24.0f] forKey:NSFontAttributeName];
111 // [attr setValue:[NSColor colorWithCalibratedRed:255 green:255 blue:255 alpha:255] forKey:NSForegroundColorAttributeName];
112 NSRect size = [t boundingRectWithSize:NSMakeSize(4000, 3000) options:NSStringDrawingUsesLineFragmentOrigin];
113 im = [[NSImage alloc] initWithSize:size.size];
115 // t = [[NSAttributedString alloc] initWithString:@"string"];
116 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
119 image = [[CIImage alloc] initWithData:[im TIFFRepresentation]];
125 - (id)initWithQuartzFile:(NSString*)path
127 return [self initWithMovieFile:path];
132 - (CIImage*)getFrameForTime:(CVTimeStamp*)timeStamp
134 CVImageBufferRef currentFrame;
138 QTVisualContextCopyImageForTime(textureContext, NULL, timeStamp, ¤tFrame);
139 ret = [CIImage imageWithCVImageBuffer:currentFrame];
140 CVOpenGLTextureRelease(currentFrame);
148 - (CIImage*)getFrameForQTTime:(QTTime)time
150 // 64ビットでframeImageAtTimeメソッドは使えないみたい。どうしろと。
151 time.timeValue *= speed;
153 return [movie frameImageAtTime:time withAttributes:attr error:nil];
159 - (void)releaseContext
162 QTVisualContextTask(textureContext);
165 - (void)setCurrentTime:(QTTime)time
167 time.timeValue *= speed;
169 [movie setAttribute:[NSValue valueWithQTTime:time] forKey:QTMovieCurrentTimeAttribute];
170 MoviesTask([movie quickTimeMovie], 0);
172 if(sound && !playing){
173 [sound setCurrentTime:time];
174 MoviesTask([sound quickTimeMovie], 0);
180 if(movie && !playing && usingStampMode){
182 [movie setRate:speed];
185 if(sound && !playing){
193 if(movie && YES && usingStampMode){
206 if(isQuartz) return QTMakeTime(30, 1);
207 QTTime d = [movie duration];
208 d.timeValue /= speed;
209 float s = (float)d.timeValue/d.timeScale;
210 if(s > 60*10) return QTMakeTime(60*10, 1);
214 return [sound duration];
216 return QTMakeTime(10, 1);
222 NSLog(@"movie finalize");
224 SetMovieVisualContext([movie quickTimeMovie], NULL);
226 CFRelease(textureContext);
232 - (QTTrack*)getSoundTrack
238 tracks = [sound tracksOfMediaType:QTMediaTypeSound];
239 size = [tracks count];
240 if(size > 0) return [tracks objectAtIndex:0];
267 - (void)encodeWithCoder:(NSCoder*)encoder
270 [encoder encodeObject:_path forKey:@"path"];
271 [encoder encodeObject:text forKey:@"text"];
272 [encoder encodeFloat:speed forKey:@"speed"];
275 else if(sound) flag = 1;
276 else if(text) flag = 3;
277 else if(image) flag = 2;
279 [encoder encodeInt:flag forKey:@"flag"];
282 - (id)initWithCoder:(NSCoder*)coder
285 NSString* path = [coder decodeObjectForKey:@"path"];
286 NSAttributedString* t = [coder decodeObjectForKey:@"text"];
287 flag = [coder decodeIntForKey:@"flag"];
292 ret = [self initWithMovieFile:path];
294 ret = [self initWithSoundFile:path];
296 ret = [self initWithImageFile:path];
298 ret = [self initWithText:t];
300 speed = [coder decodeFloatForKey:@"speed"];
305 - (QTMovie*)getSoundMovie
313 - (NSAttributedString*)text
318 - (void)setText:(NSAttributedString*)t
320 [self initWithText:t];
329 s = [[movie attributeForKey:QTMovieCurrentSizeAttribute] sizeValue];
330 return NSMakeRect(0, 0, s.width, s.height);
333 CGRect c = [image extent];
343 - (void)setSpeed:(float)s
348 - (void)setVolume:(float)v