2013-09-06 5 views
7

私はiOSプログラミングとマルチメディアが初めてで、appleで提供されたRosyWriterというサンプルプロジェクトをthis linkに行っていました。ここで私は、コードに下記のコードでcaptureOutput:didOutputSampleBuffer:fromConnectionという名前の関数があることを見た:CVImageBufferRefをCVPixelBufferRefに変換する

- (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という名前の関数がpixelBufferReadyForDisplay

のプロトタイプ型のパラメータが CVPixelBufferRef

期待すると呼ばれています

- (void)pixelBufferReadyForDisplay:(CVPixelBufferRef)pixelBuffer; 

しかし、上記のコードでは、この関数を呼び出すときに変数pixBuf wh ichはタイプのCVImageBufferRef

だから私の質問は、それはCVPixelBufferRefにCVImageBufferRefを変換するために任意の関数や型キャストを使用する必要はありませんかこれはコンパイラによって暗黙的に行われますか?

ありがとうございました。

答えて

20

あなたはXcodeのドキュメントにCVPixelBufferRefで検索を行う場合は、以下を見つけることができます:

typedef CVImageBufferRef CVPixelBufferRef; 

のでCVImageBufferRefはCVPixelBufferRefの同義語です。彼らは交換可能です。

かなり荒々しいコードがあります。 RosyWriter、そして "Chromakey"と呼ばれるもう1つのサンプルアプリケーションは、ピクセルバッファーのかなり低レベルの処理を行います。 iOSの開発やマルチメディアに慣れていない人にとっては、そう深く、早く掘り下げたくないかもしれません。心肺移植をしようとする最初の一年間の医学学生のようなものです。

+0

iOSではCVPixelBufferからCIImageを作成できますが、OSXでは作成できないのはなぜですか? CIImage(CVPixelBuffer:currPixelBuffer)はiOSでは動作しますが、OSXでは動作しません。 CIImage(CVImageBuffer:currPixelBuffer)はOSX上で動作します。 – Adam

+0

プラットフォーム間でいくつかのデータ型の違いがあります。時にはそれに正当な理由がないこともあります。 –

関連する問題