iOS - 自动调整CVPixelBufferRef的大小

时间:2011-12-19 12:34:45

标签: iphone objective-c ios avfoundation cmsamplebufferref

我正在尝试根据用户的输入裁剪CMSampleBufferRef ratio,下面的代码采用CMSampleBufferRef,将其转换为CVImageBufferRef并使用CVPixelBuffer基于以下方式裁剪内部图像它的字节。此过程的目标是使用裁剪和缩放的CVPixelBufferRef写入视频

- (CVPixelBufferRef)modifyImage:(CMSampleBufferRef) sampleBuffer {
    @synchronized (self) {
        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
        // Lock the image buffer
        CVPixelBufferLockBaseAddress(imageBuffer,0); 

        // Get information about the image
        uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); 
        size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
        size_t width = CVPixelBufferGetWidth(imageBuffer); 
        size_t height = CVPixelBufferGetHeight(imageBuffer); 

        CVPixelBufferRef pxbuffer;
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                                 [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,  
                                 [NSNumber numberWithInt:720], kCVPixelBufferWidthKey, 
                                 [NSNumber numberWithInt:1280], kCVPixelBufferHeightKey,
                                 nil];
        NSInteger tempWidth = (NSInteger) (width / ratio);
        NSInteger tempHeight = (NSInteger) (height / ratio);

        NSInteger baseAddressStart = 100 + 100 * bytesPerRow;
        CVReturn status = CVPixelBufferCreateWithBytes(kCFAllocatorDefault, tempWidth, tempHeight, kCVPixelFormatType_32BGRA, &baseAddress[baseAddressStart], bytesPerRow, MyPixelBufferReleaseCallback, NULL, (CFDictionaryRef)options, &pxbuffer);

        if (status != 0) {
            CKLog(@"%d", status);
            return NULL;
        }

        CVPixelBufferUnlockBaseAddress(imageBuffer,0);

        return pxbuffer;
    }
}

一切正常,但是当我尝试使用此方法将其写入视频的输出时,它会一直接收内存警告。如果我保持相同的比例,那就好了

- (void)writeBufferFrame:(CMSampleBufferRef)sampleBuffer pixelBuffer:(CVPixelBufferRef)pixelBuffer {
    CMTime lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);    

    if(self.videoWriter.status != AVAssetWriterStatusWriting)
    {
        CKLog(@"%d", self.videoWriter.status);
        [self.videoWriter startWriting];
        [self.videoWriter startSessionAtSourceTime:lastSampleTime];
    }

    CVPixelBufferRef pxbuffer = [self modifyImage:sampleBuffer];
    BOOL success = [self.avAdaptor appendPixelBuffer:pxbuffer withPresentationTime:lastSampleTime];
    if (!success)
        NSLog(@"Warning:  Unable to write buffer to video");
}

我还尝试了使用CMSampleBufferRefCGContext的不同方法。如果你能为这里的任何方法提供解决方案,我可以给你全部分数

1 个答案:

答案 0 :(得分:1)

尝试在对-CVPixelBufferLockBaseAddress和-CVPixelBufferUnlockBaseAddress的两次调用中使用kCVPixelBufferLock_ReadOnly标志。 有时这个问题可以通过复制像素缓冲区来解决。执行一次分配:

unsigned char *data = (unsigned char*)malloc(ySize * sizeof(unsigned char));

之后,将数据从pixelBuffer复制到数据

  

size_t size = height * bytesPerRow;

     

memcpy(data,baseAddress,size);

之后,使用数据。希望,这会有所帮助。