user454083
user454083

Reputation: 1397

Buffer size of CMSampleBufferRef

I am trying to get the size of CMSampleBufferRef from AVFoundation call-back

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

According to documentation https://developer.apple.com/library/mac/documentation/CoreMedia/Reference/CMSampleBuffer/index.html#//apple_ref/c/func/CMSampleBufferGetSampleSize

size_t CMSampleBufferGetTotalSampleSize ( CMSampleBufferRef sbuf );

If I understand it correctly, I should use this method to get buffer size. But I always got 0 from the return. and It is said that "If there are no sample sizes in this CMSampleBuffer, a size of 0 will be returned." In this case, I wonder if AVFoundation framework does not provide a buffer size information or I mis-understand document.

A follow up question: By the way, I wonder in this case if I could use

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

from pixelBuffer to get the size of sampleBuffer?

Upvotes: 5

Views: 5152

Answers (2)

Syed Ali Salman
Syed Ali Salman

Reputation: 2915

If your CMSampleBuffer expected to contain an Image then you can do something like below

- (CGSize)sampleBufferSize:(CMSampleBufferRef)sampleBuffer {
          CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
          size_t imageWidth = CVPixelBufferGetWidth(imageBuffer);
          size_t imageHeight = CVPixelBufferGetHeight(imageBuffer);
          return CGSizeMake(imageWidth, imageHeight);
}

Upvotes: 5

wao813
wao813

Reputation: 468

How do you create your AVCaptureSession?

Basically a CMSampleBuffer can contain multiple information.

  • Raw image buffer (which you can extract using CMSampleBufferGetImageBuffer)
  • Audio buffer (which you have to extract using CMSampleBufferGetBlockBuffer)
  • CoreVideo encoded image data buffer (also have to extract using CMSampleBufferGetBlockBuffer)

So you should first check if the captureoutput is what you wanted, and if it is and you expect an image, then you should get the image as CVPixelBufferRef then check the data size on the image using CVPixelBufferGetDataSize

Upvotes: 0

Related Questions