SePröbläm
SePröbläm

Reputation: 5739

How to properly address the pixels in a CVPixelBuffer?

The short question is: What's the formula to address pixel values in a CVPixelBuffer?

I'm trying to convert a CVPixelBuffer in a flat byte array and noticed a few odd things: The CVPixelBuffer is obtained from a CMSampleBuffer. It's width and height are 852x640 pixels. In sum 545280 pixels, which would require 2181120 bytes considering 4 bytes per pixel.

Now the first odd thing is, that the same CVPixelBuffer returns 3456 bytes per row, which is enough space for 864 pixels. Where do those additional 12 pixels come from? If one row in the final image is only 852 pixel broad, but there are actually 864 pixels in a row of CVPixelBuffer, how do I know which bytes need to be copied? Or which of those bytes are unused? Btw, are they unused?

An other thing is the data size reported CVPixelBuffers, which is 2211848 bytes. Now if we multiply the 3456 bytes per row with 640 rows, we'll end up at 2211840 bytes. So again, we're left with 8 additional bytes. What's the matter of those 8 bytes? Are they unused? Are they at the end?

Any advice that sheds some light on that matter is welcome, Thank you.

Upvotes: 5

Views: 1339

Answers (1)

zzyzy
zzyzy

Reputation: 983

Internally, your CMSampleBuffer is padding each row to a multiple of 16 bytes so that it can efficiently sling data about with the processor's internal 128-bit wide registers. That's my guess. If you access the pixel data using its rowbytes parameter and (x,y) in the range of (0,0) to (851,639) you should never have an issue.

Upvotes: 3

Related Questions