2021-06-09 02:57:05 -06:00
|
|
|
//
|
|
|
|
// Frame.m
|
|
|
|
// VisionCamera
|
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 08.06.21.
|
|
|
|
// Copyright © 2021 mrousavy. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Frame.h"
|
|
|
|
#import <CoreMedia/CMSampleBuffer.h>
|
2023-09-01 04:58:32 -06:00
|
|
|
#import <Foundation/Foundation.h>
|
2021-06-09 02:57:05 -06:00
|
|
|
|
|
|
|
@implementation Frame {
|
2023-07-20 07:30:04 -06:00
|
|
|
CMSampleBufferRef _Nonnull buffer;
|
2021-06-09 02:57:05 -06:00
|
|
|
UIImageOrientation orientation;
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:39:25 -06:00
|
|
|
- (instancetype)initWithBuffer:(CMSampleBufferRef _Nonnull)buffer orientation:(UIImageOrientation)orientation {
|
2021-06-09 02:57:05 -06:00
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_buffer = buffer;
|
|
|
|
_orientation = orientation;
|
2023-12-29 06:09:56 -07:00
|
|
|
CFRetain(buffer);
|
2021-06-09 02:57:05 -06:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2023-12-29 06:09:56 -07:00
|
|
|
- (void)dealloc {
|
|
|
|
CFRelease(_buffer);
|
|
|
|
}
|
|
|
|
|
2021-06-09 02:57:05 -06:00
|
|
|
@synthesize buffer = _buffer;
|
|
|
|
@synthesize orientation = _orientation;
|
|
|
|
|
2023-12-29 06:09:56 -07:00
|
|
|
- (NSString*)pixelFormat {
|
|
|
|
CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(_buffer);
|
|
|
|
FourCharCode mediaType = CMFormatDescriptionGetMediaSubType(format);
|
|
|
|
switch (mediaType) {
|
|
|
|
case kCVPixelFormatType_32BGRA:
|
|
|
|
case kCVPixelFormatType_Lossy_32BGRA:
|
|
|
|
return @"rgb";
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
|
|
|
|
case kCVPixelFormatType_Lossy_420YpCbCr8BiPlanarFullRange:
|
|
|
|
case kCVPixelFormatType_Lossy_420YpCbCr8BiPlanarVideoRange:
|
|
|
|
case kCVPixelFormatType_Lossy_420YpCbCr10PackedBiPlanarVideoRange:
|
|
|
|
return @"yuv";
|
|
|
|
default:
|
|
|
|
return @"unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isMirrored {
|
|
|
|
switch (_orientation) {
|
|
|
|
case UIImageOrientationUp:
|
|
|
|
case UIImageOrientationDown:
|
|
|
|
case UIImageOrientationLeft:
|
|
|
|
case UIImageOrientationRight:
|
|
|
|
return false;
|
|
|
|
case UIImageOrientationDownMirrored:
|
|
|
|
case UIImageOrientationUpMirrored:
|
|
|
|
case UIImageOrientationLeftMirrored:
|
|
|
|
case UIImageOrientationRightMirrored:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isValid {
|
|
|
|
return _buffer != nil && CMSampleBufferIsValid(_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (size_t)width {
|
|
|
|
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(_buffer);
|
|
|
|
return CVPixelBufferGetWidth(imageBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (size_t)height {
|
|
|
|
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(_buffer);
|
|
|
|
return CVPixelBufferGetHeight(imageBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (double)timestamp {
|
|
|
|
CMTime timestamp = CMSampleBufferGetPresentationTimeStamp(_buffer);
|
|
|
|
return CMTimeGetSeconds(timestamp) * 1000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (size_t)bytesPerRow {
|
|
|
|
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(_buffer);
|
|
|
|
return CVPixelBufferGetBytesPerRow(imageBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (size_t)planesCount {
|
|
|
|
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(_buffer);
|
|
|
|
return CVPixelBufferGetPlaneCount(imageBuffer);
|
|
|
|
}
|
|
|
|
|
2021-06-09 02:57:05 -06:00
|
|
|
@end
|