GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Dropped FPS with the last commit

Open valvoline opened this issue 14 years ago • 8 comments

Hi Brad,

and thank you for your work.

After installed the last commit, in order to solve the bug about dropped audio at the end of the video, i noticed that FPS dropped dramatically at about 9fps on iPhone4.

I'm using a simple ToneCurveFilter running at 960x540 preset that shift red and blue curve, so I don't think that the GPU overhead can be the cause of this problem.

The problem is still present at 1280x720 preset and 1920x1080 both on iPhone4 and iPhone4S.

valvoline avatar Aug 27 '12 13:08 valvoline

If you're recording audio while using the GPUImageToneCurveFilter, you will see a lower recording framerate now. The reason for the audio being dropped before was that not all of the audio was being processed, which caused pops and cutouts. I now process all of the audio that comes in, but on devices like the iPhone 4 that causes enough load to slow the video recording performance.

Any suggestions you have on how to reduce the load for audio encoding are welcome, but I've chosen to sacrifice video framerate for non-broken audio here.

BradLarson avatar Aug 27 '12 14:08 BradLarson

@Brad,

I understand your constraints. Perhaps, some fine tuning on the created queues can do the trick. By default when you create your private queues they actually all point to the default global queue. You can point these queues to other queues to make a queue hierarchy using dispatch_set_target_queue(). The only thing Apple discourages is making an loop graph where you make a queue that points to another and another eventually winding back to pointing at the first one because that behavior is undefined.

On the initWithSessionPreset method, i changed slightly the queues behaviour as follow:

    cameraProcessingQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.cameraProcessingQueue", NULL);
    audioProcessingQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.audioProcessingQueue", NULL);

    dispatch_queue_t priorityBackground = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0);
    dispatch_set_target_queue(audioProcessingQueue,priorityBackground);

    dispatch_queue_t priorityHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
    dispatch_set_target_queue(cameraProcessingQueue,priorityHigh);

also, i changed as follow the captureOutput:didOutputSampleBuffer:fromConnection method:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    __unsafe_unretained id weakSelf = self;
    if (captureOutput == audioOutput)
    {
        CFRetain(sampleBuffer);
        dispatch_async([GPUImageOpenGLESContext sharedOpenGLESQueue], ^{
            [weakSelf processAudioSampleBuffer:sampleBuffer];
            CFRelease(sampleBuffer);
        });

    }
    else
    {
        if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) != 0)
        {
            return;
        }

        CFRetain(sampleBuffer);

        dispatch_async(cameraProcessingQueue, ^{
            //Feature Detection Hook.
            if (self.delegate)
            {
                [self.delegate willOutputSampleBuffer:sampleBuffer];
            }

            [weakSelf processVideoSampleBuffer:sampleBuffer];

            CFRelease(sampleBuffer);
            dispatch_semaphore_signal(frameRenderingSemaphore);
        });
    }
}

with these patch i'm able to capture upto 30fps using an iPhone4S (5.1.1). Was ~14.4FPS before.

However the changes are not effective for iPhone4 running 6.0b4.

Also, sometimes the video recorded is empty and i receive a random error on FBO buffer.

Any suggestions?

Thank you

valvoline avatar Aug 27 '12 15:08 valvoline

You can't change the video processing portion of this from running on the shared GPUImage queue to a generic dispatch queue. All frames being processed must run on the GPUImage queue to avoid simultaneous access to the OpenGL ES context, which will result in very odd behavior and ultimately crashes.

There might be another way to optimize the dispatch queues, but the altered delegate method you show above will not operate correctly.

BradLarson avatar Aug 27 '12 15:08 BradLarson

I understand. However as far as i know the hack above show that at least for iPhone4S the problem for low FPS is due to a misuse of the dispatch_async queue. Changing it as the above code, infact, improve at least the FPS performance.

Any hints on how to tune the GPUImageQueue?

On Aug 27, 2012, at 5:56 PM, Brad Larson [email protected] wrote:

You can't change the video processing portion of this from running on the shared GPUImage queue to a generic dispatch queue. All frames being processed must run on the GPUImage queue to avoid simultaneous access to the OpenGL ES context, which will result in very odd behavior and ultimately crashes.

There might be another way to optimize the dispatch queues, but the altered delegate method you show above will not operate correctly.

— Reply to this email directly or view it on GitHub.

valvoline avatar Aug 27 '12 16:08 valvoline

Again, this isn't a "misuse" of a dispatch queue, I deliberately maintain a single video processing queue because things will go off the rails if I don't. There's an opportunity to have a separate encoding queue for the audio and video writing, but that will need to be a serial queue to prevent simultaneous accesses to the writer (which will cause crashes otherwise). Video processing will need to block on a video frame being encoded to avoid the contents of the shared buffer changing during the encoding.

There are certainly ways to optimize this, but we'll need to avoid simultaneous audio and video writes to the encoder, as well as make sure that only one video processing operation is running at once.

BradLarson avatar Aug 27 '12 22:08 BradLarson

Hi Brad, thank you for your response.

I have modified the GPUImageOpenGLESContext init method as follow:

- (id)init;
{
    if (!(self = [super init]))
    {
        return nil;
    }

    _contextQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.openGLESContextQueue", NULL);

    dispatch_queue_t priorityHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
    dispatch_set_target_queue(_contextQueue,priorityHigh);

    return self;
}

leaving intact the other queues mentioned in the previous post. With this patch i'm able to obtain a ~27FPS on iPhone4S, probably due to the HIGH_PRIORITY queue.

On Aug 28, 2012, at 12:18 AM, Brad Larson [email protected] wrote:

Again, this isn't a "misuse" of a dispatch queue, I deliberately maintain a single video processing queue because things will go off the rails if I don't. There's an opportunity to have a separate encoding queue for the audio and video writing, but that will need to be a serial queue to prevent simultaneous accesses to the writer (which will cause crashes otherwise). Video processing will need to block on a video frame being encoded to avoid the contents of the shared buffer changing during the encoding.

There are certainly ways to optimize this, but we'll need to avoid simultaneous audio and video writes to the encoder, as well as make sure that only one video processing operation is running at once.

— Reply to this email directly or view it on GitHub.

valvoline avatar Aug 28 '12 06:08 valvoline

I've finally experimented with this, and found an approach that may work. I have both the video camera and audio inputs use different concurrent queues (high priority for the camera, low priority for the audio), feed the video into the GPUImage serial processing queue, but the audio no longer touches that. There is now a serial queue around the movie writer, which both the video and audio queues feed into. This guarantees that there won't be simultaneous accesses, and won't break like the other tries described above.

With this, I see a solid 30 FPS at 720p on an iPhone 4S when recording 720p video with audio. On an iPhone 4 running iOS 5.0, the framerate for recording only drops from 15 FPS to 13 FPS when audio is added vs. just video recording. There are probably still ways of tuning this, but this seems to mitigate the slowdown from adding an audio source.

BradLarson avatar Jun 09 '13 03:06 BradLarson

I've since modified GPUImageWriter so that it now uses its own OpenGL ES context, which should allow it to truly run in parallel to the rest of the filter pipeline. I'm still tweaking this, but it might finally fix the slowdown we've experienced here.

BradLarson avatar May 26 '14 23:05 BradLarson