Skip to content

Commit 0e711a8

Browse files
committed
Fixed photo capture on iPhone 4. Fixed data acquisition from raw data outputs.
1 parent 58708f8 commit 0e711a8

File tree

7 files changed

+21
-37
lines changed

7 files changed

+21
-37
lines changed

examples/iOS/SimpleVideoFilter/SimpleVideoFilter/SimpleVideoFilterViewController.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ - (void)viewDidLoad
4444

4545
[videoCamera addTarget:filter];
4646
GPUImageView *filterView = (GPUImageView *)self.view;
47-
[filter addTarget:filterView];
4847
// filterView.fillMode = kGPUImageFillModeStretch;
4948
// filterView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
5049

@@ -54,10 +53,12 @@ - (void)viewDidLoad
5453
unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
5554
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
5655
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
56+
movieWriter.encodingLiveVideo = YES;
5757
// movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)];
5858
// movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720.0, 1280.0)];
5959
// movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(1080.0, 1920.0)];
6060
[filter addTarget:movieWriter];
61+
[filter addTarget:filterView];
6162

6263
[videoCamera startCameraCapture];
6364

@@ -66,7 +67,7 @@ - (void)viewDidLoad
6667
dispatch_after(startTime, dispatch_get_main_queue(), ^(void){
6768
NSLog(@"Start recording");
6869

69-
videoCamera.audioEncodingTarget = movieWriter;
70+
// videoCamera.audioEncodingTarget = movieWriter;
7071
[movieWriter startRecording];
7172

7273
// NSError *error = nil;
@@ -77,7 +78,7 @@ - (void)viewDidLoad
7778
// [videoCamera.inputCamera setTorchMode:AVCaptureTorchModeOn];
7879
// [videoCamera.inputCamera unlockForConfiguration];
7980

80-
double delayInSeconds = 30.0;
81+
double delayInSeconds = 10.0;
8182
dispatch_time_t stopTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
8283
dispatch_after(stopTime, dispatch_get_main_queue(), ^(void){
8384

framework/Source/GPUImageFramebuffer.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ - (CGImageRef)newCGImageFromFramebufferContents;
371371
- (void)restoreRenderTarget;
372372
{
373373
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
374-
CFRelease(renderTarget);
375374
CVPixelBufferUnlockBaseAddress(renderTarget, 0);
375+
CFRelease(renderTarget);
376376
#else
377377
#endif
378378
}
@@ -399,7 +399,10 @@ - (NSUInteger)bytesPerRow;
399399
- (GLubyte *)byteBuffer;
400400
{
401401
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
402-
return (GLubyte *)CVPixelBufferGetBaseAddress(renderTarget);
402+
CVPixelBufferLockBaseAddress(renderTarget, 0);
403+
GLubyte * bufferBytes = CVPixelBufferGetBaseAddress(renderTarget);
404+
CVPixelBufferUnlockBaseAddress(renderTarget, 0);
405+
return bufferBytes;
403406
#else
404407
return NULL; // TODO: do more with this on the non-texture-cache side
405408
#endif

framework/Source/GPUImageRawDataOutput.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ - (GLubyte *)rawBytesForImage;
260260
{
261261
glFinish();
262262
_rawBytesForImage = [outputFramebuffer byteBuffer];
263+
NSLog(@"Output framebuffer: %@", outputFramebuffer);
263264
}
264265
else
265266
{

framework/Source/GPUImageStillCamera.m

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void GPUImageCreateResizedSampleBuffer(CVPixelBufferRef cameraFrame, CGSize fina
3737
CMSampleTimingInfo timing = {frameTime, frameTime, kCMTimeInvalid};
3838

3939
CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixel_buffer, YES, NULL, NULL, videoInfo, &timing, sampleBuffer);
40+
CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
4041
CFRelease(videoInfo);
4142
CVPixelBufferRelease(pixel_buffer);
4243
}
@@ -77,7 +78,7 @@ - (id)initWithSessionPreset:(NSString *)sessionPreset cameraPosition:(AVCaptureD
7778

7879
// Having a still photo input set to BGRA and video to YUV doesn't work well, so since I don't have YUV resizing for iPhone 4 yet, kick back to BGRA for that device
7980
// if (captureAsYUV && [GPUImageContext supportsFastTextureUpload])
80-
if (captureAsYUV && [GPUImageContext supportsFastTextureUpload])
81+
if (captureAsYUV && [GPUImageContext deviceSupportsRedTextures])
8182
{
8283
BOOL supportsFullYUVRange = NO;
8384
NSArray *supportedPixelFormats = photoOutput.availableImageDataCVPixelFormatTypes;
@@ -105,16 +106,6 @@ - (id)initWithSessionPreset:(NSString *)sessionPreset cameraPosition:(AVCaptureD
105106
[videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
106107
}
107108

108-
// if (captureAsYUV && [GPUImageContext deviceSupportsRedTextures])
109-
// {
110-
// // TODO: Check for full range output and use that if available
111-
// [photoOutput setOutputSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
112-
// }
113-
// else
114-
// {
115-
// [photoOutput setOutputSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
116-
// }
117-
118109
[self.captureSession addOutput:photoOutput];
119110

120111
[self.captureSession commitConfiguration];

framework/Source/GPUImageVideoCamera.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,14 +719,15 @@ - (void)processVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer;
719719

720720
[GPUImageContext useImageProcessingContext];
721721

722-
if ([GPUImageContext supportsFastTextureUpload])
722+
if ([GPUImageContext supportsFastTextureUpload] && captureAsYUV)
723723
{
724724
CVOpenGLESTextureRef luminanceTextureRef = NULL;
725725
CVOpenGLESTextureRef chrominanceTextureRef = NULL;
726726

727727
// if (captureAsYUV && [GPUImageContext deviceSupportsRedTextures])
728728
if (CVPixelBufferGetPlaneCount(cameraFrame) > 0) // Check for YUV planar inputs to do RGB conversion
729729
{
730+
CVPixelBufferLockBaseAddress(cameraFrame, 0);
730731

731732
if ( (imageBufferWidth != bufferWidth) && (imageBufferHeight != bufferHeight) )
732733
{

framework/Source/iOS/GPUImageMovieWriter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ extern NSString *const kGPUImageColorSwizzlingFragmentShaderString;
2424
AVAssetWriterInputPixelBufferAdaptor *assetWriterPixelBufferInput;
2525
dispatch_queue_t movieWritingQueue;
2626

27-
CVOpenGLESTextureCacheRef coreVideoTextureCache;
2827
CVPixelBufferRef renderTarget;
2928
CVOpenGLESTextureRef renderTexture;
3029

framework/Source/iOS/GPUImageMovieWriter.m

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -501,17 +501,6 @@ - (void)createDataFBO;
501501

502502
if ([GPUImageContext supportsFastTextureUpload])
503503
{
504-
#if defined(__IPHONE_6_0)
505-
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, [[GPUImageContext sharedImageProcessingContext] context], NULL, &coreVideoTextureCache);
506-
#else
507-
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)[[GPUImageContext sharedImageProcessingContext] context], NULL, &coreVideoTextureCache);
508-
#endif
509-
510-
if (err)
511-
{
512-
NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d", err);
513-
}
514-
515504
// Code originally sourced from http://allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/
516505

517506

@@ -526,7 +515,7 @@ - (void)createDataFBO;
526515
CVBufferSetAttachment(renderTarget, kCVImageBufferYCbCrMatrixKey, kCVImageBufferYCbCrMatrix_ITU_R_601_4, kCVAttachmentMode_ShouldPropagate);
527516
CVBufferSetAttachment(renderTarget, kCVImageBufferTransferFunctionKey, kCVImageBufferTransferFunction_ITU_R_709_2, kCVAttachmentMode_ShouldPropagate);
528517

529-
CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault, coreVideoTextureCache, renderTarget,
518+
CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault, [[GPUImageContext sharedImageProcessingContext] coreVideoTextureCache], renderTarget,
530519
NULL, // texture attributes
531520
GL_TEXTURE_2D,
532521
GL_RGBA, // opengl format
@@ -576,11 +565,6 @@ - (void)destroyDataFBO;
576565

577566
if ([GPUImageContext supportsFastTextureUpload])
578567
{
579-
if (coreVideoTextureCache)
580-
{
581-
CFRelease(coreVideoTextureCache);
582-
}
583-
584568
if (renderTexture)
585569
{
586570
CFRelease(renderTexture);
@@ -634,9 +618,9 @@ - (void)renderAtInternalSize;
634618
glVertexAttribPointer(colorSwizzlingTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);
635619

636620
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
621+
[firstInputFramebuffer unlock];
637622

638623
glFinish();
639-
[firstInputFramebuffer unlock];
640624
}
641625

642626
#pragma mark -
@@ -709,7 +693,7 @@ - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
709693
void(^write)() = ^() {
710694
while( ! assetWriterVideoInput.readyForMoreMediaData && ! _encodingLiveVideo && ! videoEncodingIsFinished ) {
711695
NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:0.1];
712-
//NSLog(@"video waiting...");
696+
// NSLog(@"video waiting...");
713697
[[NSRunLoop currentRunLoop] runUntilDate:maxDate];
714698
}
715699
if (!assetWriterVideoInput.readyForMoreMediaData)
@@ -735,9 +719,13 @@ - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
735719
};
736720

737721
if( _encodingLiveVideo )
722+
{
738723
dispatch_async(movieWritingQueue, write);
724+
}
739725
else
726+
{
740727
write();
728+
}
741729
}
742730

743731
- (NSInteger)nextAvailableTextureIndex;

0 commit comments

Comments
 (0)