Skip to content

Commit

Permalink
Added a new playAtActualSpeed option to GPUImageMovie to have it play…
Browse files Browse the repository at this point in the history
…back at the actual speed of the source movie. The timing isn't perfect on this yet.
  • Loading branch information
BradLarson committed Sep 5, 2012
1 parent 202ce48 commit fa8ea2f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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];
}

Expand Down
4 changes: 4 additions & 0 deletions framework/Source/GPUImageMovie.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 30 additions & 1 deletion framework/Source/GPUImageMovie.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ @interface GPUImageMovie ()
GPUImageMovieWriter *synchronizedMovieWriter;
CVOpenGLESTextureCacheRef coreVideoTextureCache;
AVAssetReader *reader;
CMTime previousFrameTime;
CFAbsoluteTime previousActualFrameTime;
}

- (void)processAsset;
Expand All @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -230,6 +256,9 @@ - (void)readNextAudioSampleFromOutput:(AVAssetReaderTrackOutput *)readerAudioTra

- (void)processMovieFrame:(CMSampleBufferRef)movieSampleBuffer;
{
// CMTimeGetSeconds
// CMTimeSubtract

CMTime currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(movieSampleBuffer);
CVImageBufferRef movieFrame = CMSampleBufferGetImageBuffer(movieSampleBuffer);

Expand Down

0 comments on commit fa8ea2f

Please sign in to comment.