gpt4 book ai didi

iphone - iOS - 自动调整 CVPixelBufferRef 的大小

转载 作者:可可西里 更新时间:2023-11-01 06:12:18 25 4
gpt4 key购买 nike

我正在尝试根据用户的输入裁剪和缩放 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");
}

我还尝试了使用 CMSampleBufferRef 的不同方法和 CGContext .如果你能在这里提供任何一种方法的解决方案,我都可以给你满分

最佳答案

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

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

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

size_t size = height * bytesPerRow;

memcpy(data, baseAddress, size);

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

关于iphone - iOS - 自动调整 CVPixelBufferRef 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8561456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com