Permalink
Browse files

Added a new playAtActualSpeed option to GPUImageMovie to have it play…

…back at the actual speed of the source movie. The timing isn't perfect on this yet.
  • Loading branch information...
1 parent 202ce48 commit fa8ea2fb829a64803eb26d056ede8e71585c4d94 @BradLarson committed Sep 5, 2012
@@ -25,6 +25,7 @@ - (void)viewDidLoad
movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
movieFile.runBenchmark = YES;
+ movieFile.playAtActualSpeed = YES;
// filter = [[GPUImagePixellateFilter alloc] init];
// filter = [[GPUImageUnsharpMaskFilter alloc] init];
@@ -86,7 +87,8 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
- (IBAction)updatePixelWidth:(id)sender
{
- [(GPUImageUnsharpMaskFilter *)filter setIntensity:[(UISlider *)sender value]];
+// [(GPUImageUnsharpMaskFilter *)filter setIntensity:[(UISlider *)sender value]];
+ [(GPUImageDissolveBlendFilter *)filter setMix:[(UISlider *)sender value]];
// pixellateFilter.fractionalWidthOfAPixel = [(UISlider *)sender value];
}
@@ -14,6 +14,10 @@
*/
@property(readwrite, nonatomic) BOOL runBenchmark;
+/** This determines whether to play back a movie as fast as the frames can be processed, or if the original speed of the movie should be respected. Defaults to NO.
+ */
+@property(readwrite, nonatomic) BOOL playAtActualSpeed;
+
/// @name Initialization and teardown
- (id)initWithAsset:(AVAsset *)asset;
- (id)initWithURL:(NSURL *)url;
@@ -7,6 +7,8 @@ @interface GPUImageMovie ()
GPUImageMovieWriter *synchronizedMovieWriter;
CVOpenGLESTextureCacheRef coreVideoTextureCache;
AVAssetReader *reader;
+ CMTime previousFrameTime;
+ CFAbsoluteTime previousActualFrameTime;
}
- (void)processAsset;
@@ -18,6 +20,7 @@ @implementation GPUImageMovie
@synthesize url = _url;
@synthesize asset = _asset;
@synthesize runBenchmark = _runBenchmark;
+@synthesize playAtActualSpeed = _playAtActualSpeed;
#pragma mark -
#pragma mark Initialization and teardown
@@ -92,10 +95,14 @@ - (void)enableSynchronizedEncodingUsingMovieWriter:(GPUImageMovieWriter *)movieW
- (void)startProcessing
{
- if(self.url == nil) {
+ if(self.url == nil)
+ {
[self processAsset];
return;
}
+
+ previousFrameTime = kCMTimeZero;
+ previousActualFrameTime = CFAbsoluteTimeGetCurrent();
NSDictionary *inputOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:self.url options:inputOptions];
@@ -181,6 +188,25 @@ - (void)readNextVideoFrameFromOutput:(AVAssetReaderTrackOutput *)readerVideoTrac
CMSampleBufferRef sampleBufferRef = [readerVideoTrackOutput copyNextSampleBuffer];
if (sampleBufferRef)
{
+ if (_playAtActualSpeed)
+ {
+ // Do this outside of the video processing queue to not slow that down while waiting
+ CMTime currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBufferRef);
+ CMTime differenceFromLastFrame = CMTimeSubtract(currentSampleTime, previousFrameTime);
+ CFAbsoluteTime currentActualTime = CFAbsoluteTimeGetCurrent();
+
+ CGFloat frameTimeDifference = CMTimeGetSeconds(differenceFromLastFrame);
+ CGFloat actualTimeDifference = currentActualTime - previousActualFrameTime;
+
+ if (frameTimeDifference > actualTimeDifference)
+ {
+ usleep(1000000.0 * (frameTimeDifference - actualTimeDifference));
+ }
+
+ previousFrameTime = currentSampleTime;
+ previousActualFrameTime = CFAbsoluteTimeGetCurrent();
+ }
+
__unsafe_unretained GPUImageMovie *weakSelf = self;
runSynchronouslyOnVideoProcessingQueue(^{
[weakSelf processMovieFrame:sampleBufferRef];
@@ -230,6 +256,9 @@ - (void)readNextAudioSampleFromOutput:(AVAssetReaderTrackOutput *)readerAudioTra
- (void)processMovieFrame:(CMSampleBufferRef)movieSampleBuffer;
{
+// CMTimeGetSeconds
+// CMTimeSubtract
+
CMTime currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(movieSampleBuffer);
CVImageBufferRef movieFrame = CMSampleBufferGetImageBuffer(movieSampleBuffer);

0 comments on commit fa8ea2f

Please sign in to comment.