GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Movement of the camera during photo shooting, causes "image segmentation"

Open valvoline opened this issue 14 years ago • 7 comments

Hi Brad,

I'm going forward with the test of your framework and i noticed a very annoying problem, occurring during the shooting of a still image.

Here there's my setup, forked from the SimplePhotoFilter example:

  • (void)viewDidLoad {

    [super viewDidLoad];

    stillCamera = [[GPUImageStillCamera alloc] init];

    filter = [[GPUImageSketchFilter alloc] init];

    [filter forceProcessingAtSize:CGSizeMake(768, 1024)];

    GPUImageRotationFilter *rotationFilter = [[GPUImageRotationFilter alloc] initWithRotation:kGPUImageRotateRight];

    [rotationFilter forceProcessingAtSize:CGSizeMake(768, 1024)];

    [filter prepareForImageCapture];

    [stillCamera addTarget:rotationFilter];

    GPUImageView *filterView = (GPUImageView *)self.view;

    [rotationFilter addTarget:filter];

    [filter addTarget:filterView];

    [stillCamera startCameraCapture];

}

and here there's my takePhoto:

  • (IBAction)takePhoto:(id)sender {

    [stillCamera capturePhotoProcessedUpToFilter:filter withCompletionHandler:^(UIImage *anImage, NSError *error){

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
    CGImageRef imageRef = [anImage CGImage];
    
    CGImageRetain(imageRef);
    
    [library writeImageToSavedPhotosAlbum:imageRef metadata:nil completionBlock:^(NSURL *assetURL, NSError *error2)
     {
    
         if (error2) {
    
             NSLog(@"ERROR: the image failed to be written");
    
         }
    
         else {
    
             NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
    
         }
    
         CGImageRelease(imageRef);
    
     }];
    

    }];

}

The result is as the one in the following example:

http://bit.ly/IdDRCP

valvoline avatar Apr 29 '12 09:04 valvoline

Apparently, this bug is closed related to the closed one #71.

Forcing the GPUImageOpenGLESContext to return FALSE on supportsFastTextureUpload, workaround the problem:

  • (BOOL)supportsFastTextureUpload; { return FALSE; }

Tested both on iPhone 4S and iPad3

c.

valvoline avatar Apr 29 '12 10:04 valvoline

Looking into this, it's not quite the same issue. The one in #71 was due to a bug in the way that frames of video were being handed off by the OS itself. This is a problem with my processing.

When you take a photo, it processes and saves that photo asynchronously. This means that it can be reading from the video frame in saving while other processing is going on at the same time. When the iOS 5.0 texture caches are disabled, I have to copy the entire photo into memory (which can lead to severe memory spikes), but that means that nothing else will touch the frame data when it comes time to write to disk. The texture caches allow me to access the memory directly, but if something else is rendered to that, you'll see the artifacting that you do here.

I need to put proper locking around the pixel buffers here to prevent access to them while this writing to disk is going on. Let me see about adding that.

BradLarson avatar May 02 '12 18:05 BradLarson

Ok,

let me know if I can help you.

valvoline avatar May 03 '12 07:05 valvoline

Try the latest code in the repository. I now pause the camera input while a photo is being processed. I tried locking around the pixel buffer, but this did nothing. Stopping the input of new frames seems to be the only way to guarantee that memory won't be overwritten while saving images with the texture cache support enabled.

In my tests, this produces photos at any size without the artifacts you describe above.

BradLarson avatar May 03 '12 22:05 BradLarson

Hi Brad,

Thanks for your fast fix. However, it does not work at all. Using the last commit, I receive systematically a Segmentation Fault at:

- (void)dealloc 
{
    [self stopCameraCapture];
    //    [videoOutput setSampleBufferDelegate:nil queue:dispatch_get_main_queue()];    

    [self removeInputsAndOutputs];

    if ([GPUImageOpenGLESContext supportsFastTextureUpload])
    {
-->        CFRelease(coreVideoTextureCache);  // EXC_BAD_ACCESS
    }

    if (audioProcessingQueue != NULL)
    {
        dispatch_release(audioProcessingQueue);
    }
}

Tried both on iPhone4S, iPhone4 and iPad3

valvoline avatar May 04 '12 07:05 valvoline

Hi,

For the one that encountered the same issue. I solved (temporarily), modifying the capturePhotoProcessedUpToFilter GPUStillCamera.m method as follow:

- (void)capturePhotoProcessedUpToFilter:(GPUImageOutput<GPUImageInput> *)finalFilterInChain withCompletionHandler:(void (^)(UIImage *processedImage, NSError *error))block;
{
    [photoOutput captureStillImageAsynchronouslyFromConnection:[[photoOutput connections] objectAtIndex:0] completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
        [self stopCameraCapture];

        [self captureOutput:photoOutput didOutputSampleBuffer:imageSampleBuffer fromConnection:[[photoOutput connections] objectAtIndex:0]];

        UIImage *filteredPhoto = [finalFilterInChain imageFromCurrentlyProcessedOutput];

        block(filteredPhoto, error);        
       // [self startCameraCapture];
    }];

    return;
}

Seems that the RESTARTING of the camera was causing the crash.

Please, note that in my pattern there is:

  • a PhotoViewController that capture the shoot and as the photo is saved in memory it return passing them to another ViewController.
  • PhotoViewController is an ARC based class, in a NO-ARC environment.

valvoline avatar May 04 '12 08:05 valvoline

OK, the first attempt was flawed, because it could lead to the behavior you saw. I've implemented a filter-level locking around the pixel buffer, which should prevent updates to that while an image saving operation is in progress. In my testing, this prevents the crash you saw while still eliminating the photo capture artifacts. Try the latest code and see if this works for you.

BradLarson avatar May 04 '12 15:05 BradLarson