Created
February 29, 2016 23:53
-
-
Save ivanlesko/df60a04779cec4a2b02a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this ALMOST crops a CMSampleBufferRef. | |
// | |
// SampleBufferHelper.m | |
// sampleBufferBS | |
// | |
// Created by Ivan Lesko on 2/25/16. | |
// Copyright © 2016 Ivan Lesko. All rights reserved. | |
// | |
#import "SampleBufferHelper.h" | |
@implementation SampleBufferHelper | |
+ (CMSampleBufferRef)cropSampleBufferTo16by9:(CMSampleBufferRef)buffer { | |
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(buffer); | |
CGFloat bufferWidth = CVPixelBufferGetWidth(imageBuffer); | |
CGFloat bufferHeight = CVPixelBufferGetHeight(imageBuffer); | |
CGFloat newHeight = bufferHeight * 0.75; | |
CGFloat newYpos = bufferHeight * 0.875; | |
CGRect cropRect = CGRectMake(0, 200, 2000, 1800); | |
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer]; //options: [NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], kCIImageColorSpace, nil]]; | |
ciImage = [ciImage imageByCroppingToRect:cropRect]; | |
CFDictionaryRef empty; // empty value for attr value. | |
CFMutableDictionaryRef attrs; | |
empty = CFDictionaryCreate(kCFAllocatorDefault, // our empty IOSurface properties dictionary | |
NULL, | |
NULL, | |
0, | |
&kCFTypeDictionaryKeyCallBacks, | |
&kCFTypeDictionaryValueCallBacks); | |
attrs = CFDictionaryCreateMutable(kCFAllocatorDefault, | |
1, | |
&kCFTypeDictionaryKeyCallBacks, | |
&kCFTypeDictionaryValueCallBacks); | |
CFDictionarySetValue(attrs, | |
kCVPixelBufferIOSurfacePropertiesKey, | |
empty); | |
CVPixelBufferRef pixelBuffer; | |
CVPixelBufferCreate(kCFAllocatorSystemDefault, cropRect.size.width, cropRect.size.height, kCVPixelFormatType_32BGRA, attrs, &pixelBuffer); | |
CVPixelBufferLockBaseAddress( pixelBuffer, 0 ); | |
CIContext * ciContext = [CIContext contextWithOptions: nil]; | |
[ciContext render:ciImage toCVPixelBuffer:pixelBuffer]; | |
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 ); | |
CMSampleTimingInfo sampleTime = { | |
.duration = CMSampleBufferGetDuration(buffer), | |
.presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(buffer), | |
.decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(buffer) | |
}; | |
CMVideoFormatDescriptionRef videoInfo = NULL; | |
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo); | |
CMSampleBufferRef oBuf; | |
CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf); | |
CFRelease(attrs); | |
CFRelease(empty); | |
CFRelease(videoInfo); | |
CFRelease(pixelBuffer); | |
return oBuf; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment