【问题标题】:iOS - Automatically resize CVPixelBufferRefiOS - 自动调整 CVPixelBufferRef 的大小
【发布时间】:2011-12-19 12:34:45
【问题描述】:

我正在尝试根据用户的输入裁剪和缩放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 的不同方法。如果您可以在这里为任何方法提供解决方案,我可以给您满分

【问题讨论】:

  • 我只是想调整 CVpixcelbuffer 的大小。哪个是可能的?我刚刚使用了你的代码。但无法得到它预期的 o/p.in 选项 dic 我已经添加了原始大小和临时大小我已经指出了我的预期大小但它不起作用

标签: iphone objective-c ios avfoundation cmsamplebufferref


【解决方案1】:

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

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

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

size_t size = height * bytesPerRow;

memcpy(数据, baseAddress, 大小);

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

【讨论】:

  • 但是我需要将我的pixelBuffer写入视频,我该如何使用data来做到这一点?
  • 什么abput示例代码?无论如何,您正在使用 CVReturn status = CVPixelBufferCreateWithBytes(kCFAllocatorDefault, tempWidth, tempHeight, kCVPixelFormatType_32BGRA, &baseAddress[baseAddressStart], bytesPerRow, MyPixelBufferReleaseCallback, NULL, (CFDictionaryRef)options, &pxbuffer); 创建像素缓冲区
  • 是的,我正在用它创建一个像素缓冲区。我只是不明白你对我现在应该做什么的建议
  • @nikita 你能帮我解决创建视频时的内存问题吗..
猜你喜欢
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2014-09-25
  • 1970-01-01
  • 2014-10-20
  • 2012-09-19
  • 1970-01-01
相关资源
最近更新 更多