2017-01-02 10 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

?結果は特にこの方法を指していますか? – dlbuckley

+0

@dlbuckley私は計測器を使ってみましたが、imageDataオブジェクトをリリースしようとしましたが、imageDataの解放時にアプリがクラッシュし始めました。 – Leena

+0

imageDataのリリースはどこですか? – dlbuckley

答えて

0

CG * - CoreGraphicsを使用すると、メモリリークをチェックして検索する楽器を使用してみましたCPUを使用して、リアルタイムのビデオのために遅すぎる、CVを使用*とGPU

// - (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