Swift 3: How to access the value of matrix_float3x3 in a 48-byte CFData?
Asked Answered
C

3

6

I'm trying to access intrinsic matrix following this answer.

By running the commend below, I was able to get a 48-byte AnyObject, and I further convert it into a CFData.

let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil)

However, I checked the output of sampleBuffer in CMSampleBuffer.h:

/*! @constant   kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix
     @abstract  Indicates the 3x3 camera intrinsic matrix applied to the current sample buffer.
     @discussion Camera intrinsic matrix is a CFData containing a matrix_float3x3, which is column-major.
        ....
 */
CM_EXPORT const CFStringRef kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix  // CFData (matrix_float3x3) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_11_0);

How should I access the value in the matrix_float3x3 from the CFData?

Consols answered 1/2, 2018 at 14:37 Comment(0)
G
3

This should work:

  • Use the bridging from CFData to NSData to Data, and
  • the withUnsafeBytes method to get a pointer of the desired type to the data bytes,
  • .pointee to dereference the pointer.

Example:

if let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) as? Data {
    let matrix: matrix_float3x3 = camData.withUnsafeBytes { $0.pointee }

    // ...

}

The pointer type ($0 inside the closure) is inferred from the context as UnsafePointer<matrix_float3x3>.

Gerfalcon answered 1/2, 2018 at 14:54 Comment(0)
T
2

Here is an update of Martin R's answer since the withUnsafeBytes had been deprecated:

if let camData = CMGetAttachment(sampleBuffer, 
                                 key: kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, 
                                 attachmentModeOut: nil) as? Data {
    let matrix = camData.withUnsafeBytes { $0.load(as: matrix_float3x3.self) }
    ...
}
Tabithatablature answered 26/8, 2022 at 3:49 Comment(0)
S
1

That's version of Martin R's answer for objective-c:

CFTypeRef cameraIntrinsicData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil);
if(cameraIntrinsicData != nil){
    CFDataRef cfdr = (CFDataRef)(cameraIntrinsicData);
    matrix_float3x3 *camMatrix = (matrix_float3x3 *)(CFDataGetBytePtr(cfdr));
}
Sunbow answered 3/10, 2019 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.