0

私はiPhoneアプリに取り組んでいます。ライブラリからのビデオにいくつかのフィルタを適用したいと考えています。調査の後、この投稿http://www.sunsetlakesoftware.com/2010/10/22/gpu-accelerated-video-processing-mac-and-iosと彼の ColorTracking ソースから始めました。このコードから、AVCaptureSession を使用してリアルタイムでグレースケール フィルターを適用できます。右。しかし、ライブラリのビデオでも同じことをしたいです。そこで、AVAssetReader を使用してソース ビデオを読み取ります。AVCaptureSession と同様に、CVImageBufferRef を取得できます。AVCaptureSession と同じ幅、高さ、データ サイズですが、私の openGL ビューは常に黒です。

Frame をキャプチャする私のソース:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


    [self dismissModalViewControllerAnimated:NO];


    /// incoming video
    NSURL *videoURL = [info valueForKey:UIImagePickerControllerMediaURL];
    NSLog(@"Video : %@", videoURL);

    // AVURLAsset to read input movie (i.e. mov recorded to local storage)
    NSDictionary *inputOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:videoURL options:inputOptions];

    // Load the input asset tracks information
    [inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{


        NSError *error = nil;

        // Check status of "tracks", make sure they were loaded    
        AVKeyValueStatus tracksStatus = [inputAsset statusOfValueForKey:@"tracks" error:&error];
        if (!tracksStatus == AVKeyValueStatusLoaded)
            // failed to load
            return;




        /* Read video samples from input asset video track */
        AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:inputAsset error:&error];

        NSMutableDictionary *outputSettings = [NSMutableDictionary dictionary];
        [outputSettings setObject: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]  forKey: (NSString*)kCVPixelBufferPixelFormatTypeKey];
        AVAssetReaderTrackOutput *readerVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[inputAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] outputSettings:outputSettings];


        // Assign the tracks to the reader and start to read
        [reader addOutput:readerVideoTrackOutput];
        if ([reader startReading] == NO) {
            // Handle error
            NSLog(@"Error reading");
        }

        NSAutoreleasePool *pool = [NSAutoreleasePool new];
        while (reader.status == AVAssetReaderStatusReading) {

            CMSampleBufferRef sampleBufferRef = [readerVideoTrackOutput copyNextSampleBuffer];
            if (sampleBufferRef) {
                CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBufferRef);
                [self processNewCameraFrame:pixelBuffer];

                CMSampleBufferInvalidate(sampleBufferRef);
                CFRelease(sampleBufferRef);
            }
        }
        [pool release];

        NSLog(@"Finished");
    }];
}

フレームを処理するためのコードは次のとおりです。

- (void)processNewCameraFrame:(CVImageBufferRef)cameraFrame {


    CVPixelBufferLockBaseAddress(cameraFrame, 0);
    int bufferHeight = CVPixelBufferGetHeight(cameraFrame);
    int bufferWidth = CVPixelBufferGetWidth(cameraFrame);

    NSLog(@"Size : %i %i %zu", bufferWidth, bufferHeight, CVPixelBufferGetDataSize(cameraFrame));


    // Create a new texture from the camera frame data, display that using the shaders
    glGenTextures(1, &videoFrameTexture);
    glBindTexture(GL_TEXTURE_2D, videoFrameTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    // This is necessary for non-power-of-two textures
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    // Using BGRA extension to pull in video frame data directly
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));

    GLenum err = glGetError();
    if (err != GL_NO_ERROR)
        NSLog(@"Error uploading texture. glError: 0x%04X", err);

    [self drawFrame];

    glDeleteTextures(1, &videoFrameTexture);

    CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
}

OpenGL ビューでフレームを描画する場合:

- (void)drawFrame {    
    // Replace the implementation of this method to do your own custom drawing.
    static const GLfloat squareVertices[] = {
        -1.0f, -1.0f,
        1.0f, -1.0f,
        -1.0f,  1.0f,
        1.0f,  1.0f,
    };

    static const GLfloat textureVertices[] = {
        1.0f, 1.0f,
        1.0f, 0.0f,
        0.0f,  1.0f,
        0.0f,  0.0f,
    };

    [glView setDisplayFramebuffer];
    glUseProgram(grayScaleProgram);         

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, videoFrameTexture);

    // Update uniform values
    glUniform1i(uniforms[UNIFORM_VIDEOFRAME], 0);   

    // Update attribute values.
    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureVertices);
    glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



    [glView presentFramebuffer];
}

私はコードを追加しませんが、私を助ける必要があれば、私はできます... 私を助ける方法はありますか?

ありがとう!

4

2 に答える 2

1

グレースケール フィルターと言ったのは、kCVPixelFormatType_420YpCbCr8BiPlanarVideoRangeビデオ フレームを使用しているということですか? もしそうなら、テクスチャを としてバインドすると仮定していますGL_LUMINANCE。ただしAVAssetReader、ピクセル形式を使用kCVPixelFormatType_32BGRAし、処理方法ではテクスチャを としてバインドしますGL_RGBA

ビデオ キャプチャ ピクセル形式とテクスチャ バインディングのこのような不適切な組み合わせに出くわし、黒い画面になってしまいました。セットアップ コードのAVCaptureVideoDataOutput設定を確認します。AVCaptureSessionピクセル フォーマットとテクスチャ バインディングは同じである必要があります。

編集: 黒い画面のもう 1 つの考えられる理由は、OpenGL コンテキストがスレッド間で共有されていないという事実です。メイン キュー以外のカメラ ビデオ フレームにキューを使用する場合dispatch_get_main_queue()、メソッドは

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
       fromConnection:(AVCaptureConnection *)connection  

は、UI を更新できないバックグラウンド スレッドで呼び出されます。

メインのディスパッチ キューでキャプチャ セッションを設定してみて、何が起こるかを確認してください。

AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
// set video settings, frame rate .. etc
[videoOut setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
于 2011-11-24T01:54:06.760 に答える
0

cameraFrameをvideoFrameTextureにリンクしていないようです。
CVImageBufferRef image;
glBindTexture( CVOpenGLTextureGetTarget( image ), CVOpenGLTextureGetName( image ) );

于 2011-10-28T08:40:28.117 に答える