将CVImageBufferRef转换为CVPixelBufferRef

7

我刚开始学习iOS编程和多媒体,正在浏览苹果提供的一个名为RosyWriter的示例项目,链接在这里。在这里,我看到在下面的代码中有一个名为captureOutput:didOutputSampleBuffer:fromConnection的函数:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 
{   
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);

if ( connection == videoConnection ) {
    
    // Get framerate
    CMTime timestamp = CMSampleBufferGetPresentationTimeStamp( sampleBuffer );
    [self calculateFramerateAtTimestamp:timestamp];
    
    // Get frame dimensions (for onscreen display)
    if (self.videoDimensions.width == 0 && self.videoDimensions.height == 0)
        self.videoDimensions = CMVideoFormatDescriptionGetDimensions( formatDescription );
    
    // Get buffer type
    if ( self.videoType == 0 )
        self.videoType = CMFormatDescriptionGetMediaSubType( formatDescription );

    CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    
    // Synchronously process the pixel buffer to de-green it.
    [self processPixelBuffer:pixelBuffer];
    
    // Enqueue it for preview.  This is a shallow queue, so if image processing is taking too long,
    // we'll drop this frame for preview (this keeps preview latency low).
    OSStatus err = CMBufferQueueEnqueue(previewBufferQueue, sampleBuffer);
    if ( !err ) {        
        dispatch_async(dispatch_get_main_queue(), ^{
            CMSampleBufferRef sbuf = (CMSampleBufferRef)CMBufferQueueDequeueAndRetain(previewBufferQueue);
            if (sbuf) {
                CVImageBufferRef pixBuf = CMSampleBufferGetImageBuffer(sbuf);
                [self.delegate pixelBufferReadyForDisplay:pixBuf];
                CFRelease(sbuf);
            }
        });
    }
}

CFRetain(sampleBuffer);
CFRetain(formatDescription);
dispatch_async(movieWritingQueue, ^{

    if ( assetWriter ) {
    
        BOOL wasReadyToRecord = (readyToRecordAudio && readyToRecordVideo);
        
        if (connection == videoConnection) {
            
            // Initialize the video input if this is not done yet
            if (!readyToRecordVideo)
                readyToRecordVideo = [self setupAssetWriterVideoInput:formatDescription];
            
            // Write video data to file
            if (readyToRecordVideo && readyToRecordAudio)
                [self writeSampleBuffer:sampleBuffer ofType:AVMediaTypeVideo];
        }
        else if (connection == audioConnection) {
            
            // Initialize the audio input if this is not done yet
            if (!readyToRecordAudio)
                readyToRecordAudio = [self setupAssetWriterAudioInput:formatDescription];
            
            // Write audio data to file
            if (readyToRecordAudio && readyToRecordVideo)
                [self writeSampleBuffer:sampleBuffer ofType:AVMediaTypeAudio];
        }
        
        BOOL isReadyToRecord = (readyToRecordAudio && readyToRecordVideo);
        if ( !wasReadyToRecord && isReadyToRecord ) {
            recordingWillBeStarted = NO;
            self.recording = YES;
            [self.delegate recordingDidStart];
        }
    }
    CFRelease(sampleBuffer);
    CFRelease(formatDescription);
});
}

这里调用了一个名为pixelBufferReadyForDisplay的函数,它期望一个类型为CVPixelBufferRef的参数。

pixelBufferReadyForDisplay的原型

- (void)pixelBufferReadyForDisplay:(CVPixelBufferRef)pixelBuffer; 

但是在上面的代码中,在调用此函数时,它传递了变量 pixBuf,其类型为 CVImageBufferRef

因此我的问题是,是否需要使用任何函数或类型转换将 CVImageBufferRef 转换为 CVPixelBufferRef,或者这是编译器隐式完成的?

谢谢。

1个回答

27

如果您在 Xcode 文档中搜索 CVPixelBufferRef,则会找到以下内容:

typedef CVImageBufferRef CVPixelBufferRef;

因此,CVImageBufferRef是CVPixelBufferRef的同义词。它们可以互换使用。

您正在查看一些相当复杂的代码。RosyWriter和另一个名为“Chromakey”的示例应用程序对像素缓冲区进行了一些非常低级别的处理。如果您是iOS开发的新手,同时也是多媒体的新手,您可能不想那么快深入挖掘。这有点像一名一年级的医学生试图进行心肺移植手术。


1
为什么在iOS上可以从CVPixelBuffer创建CIImage,而在OSX上却不行?在iOS上,CIImage(CVPixelBuffer: currPixelBuffer)可以工作,但在OSX上不行。在OSX上,CIImage(CVImageBuffer: currPixelBuffer)可以工作。 - Adam
不同平台之间存在一些数据类型差异。有时候这并没有什么好的理由。 - Duncan C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接