2017-01-02 1 views
2

저는 CVUImage 라이브러리 함수를 사용하여 CVPixelbuffer의 높이와 너비를 조작하고 있습니다. 세로로 비디오를 녹화하고 있는데 사용자가 장치를 회전하면 화면이 가로 모드로 조정됩니다. 가로 프레임을 화면의 가로 세로 비율로 맞추고 싶습니다.CVPixelBufferRef 조작 높이 및 너비

예 : - 세로 모드 320x568로 비디오를 시작하고 장치를 가로로 돌릴 때 프레임이 320x568에 맞게 568x320입니다. 이 것을 조정하기 위해 나는 CVPixelBuffer를 조작하려고 생각했다. 그러나 이것은 많은 기억을 먹고 있으며 결국에는 앱이 다운됩니다.

- (CVPixelBufferRef) GPUImageCreateResizedSampleBufferWithBuffer:(CVPixelBufferRef)cameraFrame withBuffer:(CGSize)finalSize withSampleBuffer:(CMSampleBufferRef)sampleBuffer 
{ 
    CVPixelBufferRef pixel_buffer = NULL; 

// CVPixelBufferCreateWithPlanarBytes for YUV input 
@autoreleasepool { 

    CGSize originalSize = CGSizeMake(CVPixelBufferGetWidth(cameraFrame), CVPixelBufferGetHeight(cameraFrame)); 

    CVPixelBufferLockBaseAddress(cameraFrame, 0); 
    GLubyte *sourceImageBytes = (GLubyte *)CVPixelBufferGetBaseAddress(cameraFrame); 
    CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, sourceImageBytes, CVPixelBufferGetBytesPerRow(cameraFrame) * originalSize.height, NULL); 
    CGColorSpaceRef genericRGBColorspace = CGColorSpaceCreateDeviceRGB(); 
    CGImageRef cgImageFromBytes = CGImageCreate((int)originalSize.width, (int)originalSize.height, 8, 32, CVPixelBufferGetBytesPerRow(cameraFrame), genericRGBColorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst, dataProvider, NULL, NO, kCGRenderingIntentDefault); 

    GLubyte *imageData = (GLubyte *) calloc(1, ((int)finalSize.width * (int)finalSize.height * 4)); 


    CGContextRef imageContext = CGBitmapContextCreate(imageData, (int)finalSize.width, (int)finalSize.height, 8, (int)finalSize.width * 4, genericRGBColorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 

    CGRect scaledRect = AVMakeRectWithAspectRatioInsideRect(originalSize, CGRectMake(0, 0, finalSize.width, finalSize.height)); 

    CGContextDrawImage(imageContext, scaledRect, cgImageFromBytes); 
    CGImageRelease(cgImageFromBytes); 
    CGContextRelease(imageContext); 
    CGColorSpaceRelease(genericRGBColorspace); 
    CGDataProviderRelease(dataProvider); 

    CVPixelBufferCreateWithBytes(kCFAllocatorDefault, finalSize.width, finalSize.height, kCVPixelFormatType_32BGRA, imageData, finalSize.width * 4, stillImageDataReleaseCallback, NULL, NULL, &pixel_buffer); 
    CMVideoFormatDescriptionRef videoInfo = NULL; 
    CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixel_buffer, &videoInfo); 

    CMTime frameTime = CMTimeMake(1, 30); 
    CMSampleTimingInfo timing = {frameTime, frameTime, kCMTimeInvalid}; 

    CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixel_buffer, YES, NULL, NULL, videoInfo, &timing, &sampleBuffer); 
    CVPixelBufferUnlockBaseAddress(cameraFrame, 0); 
    CFRelease(videoInfo); 
    // CVPixelBufferRelease(pixel_buffer); 

} 
return pixel_buffer; 

} 
+0

당신이 확인하고 메모리 누수를 찾기 위해 악기를 사용하여 시도 적이 실시간 비디오를 사용 CV * 및 GPU에 대한 너무 느립니다? 결과가이 방법을 특별히 가리 킵니까? – dlbuckley

+0

@dlbuckley 나는 계측기를 사용해 보았고 imageData 객체를 릴리스하려고 시도했지만 imageData를 공개 할 때 응용 프로그램이 충돌하기 시작했습니다. – Leena

+0

imageData를 출시하려는 곳은 어디입니까? – dlbuckley

답변

0

CG * -있는 CoreGraphics가 CPU를 사용하고

// - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 
    CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly); 

    CIImage *baseImg = [CIImage imageWithCVPixelBuffer:pixelBuffer]; 
    CIImage *resultImg = [baseImg imageByCroppingToRect:outputFrameCropRect]; 
    resultImg = [resultImg imageByApplyingTransform:outputFrameTransform]; 

    // created once 
    // glCtx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 
    // ciContext = [CIContext contextWithEAGLContext:glCtx options:@{kCIContextWorkingColorSpace:[NSNull null]}]; 
    // ciContextColorSpace = CGColorSpaceCreateDeviceRGB(); 
    // CVReturn res = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, VTCompressionSessionGetPixelBufferPool(compressionSession), &finishPixelBuffer); 

    [ciContext render:resultImg toCVPixelBuffer:finishPixelBuffer bounds:resultImg.extent colorSpace:ciContextColorSpace]; 

    CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly); 
+0

이미지의 일부를 자르거나 변형하지 않습니다. – Leena